Improve vim responsiveness with better timeout settings
Vim is typically fast, but it can be painfully slow at times, too.
A great example of this is when you make a block selection, insert/append text, and then hit ESC to apply your action to the rest of your selection. It seems to take forever before you’re back in normal mode and can see your action applied to the selection.
After digging into the issue, I learned that it doesn’t have to be this way.
Vim has two settings that you let you control timeouts when working with map key sequences and terminal keycodes/escape sequences:
timeoutlen defaults to 1000 and is the delay in milliseconds that vim uses when checking for map key sequences. It’s also used when checking keycodes if ttimeoutlen is disabled.
ttimeoutlen defaults to -1 (disabled) and is the delay in milliseconds that vim uses when checking for keycodes.
Out of the box, my vim was taking a full second to switch between insert and normal mode because ttimeoutlen was disabled; every hit of the ESC key caused vim to wait the full timeout in anticipation of a possible keycode.
To get around this I added the following to my vimrc:
" Adjust keycode timeout length
set ttimeoutlen=100
Since doing so, I’ve noticed that vim has been much snappier and even more of a pleasure to work with than usual.
If you’re like me and use vim in tmux, it’s worth noting that I also had to adjust my tmux.conf to get the timeout settings above working as expected. tmux has its own built-in delay for escape sequences that needs to be tweaked.