Auto-reloading a file in VIM as soon as it changes on disk

In your ~/.vimrc:

set autoread

This is what worked for me

set autoread                                                                                                                                                                                    
au CursorHold * checktime  

The first line wasn't enough by itself. You can put in you .vimrc

credit to Phan Hai Quang


from this answer (refering to an answer by PhanHaiQuang and a comment by @flukus)

One can run this oneliner from ex (whithin vim) when needed (or put each command in vimrc, for when log-files are opened.)

:set autoread | au CursorHold * checktime | call feedkeys("lh")

Explanation:
- autoread: reads the file when changed from the outside (but it doesnt work on its own, there is no internal timer or something like that. It will only read the file when vim does an action, like a command in ex :!
- CursorHold * checktime: when the cursor isn't moved by the user for the time specified in 'updatetime' (which is 4000 miliseconds by default) checktime is executed, which checks for changes from outside the file
- call feedkeys("lh"): the cursor is moved once, right and back left. and then nothing happens (... which means, that CursorHold is triggered, which means we have a loop)

Tags:

Vim