Fandom

Vim Tips Wiki

Timer to execute commands periodically

Redirected from VimTip1612

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

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 < 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


CommentsEdit

  • 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

Also on Fandom

Random Wiki