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
- The trick with K_IGNORE does not work anymore after version 7.2.025 (Oct-2008). You need to use different sequence of keys. Ref: http://groups.google.com/group/vim_use/browse_thread/thread/8c535e5cf2b35f63/e40998ff7f2d909b?lnk=gst&q=yakov#e40998ff7f2d909b