Is it possible to set a different timeoutlen
depending on the typed key?
For example, I have this short timeout to go with my <Esc>
remapping to jk
set timeoutlen=200
I'd like to make timeoutlen
longer if I start with a <leader>
because I have some mappings that require sequences of keys that are not as easy to type as jk
.
The default setting for 'timeoutlen' is one second. If the 'timeout' option is reset, then Vim will not timeout for mapped key sequences. But it is better not to change the 'timeout' option setting and leave the option in its default value.
If a particular key sequence is handled by a window manager or is intercepted by the operating system, then Vim will not see that key sequence. Then, you can not use that key sequence in Vim. To determine whether Vim receives a key sequence, in insert mode press <CTRL-V> followed by the key sequence.
Generally, you want to set timeout and timeoutlen according to how quickly you generally type mappings, and you should set ttimeoutlen to a pretty small value: defaults.vim sets this to 100 milliseconds, but you can probably go a fair bit shorter than this without unwanted consequences.
These are pretty straightforward: if you increase the length of timeoutlen, then Vim will wait for longer after each keystroke of the mapping before aborting it and carrying out the behaviour of the keys typed so far.
There's nothing built-in. With regards to your mapping, you probably mean :inoremap jj <Esc>
, and for that to apply quickly, you just need to ensure that there are no other insert mode mappings that start with jj
. To avoid that the first j
appears only with a delay, you could use :autocmds
to toggle the 'timeoutlen'
value:
:autocmd InsertEnter * set timeoutlen=200
:autocmd InsertLeave * set timeoutlen=1000
The solution proposed by Ingo Karkat will affect all insert mode mappings, so it may break plugins that define other insert mode mappings that are hard to type in so short a time period.
In order to escape from insert mode without lagging, I have found a smarter way, which leads to plugin better-escape.vim.
The default shortcut to escape insert mode is jk
, you can change it via the following option:
let g:better_escape_shortcut = 'jj'
It will calculate the time interval between pressing the first char and the second char in the shortcut (the default is 150 ms). If you press those two chars quickly, you will leave insert mode. Otherwise, the characters will be written literally. To adjust the time interval, use the following option:
let g:better_escape_interval = 200
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With