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

后端 未结 6 1063
陌清茗
陌清茗 2020-12-08 04:49

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 b

6条回答
  •  [愿得一人]
    2020-12-08 05:03

    I came up with my own solution before seeing this question:

    (define-key input-decode-map [#x2000009] [#x6000069]) ; C-S-i
    (define-key input-decode-map [#x200000d] [#x600006d]) ; C-S-m
    (define-key input-decode-map "\C-i" [#x4000069])
    (define-key input-decode-map "\C-m" [#x400006d])
    (define-key input-decode-map "\C-[" [#x400005b])
    

    The reason this works is, the keycode that the letter key generates is the "real" keycode [on the left], whereas the keycode that tab, enter, escape generate are function key symbols. The function key symbols are already mapped in input-decode-map to the same symbols that the letters naturally generate, and will continue to work.

    The keycodes that I mapped them to use the same mechanism as keycodes for C-0, etc [i.e. keys that don't have control codes in ASCII]. So, they're described as e.g. C-i (since they're excluded from the special casing that describes them as TAB/etc), and still have event-modifiers/event-basic-type (control)/?i.

    The downside is that they're a bit harder to work with - you have to use the numbers, since they won't be generated by (kbd) and you can't call a function or variable within a vector literal. If you bind C-i or C-m to a movement function, it will work with shift selection (since even though they're "fake" keys the mode still knows that C-S-m is shifted and that it's normal equivalent is C-m)

    NOTE: This will impact text terminals, so if you use text terminals you will need to detect whether the gui is running with (when window-system ...) - if you use the Emacs Daemon with graphical windows you may want to put this in a window-setup-hook.

提交回复
热议问题