Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key-specific timeoutlen in vim

Tags:

vim

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.

like image 669
qiubix Avatar asked Nov 09 '14 14:11

qiubix


People also ask

What is the default timeout for mapped key sequences in Vim?

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.

Why can’t I use a key sequence in Vim?

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.

What is the default value of T timeoutlen in Vim?

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.

Why does Vim take so long to map?

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.


2 Answers

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
like image 192
Ingo Karkat Avatar answered Sep 29 '22 22:09

Ingo Karkat


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
like image 36
jdhao Avatar answered Sep 29 '22 23:09

jdhao