1) I need to capture the next key press from the keyboard, if there are any in 200 millisecond. Which function should I use to achieve this?

2) Basically I need to re-send the captured key, with some prefix key added. How can I check the code of key press properly? (I only need to check letter [a-zA-Z] and Ctrl-letter.

  • Just to clarify, you want to capture the next keypress if it comes within 200ms of calling the function, but if it's after 200ms from calling the function ignore it?
    – DJMcMayhem
    Dec 20, 2018 at 19:21
  • yes, ignore if no key pressed with 200ms.
    – qeatzy
    Dec 20, 2018 at 19:25
  • Maybe you should explain to us what you want to do because having to capture a keypress, given a delay is not that common in Vim a plugin... Maybe there's a simpler way to do what you want and you are facing a XY problem
    – statox
    Dec 22, 2018 at 12:32
  • Yes, it's indeed A XY problem, and now I simply forget about to capture keys. there are 2 case I want this. 1. in terminal mode, C-w to delete word. Which solved by set termkey=<C-l>. 2. with set timeoutlen=200, want to map g-someletter, for some easy type letter, it's ok, but for other letter it is un-usable. Then again, I decided to forget about it, since currently it's not worth to dig further.
    – qeatzy
    Dec 22, 2018 at 12:54

1 Answer 1


1) This should work for you, though it isn't perfect:

function! GetKeyUnder200ms()
  let l:start = reltimefloat(reltime())
  let l:char = getchar()

  if reltimefloat(reltime()) - l:start < 0.200
    echo l:char
    echo "Too slow!"

The only downside to this approach is that you'll have to press a button regardless of whether you press it within 200 milliseconds or not.

Of course, change echo l:char and echo "Too slow!" to whatever you're actually trying to accomplish.

2) You can check that with this expression:

if nr2char(l:char) =~ "\a" || l:char < 32

Or this one:

if 'a' =~ '[\x00-\xFFa-zA-Z]'

Or even

if 'a' =~ '[\x00-\xFF]\|\a'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.