Is it possible to setup vim for auto updating on changing of file content?

I think about working with logs that can extend if new event happened.
What I expect is that vim will spot a file change and update the buffer.

To avoid "merge conflicts" I can run in read-only mode and just use vim as a perfect viewer with all search capability and well known user interface

  • You can set autoread but it’s bot quite the same – D. Ben Knoble Feb 13 at 13:52
  • If there is no auto-scrolling it is even better for me. I can always go to new portion of data with 'G' – Adam Mierzwiak Feb 13 at 13:58

Tracking file change automatically is OS-dependent, so by default Vim only checks file timestamps (:checktime) on focus gain (works in GUI) and after executing shell commands. But you can force it any moment using a bit of scripting.

For example, check by timer:

function! PollBuffer(bufnr, msec)
    " make sure autoread is on, so Vim won't ask any questions
    call setbufvar(a:bufnr, '&autoread', 1)
    " check timestamp by timer event
    return timer_start(a:msec, {_ -> execute(a:bufnr..'checktime')}, {'repeat': -1})

let timerID = PollBuffer(bufnr('foobar.txt'), 5000)
| improve this answer | |
  • I use vim on Ubuntu or WSL Ubuntu in BASH console In this function is fixed foobar.txt filename. Is there any variable/pointer to currently opened file that I can put in this place? – Adam Mierzwiak Feb 13 at 14:30
  • 2
    @AdamMierzwiak for example, expand('%'). – D. Ben Knoble Feb 13 at 14:47
  • 2
    @AdamMierzwiak Simply bufnr() without argument – Matt Feb 13 at 15:39

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.