简体   繁体   中英

How to rebind Ctrl-[ in .vimrc

In vim, Ctrl + [ acts like Esc by default. However, I would like to rebind Ctrl + [ to perform a custom action. (Specifically, to Ctrl-t because I find it more natural for [ and ] to have complementary actions for ctags.)

However, when I execute the command:

:nnoremap <C-[> <Ct>

then my arrow key navigation get messed up. I'm not sure what happens, but clearly that's not the ideal way to do it.

I've tried to unbind Ctrl + [ but vim reports that it wasn't bound, and I've tried some tricks like first binding Ctrl + [ to itself and then unbinding. Always the same result.

Side note: Interestingly, when I add it to my .vimrc (as the last command) it's even worse. Something nondeterministic happens and vim opens randomly in one of these 3 states:

  • The bottom status line says "E73: tag stack empty", implying it received a Ctrl + t -esque command, however the if I hit a nav key like j , it deletes the current and bottom line and then puts me into insert mode. (Happens ~70% of the time.)

  • Sometimes it's in replace mode. (Happens ~15% of the time.)

  • Sometimes it's in normal mode. (Happens ~15% of the time.)

How can I properly remap Ctrl + [ to a different function in .vimrc ?

You can't rebind Ctrl [ . Pressing the Escape key in a terminal sends Ctrl [ , just like pressing the Tab key sends Ctrl I . There's no separate Esc code.

Vim is probably acting crazily when you try this because basically every ANSI key sequence starts with Ctrl [ , so your rebinding is firing on all terminal input , and whatever else is in the key sequence looks like more commands to vim.

Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today, even in GVIM. 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> / <CI> , <CR> / <CM> / <Esc> / <C-[> etc. (Only exception is <BS> / <CH> .) 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. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8

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 8 major release.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM