10

I recently noticed that in normal mode when I type Ctrl-i (command for jumps) it is "confused" for the TAB key. In particular, I have this mapping:

nnoremap <Tab> :tabnext<Enter>
  • 2
    Ctrl+i and Tab are identical. Just like Ctrl+[ and Esc. If you compare what ASCII codes are sent for Tab and Ctrl+i (by typing into od for example), you will see that both produces ASCII 9 (011 in octal), which corresponds to "horizontal tab". – Kusalananda Jan 22 '20 at 16:50
  • 1
    If you're running vim in the terminal, there's nothing vim could do about it: Ctrl-I, Tab, Ctrl-Tab, etc are the same. But you could change your terminal emulator to send a different sequence for Ctrl-I, eg. \E[Y, and then remap that key in vim. Example with xterm: xterm -xrm '*VT100*translations: #override\n\Ctrl<Key>i:string("\033[Y")' –  Jan 22 '20 at 17:18
  • For an Xterm solution: https://unix.stackexchange.com/q/631241 – Quasímodo Feb 14 '21 at 12:18

2 Answers2

12

Terminal I/O applications just see the composed characters sent by the terminal, and cannot distinguish amongst specific key chords, while GUI applications can, because GUIs tend to operate in terms of key press and release messages.

Most terminals, and most terminal emulators, send a ␉ (U+0009) character down the (virtual) wire to the host system when either ⇥ Tab or ⎈ Control+I are pressed. This is not vim. This is how terminals work, and how the emulators that emulate them work too.

Similarly, and oft forgotten nowadays I observe, these terminals and terminal emulators send a ␛ (U+001B) character down the (virtual) wire to the host system when either ⎋ Esc or ⎈ Control+[ are pressed.

Good Pen
  • 205
JdeBP
  • 68,745
1

Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today. Some key combinations, like Ctrl + non-alphabetic cannot be mapped, and Ctrl + letter vs. Ctrl + Shift + letter cannot be distinguished. (Unless your terminal sends a distinct termcap code for it, which most don't.) In insert or command-line mode, try typing the key combination. If nothing happens / is inserted, you cannot use that key combination. This also applies to <Tab> / <C-I>, <CR> / <C-M> / <Esc> / <C-[> etc. (Only exception is <BS> / <C-H>.) This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.

Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. https://groups.google.com/forum/#!topic/vim_dev/Ym6D-kWIsyo

But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim release.

Ingo Karkat
  • 11,864