4

I have a custom comment-dwim-line command that I want to bind to the key combination C-M-; (in other words, control, meta and ; buttons pressed simultaneously). The built-in comment-dwim command is bound to M-; by default, and I want to keep this binding.

So far I have tried:

(global-set-key "\C-M-;" 'comment-dwim-line)

which throws global-set-key: Key sequence RET - ; starts with non-prefix key RET.

(global-set-key "\C-\M-;" 'comment-dwim-line)

which throws load-with-code-conversion: Invalid modifier in string.

(global-set-key "C-\M-;" 'comment-dwim-line)

which throws global-set-key: Key sequence C - M-; starts with non-prefix key C.

(global-set-key [?\C-M-;] 'next-line)

which throws read--expression: Invalid read syntax: "?".

(global-set-key [?\C-\M-;] 'next-line)

which throws Symbol's value as variable is void: M-.

Finally

(global-set-key (kbd "C-M-;") 'comment-dwim-line)

does not throw an error, but the key binding has no effect: when I actually press C-M-; it behaves the same as M-;(default comment-dwim is called).

I am using Emacs 24.4 on Mac OS X 10.10, running in iTerm2.

schatten
  • 261
  • 1
  • 8
  • Something happened to your formatting. In particular, it looks like it chopped off whatever goes wrong when you try `(kbd "C-M-;")`. That's what I use - could you update with that error message? – purple_arrows Nov 05 '14 at 21:17
  • @purple_arrows updated my question, thanks. – schatten Nov 05 '14 at 21:23
  • You seem to have still forgotten a right paren after `(global-set-key (kbd "C-M-;") 'comment-dwim-line`. – Drew Nov 05 '14 at 21:31
  • @Drew I accidentally deleted final ) when editing the question, it was in the code, but no success. – schatten Nov 05 '14 at 21:39
  • Side question: how come some many people use Emacs inside a terminal? I mean, it's great to see that users do take advantage of the effort we put into providing good support for terminal use, but I'm surprised at how many people seem to use Emacs that way. – Stefan Nov 05 '14 at 22:35
  • @Stefan I think a lot people still spend time in terminal anyway, even if they have another IDEs running in parallel as a main coding tool. So you need to have a terminal based editor to use for all purposes: organize your notes, edit commit messages, edit sql files, write some script, run shell interpreters for python and erlang with syntax highlight and autocomplete, and some types of coding maybe even easier to do in emacs if you are already familiar with it, etc. And if I am in terminal, I don't want to leave it until I have to or want to completely switch focus. – schatten Nov 05 '14 at 23:46
  • 1
    Of possible relevance is the thread on [problems with keybindings when using terminal](http://emacs.stackexchange.com/questions/1020/problems-with-keybindings-when-using-terminal). – Dan Nov 06 '14 at 16:48

3 Answers3

4

Your terminal likely translates C-M-; to M-;. Try testing it out in GUI Emacs.

If it works in the GUI, you'll know it's iTerm2 causing the issue.

nanny
  • 5,704
  • 18
  • 38
  • 2
    He's indeed likely using the terminal, not all key combos will work inside a terminal, so he would need a different keybinding, if the same behaviour is expected for GUI and console usage. – rimero Nov 05 '14 at 21:53
1

You forgot a backslash, \, before M.

(global-set-key [?\C-\M-;] 'next-line)

This one is OK (except you forgot the final )).

(global-set-key (kbd "C-M-;") 'comment-dwim-line)

This one does not work for ; (but it would work for "\C-\M-y", for instance).

(global-set-key "\C-\M-;" 'comment-dwim-line)

Finally, you say:

(global-set-key (kbd "C-M-;") 'comment-dwim-line) does not throw an error, but the key binding has no effect: when I actually press C-M-; it behaves the same as M-; (default comment-dwim is called).

I don't see that. Do you see this problem if you start Emacs using emacs -Q (no init file)? If so, provide a step-by-step recipe, starting with emacs -Q to reproduce it. If not, bisect your init file to find the culprit.

It works fine for me, on MS Windows, with all versions of Emacs (20 and later):

  1. emacs -Q --debug-init -nw

  2. Evaluate this:

    (defun comment-dwim-line () (interactive) (message "FOOOOOOOOO"))
    (global-set-key (kbd "C-M-;") 'comment-dwim-line)
    
  3. C-h k C-M-;

    C-M-; runs the command comment-dwim-line (found in global-map), which is an interactive Lisp function.

    It is bound to C-M-;.

    (comment-dwim-line)

    Not documented.

  4. C-M-; shows the message FOOOOOOOOO in the echo area.

Drew
  • 75,699
  • 9
  • 109
  • 225
1

Terminal emulators do not forward all possible key combinations to the shell, so Emacs likely does not see C-M-; when used in the terminal. You can visualize the command processed by your terminal emulator (iTerm2 in your case) by pressing C+v (CTRL+v) in the terminal, followed by a key combination. For C-v C-M-; I get with the XFCE terminal under Linux

$ ^[;

which is the same as M-;, so the CTRL key is basically ignored.

A possible workaround is to define an alternate key combination in Emacs, let us say C-c M-;, and to instruct iTerm2 to forward this combination when you press C-M-;. This way you can use the keys that you want in iTerm2, but Emacs will behave as if you pressed C-M-;. I have done this previously, but I do not have a Mac at hand right now to search for the iTerm2 options.

Edit: Just found this related question: Shift+Up isn't recognized by Emacs in a terminal

alexurba
  • 216
  • 1
  • 4
  • 1
    Of possible relevance is the thread on [problems with keybindings when using terminal](http://emacs.stackexchange.com/questions/1020/problems-with-keybindings-when-using-terminal). – Dan Nov 06 '14 at 16:53