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[]
- 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
- How about if Vim is in Insert mode for a while?
- In Insert mode, you can harness CursorHoldI and CursorMovedI in addition to CursorHold and CursorMoved; but the risk of interference remains with other uses of these events, or with the original use of 'updatetime' to write the swap file.
- 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