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.

  • 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 5 '14 at 21:17
  • @purple_arrows updated my question, thanks. – schatten Nov 5 '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 5 '14 at 21:31
  • @Drew I accidentally deleted final ) when editing the question, it was in the code, but no success. – schatten Nov 5 '14 at 21:39
  • 1
    Of possible relevance is the thread on problems with keybindings when using terminal. – Dan Nov 6 '14 at 16:48
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.

  • 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 5 '14 at 21:53
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

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.

  • I accidentally deleted final ) when editing the question, it was in the code, but no success. – schatten Nov 5 '14 at 21:37

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.