Vim Tips Wiki
Vim Tips Wiki
Advertisement
Tip 1612 Printable Monobook Previous Next

created 2009 · complexity basic · version 7.0


It may be helpful to run a commmand periodically in the background. This can be used, for example, to write an auto-updating clock in Vim, or to check for user input via an input loop, such as in a game. In one application, Vim was used as a front end for a media player, where the song information had to be updated and other actions taken when a song finished.

Faking a timer[]

Since there is no timer function in Vim < version 8, a trick is needed: the CursorHold autocommand that re-triggers itself. In the following, feedkeys() function is used with specially crafted key sequence that has no effect except re-triggering the autoevent. The period depends on the value of updatetime option (also known as ut). The value of updatetime option is in milliseconds. Do not set the value too low.

autocmd CursorHold * call Timer()
function! Timer()
  call feedkeys("f\e")
  " K_IGNORE keycode does not work after version 7.2.025)
  " there are numerous other keysequences that you can use
endfunction


Comments[]

  • How about if Vim is in Insert mode for a while?
  • vim 8 has 'real' timer support.No need to fake timer.

Details are as here: https://github.com/vim/vim/blob/master/runtime/doc/version8.txt#L66

Advertisement