Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rebind Ctrl-[ in .vimrc

Tags:

vim

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-[> <C-t>

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?

like image 850
B-Con Avatar asked Mar 19 '23 12:03

B-Con


1 Answers

You can't rebind Ctrl[. Pressing the Escape key in a terminal sends Ctrl[, just like pressing the Tab key sends CtrlI. 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.

like image 64
Eevee Avatar answered May 11 '23 17:05

Eevee