an9wer

Esc Delay in Vim

posted:

2022-01-23

I've been annoyed by a strange behavior in Vim for quite a while: when I press Esc to exit insert or command mode and return to normal mode, there's a noticeable delay. This delay interrupts my flow, as I have to wait before I can continue typing or issuing commands, making the experience feel unresponsive.

Why does the Esc delay happen? The delay happens because some key mappings - either in Vim or in the terminal (e.g., xterm, urxvt, or st) - contain the keycode of the Esc key, which is ^[, as part of their sequence. Vim waits briefly to determine whether the Esc press is a standalone key or the beginning of a longer mapped sequence. For example:

  1. key mappings in Vim: inoremap <esc>x <esc>:echom "balabala"<cr> [1]

  2. key mappings in st (the terminal of my choice): the keycode of the key "F5" is mapped to ^[[15~ [2]

Then, how to avoid the Esc delay? In short (check out :h ttimeout for more details):

  1. If it is caused by the key mappings in Vim, set a short timeoutlen (e.g., set timeoutlen=50).

  2. If is is caused by the key mappings in the terminal, set a short ttimeoutlen (e.g., set ttimeoutlen=50).

Thanks for reading :)

Further Readings