0

I want a clock display in Vim. My statusline is pretty full, so I want to use the tabline. I use taboo.vim to control my tabline, and I replaced the useless X "button" with a clock display using the following line in my .vimrc:

" Put a clock in the top-right corner of the tabline
let g:taboo_close_tabs_label = "%{strftime('%a\ %e\ %b\ %I:%M:%S\ %p')}"

This creates a nice clock in the top-right corner:

Clock on tabline in Vim

The tabline updates when switching buffers, inserting text, etc. However, I want the clock to update every second. The closest I have come is with this:

" Update the clock whenever possible
autocmd CursorHold,CursorHoldI * silent redrawtabline

in conjunction with this (which I use for other purposes too):

set updatetime=100

Those two together cause the tabline to get redrawn every time I move the cursor (in addition to all the normal update times). But it's still not perfect because the clock still doesn't update when I'm not doing anything.

Is there a way to get my clock display to update every second (or every minute)?

1 Answer 1

2

You can use a timer, which you set up with timer_start(), in order to run a callback that will invoke redrawtabline for you every second.

In your specific situation:

function! RedrawTabline(timerID)
  silent redrawtabline
endfunction
call timer_start(1000, 'RedrawTabline', {'repeat': -1})

Using {'repeat': -1} will make this timer repeat every second, forever.

You can store the result of the timer_start() call with the timer ID into a variable, if you intend to eventually call timer_stop() on it.

7
  • 2
    For the most part though, I wouldn't recommend adding a clock to your status or tab line in Vim, redrawing can be an expensive operation and Vim is not very well suitable to perform this kind of action.
    – filbranden
    Aug 5 at 18:31
  • 1
    I'm glad you clarified this is actually not a good idea.
    – r_31415
    Aug 5 at 20:31
  • 1
    Thank you for the solution and the warning. When I wrote it how you did, I got Undefined function: RedrawTabline when opening vim. So I tried changing the definition to function! RedrawTabline(), But then I got the message "E118: Too many arguments for function: RedrawTabline" every second. Finally, I tried defining it like function! RedrawTabline(timerID) to consume the first argument and that worked.
    – nullromo
    Aug 5 at 22:35
  • 1
    @nullromo Sorry for the oversight and thanks for the edit! Glad this was helpful!
    – filbranden
    Aug 5 at 22:53
  • 2
    @nullromo function! should not be necessary. I define most functions as function F(…) abort. The ! is only needed for redefinitions.
    – D. Ben Knoble
    Aug 6 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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