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

Assuming that a file doesn't have unsaved changes in the VIM buffer, I'd like it to reload automatically if the file changes on disk. The most frequent use case for this is when I update the working copy in which the file resides.

How can I achieve this?


Solution 1:

In your ~/.vimrc:

set autoread

Solution 2:

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

Solution 3:

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)