41

I'm trying to set a key-binding to Ctrl+TAB in Emacs. I used the following call:

(global-set-key (read-kbd-macro "C-TAB") 'my-func)

However, whenever I use it, I get a

<C-tab> is undefined

error message. Trying to set the binding to "C-tab" results in an error message.

How can I set my binding to C-TAB?

Martin Cote
  • 28,864
  • 15
  • 75
  • 99

6 Answers6

53

Unlike others have suggested, it is a good idea to use kbd (or read-kbd-macro which is basically the same thing) in case you ever want to use the same configuration files in other versions of Emacs; kbd works across several versions of Emacs and XEmacs, where the internal representation of key sequences are different.

(global-set-key (kbd "<C-tab>") 'my-func)

The input format used by read-kbd-macro is documented in the docstring of edmacro-mode:

  • The special words RET, SPC, TAB, DEL, LFD, ESC, and NUL represent special control characters. The words must be written in uppercase.

  • A word in angle brackets, e.g., <return>, <down>, or <f1>, represents a function key. (Note that in the standard configuration, the function key <return> and the control key RET are synonymous.) You can use angle brackets on the words RET, SPC, etc., but they are not required there.

This is written somewhat unfortunately; the TAB referred to in the first bullet point is the ASCII character for TAB, and adding the Control modifier does something nonsensical to it. When you press Control-Tab, Emacs sees it (via your windowing system; it will not work in a text terminal) as <tab> with a Control modifier, which you can represent as C-<tab> or <C-tab>.

Jouni K. Seppänen
  • 43,139
  • 5
  • 71
  • 100
  • Is there a workaround for `` in emacs in a text terminal? I asked on serverfault: http://serverfault.com/questions/81688/problem-with-ctrl-tab-keybinding-in-emacs-in-gnome-terminal – Tom Nov 05 '09 at 13:00
  • kbd is a macro documented at http://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Sequences.html and is used to convert the keybinding format used in Emacs documentation to values understood by functions such as global-set-key. – Jouni K. Seppänen Feb 12 '12 at 20:04
  • Why is it "" instead of "C-"? – Ell Oct 28 '22 at 05:32
46
(global-set-key [C-tab] 'my-func)
Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94
11

It's because you are using read-kbd-macro incorrectly. When you see what is bound to a key:

C-h k C-TAB

Emacs tells you:

<C-tab> is undefined.

You need to include the <> in your invocation of read-kbd-macro.

(global-set-key (read-kbd-macro "<C-tab>") 'my-func)

And, I don't know how to generate <C-TAB>, but it's not the same as <C-tab>.

(equal (kbd "<C-TAB>") (kbd "<C-tab>"))
->
nil
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
8

Note that you can also call global-set-key interactively. You can then see the correct binding command with repeat-complex-command (see also KeybindingGuide):

  1. M-x: global-set-key
  2. Type the key combination you want
  3. Use C-x ESC ESC (repeat-complex-command) to see the apropiate command. In your case I get:

    (global-set-key (quote [C-tab]) (quote my-func))
    
danielpoe
  • 3,756
  • 23
  • 18
3

Instead of using read-kbd-macro, try using the more plain syntax?

;(global-set-key [(control tab)] 'my-func)

Perhaps the plainer syntax will make a difference?

More on read-kbd-macro and global-set-key.

artlung
  • 33,305
  • 16
  • 69
  • 121
  • 1
    The big advantage of using `(kbd)` is that you can use the same syntax that Emacs returns when you call `describe-key`, which makes it completely trivial to deal with (as demonstrated nicely by Trey's answer). – phils Aug 16 '11 at 22:40
0

In the gnu emacs lisp reference manual, section 21.1 "Key Sequences", they reference using \t to represent the tab key.

Using the syntax shown in the Lisp reference manual, I would use the following command:

(global-set-key (kbd "C-\t") 'my-func)

CinCout
  • 9,486
  • 12
  • 49
  • 67
skm
  • 9
  • 1