Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

emacs - How do I bind a command to C-i without changing TAB?

In emacs, I want to bind a command to C-i. So I put (global-set-key "C-i" 'forward-word)

in my .emacs file. This works, except now the TAB key is bound to 'forward-word as well.

How do I bind a command to C-i without changing TAB?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In short, this should solve the problem for you:

(setq local-function-key-map (delq '(kp-tab . [9]) local-function-key-map))
(global-set-key (kbd "C-i") 'forward-word)

Longer version:

From the emacs lisp documentation on function keys:

In ASCII, C-i and <TAB> are the same character. If the terminal can distinguish between them, Emacs conveys the distinction to Lisp programs by representing the former as the integer 9, and the latter as the symbol tab.

Most of the time, it's not useful to distinguish the two. So normally local-function-key-map (see Translation Keymaps) is set up to map tab into 9. Thus, a key binding for character code 9 (the character C-i) also applies to tab. Likewise for the other symbols in this group. The function read-char likewise converts these events into characters.

So, once you do the following, you can see the difference in the key bindings:

(setq local-function-key-map (delq '(kp-tab . [9]) local-function-key-map))

;; this is C-i
(global-set-key (kbd "C-i") (lambda () (interactive) (message "C-i"))) 
;; this is <tab> key
(global-set-key (kbd "<tab>") (lambda () (interactive) (message "<tab>")))

Note, each mode sets up the various TAB bindings differently, so you may need to do customization per mode that you care about.

Version Dependency:

The above works for Emacs 23.1. From the NEWS file:

Function key sequences are now mapped using `local-function-key-map', a new variable. This inherits from the global variable function-key-map, which is not used directly any more.

Which means, in versions 22 and earlier, you can get the same effect by using the variable function-key-map. I tested this and found it to work with Emacs 21.

(setq local-function-key-map (delq '(kp-tab . [9]) function-key-map))
(global-set-key (kbd "C-i") 'forward-word)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...