I'm trying to make a mapping which does one thing on the first press of a key, and also does something else if I press the key again within timeout
. Specifically:
nnoremap <silent> <Esc><Esc> :set hls<CR>:let @/="\\<<C-R>=expand('<cword>')<CR>\\>"<CR>
nnoremap <silent> <Esc> :noh<CR>
…I want pressing Esc in normal mode to stop highlighting the current search (immediately, without waiting for timeout
), and pressing Esc again to highlight the word under the cursor.
Things I've tried:
These mappings as-is: success, but there's a distracting delay when I press Esc once.
Adding
<nowait>
to the second mapping: it consumes the key press so the first mapping never happens.Adding
<nowait>
to the second mapping, and<Esc>
at the end:<Esc>
does not put Vim in "waiting for another key" mode (presumably because its a non-recursive mapping).Adding
<nowait>
and:call feedkeys("\<lt>esc>")<CR>
(with various options) to the second mapping: hang.
Solutions that seem plausible:
- Temporarily remove the
<Esc>
mapping, send<Esc>
, and re-add it. - Have only one mapping which knows how to wait for a second Esc.
- Somehow put Vim into a "waiting for more keys" mode with
<Esc>
in the buffer. - Having a mapping which behaves like
<nowait>
but doesn't "consume" the key press.
Any ideas?
timeout
delay anyway... So you can reduce thetimeoutlen
value to reduce the delay.timeout
.timeout
to highlight the word under the cursor.