Wikia

Vim Tips Wiki

Timer to execute commands periodically

Talk0
1,612pages on
this wiki
Revision as of 06:38, July 13, 2012 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 timerEdit

Since there is no timer function in Vim, 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


CommentsEdit

  • How about if Vim is in Insert mode for a while?

Around Wikia's network

Random Wiki