I have a vim script function MyFunction that returns a string value. I want to display this output in the statusline and so I have set the statusline as follows statusline +=\ %{MyFunction()}.

I want this function to be called periodically (say every second) and display the updated value in the statusline.

Right now, If I'm in the window and move the cursor around, the statusline is updated but when the cursor is not moving or if the window is inactive, it just stays at one value.

How can I update it periodically?


There is an easy way to trigger re-evaluating your statusline. Simply reset the option value. E.g.

:let &stl=&stl

which basically means to set the statusline option to the current value. It should force a redraw then. You could plug this even into a timer, so e.g.

 :call timer_start(1000, {-> execute(':let &stl=&stl')}, {'repeat': -1})

This sets up a timer that triggers every second and will re-evaluate your statusline.

Note, that redraws are sometimes not desired (because they might overwrite a status message or remove the intro screen). So I wouldn't not recommend this to do in general unless you know what you are doing.

  • Thanks. That works. What is {-> in that command? is that some sort of lambda expression? – Durga Swaroop Nov 8 '18 at 18:43
  • correct, it is a lambda expression (see :h expr-lambda) – Christian Brabandt Nov 8 '18 at 18:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.