How to send the ESC signal to vim when my esc key doesn't work?

Ctrl-[ sends the same character to the terminal as the physical Esc key. The latter is simply a shortcut for the former, generally.


If you want to be able to use a single key, as a pure *nix solution (without Vim mappings) you could define another key as Esc. Just like Emacs users remap CapsLock to Ctrl some Vim users (me included) remap CapsLock to Esc. This works for any *nix using X11.

Use xev -event keyboard (and then press CapsLock) to get the keycode for the CapsLock key (for me it is keycode 66). Then you can use xmodmap to remap the key:

xmodmap -e 'remove Lock = Caps_Lock' -e 'keycode 66 = Escape'

To get this at login you can add the xmodmap expressions to ~/.Xmodmap as follows:

remove Lock = Caps_Lock
keycode 66 = Escape

Although for the second part YMMV, since not all display managers run ~/.Xmodmap. You may need to add xmodmap .Xmodmap to .xinitrc on some of them.


Existing solutions notwithstanding, the conventional solution in Vim is to remap keys in your .vimrc configuration. In fact, many Vim users have an easier reachable key remapped to Esc. Popular choices are Ctrl+Enter, or jj, etc.

To enable this, just put something like the following into your .vimrc and reload it/restart Vim:

" Shift-Enter
inoremap <S-CR> <Esc>
" Double-j
inoremap jj <Esc>

More information and alternatives

Tags:

Vim