4

I insert the plus minus symbol ± frequently enough that I want to bind it to a key.

From emacs -Q, I've tried this:

(global-set-key (kbd "C-c m") "±")

However, when I enter C-c m, the minibuffer displays C-u 1-, suggesting it's waiting for me to finish entering a sequence.

I can bind C-c m to other symbols:

(global-set-key (kbd "C-c m") "⇔")

This works as expected: C-c m inserts the double-arrow .

I've tried other keys as well, e.g.

(global-set-key (kbd "C-c a") "±")

This gives the same result: emacs interprets it as C-u 1-.

Is there something special about the plus-minus glyph that makes Emacs intepret it as a prefix argument? Why can't I insert it as I would other characters?

I have solved the immediate problem by defining an abbrev to insert the plus-minus glyph, but I would like to know why the keybindings above don't work.

Update:

Inserting the character via (insert ) does work:

(global-set-key (kbd "C-c m") (lambda () (interactive) (insert "±")))
  • Weird. Out of curiosity, try binding the key to a call to insert wrapped in a lambda. – Dan Dec 21 '18 at 15:58
  • done! That does work. – Tyler Dec 21 '18 at 16:15
  • 1
    This looks like a bug, to me. – Drew Dec 21 '18 at 16:47
  • I agree with @Drew. And bugs can be reported with M-x report-emacs-bug. More information is here. – zck Dec 21 '18 at 16:52
  • I submitted Emacs bug #33829 for this. It might not be considered a bug. Probably has to do with what's allowed in the string value for a keyboard macro, and how such a string is interpreted as a macro. – Drew Dec 21 '18 at 16:53
4

With respect to why you saw what you saw:

As Eli Zaretskii said in a reply to bug report #33829, the string is interpreted as a keyboard macro, and Emacs "interprets ±, which is a single byte with the 8th bit set, as a meta character".

It's interpreted as M-1, which is shown in the echo area as C-u 1-, as the beginning of a numeric prefix arg.

The mistake/gotcha is to expect that all bytes in a keyboard-macro string are interpreted as characters (which are just inserted). This is a byte that is interpreted as a key bound to a command. It just happens to be renderable also as a Unicode character, ±.

In the context of key-binding to a (keyboard-macro) string, as in the example -- especially since that is easily and commonly used to insert a Unicode character (or any char that is not so easy to type), it's easy to forget that keyboard-macro strings are command sequences.

  • Great, thanks. That makes sense. Avoiding the gotcha seems to depend on you knowing when extended ascii stops and unicode starts, or otherwise distinguishing between single and multi-byte characters. In my case, I pulled the symbols from helm-unicode, which doesn't indicate which symbols are multi-byte (nor would I have expected it to, before now). Fun fact: ± is a single byte, but ∓ is multibyte :) – Tyler Dec 21 '18 at 22:35
4

You can get what you want with

(global-set-key (kbd "C-c m") [177])

or if you prefer

(global-set-key (kbd "C-c m") [?±])

Another solution uses the package key-chord which allows you to assign commands to keys pressed simultaneously. After you have installed it, you can define

(key-chord-define-global "-=" [177])

and then pressing "-" and "=" at the same time does what you want.

Using vectors to represent unicode characters seems to be more reliable than keyboard macros. Emacs calls self-insert-command on the character C when it sees the key [C] where C is the character (the decimal repersentation) of the unicode character you want.

  • Interesting, thanks. I'm curious what the difference between a vector and the equivalent keyboard macro is. There are a few ascii codes that are used to represent modified numbers. ±²³´µ¶·¸¹° are used for M-1 through M-0, but this only effects keyboard macros, not vectors, as your solution demonstrates. – Tyler Dec 21 '18 at 21:14
  • 1
    I think what is happening is that M- is still thought of as changing one bit (presumably adding 128) to the ascii code for 1,2 etc. So it is that the keyboard macro "±" is interpreted as "M-1". – Aidan Schofield Dec 21 '18 at 21:31
  • that looks right, and applies to other characters too - the symbol î is ascii 238, or 110 + 128. n is ascii 110, so trying to use the keyboard macro "î" fails too - presumably because Emacs uses that code to represent M-n. – Tyler Dec 21 '18 at 21:42
  • Here's confirmation from Eli Z: debbugs.gnu.org/cgi/bugreport.cgi?bug=33829#11 I guess this is obvious to people who know which characters are single bytes and which are multibytes ;) – Tyler Dec 21 '18 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.