3

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?

1 Answer 1

3

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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