[ACCEPTED]-Detect file change, offer to reload file-vim

Accepted answer
Score: 62

If you want to open the updated file use


It 1 should reload the open file.

Score: 55

As ashcatch said, the :checktime command will check for changes 12 on disk and prompt you to reload. So set 11 Vim to run checktime automatically after some event. Which 10 event to use is up to you. One possibility 9 is to use CursorHold, which fires after you move the 8 cursor and then let it sit still for updatetime milliseconds. (Default 7 4 seconds.)

:au CursorHold * checktime

You could also set it on WinEnter or 6 BufWinEnter so it changes every time you switch between 5 buffers/windows. If you're really paranoid 4 you could set it on CursorMoved so it checks the file 3 on disk every time you move the cursor, but 2 that's probably overkill and may lag a bit.

See 1 :h checktime, :h updatetime, :h autocmd-events-abc.

Score: 25

This is done using an auto command called 5 FileChangedShell. I'm too new to post links, but 4 your best bet would be to read the autocmd 3 part of the vim documentation (Google for 2 that)

but the gist is to set something like 1 the following line in your vimrc

:au FileChangedShell * echo "Warning: File changed on disk"
Score: 10

To improve on Carper's answer:

" Run checktime in buffers, but avoiding the "Command Line" (q:) window
au CursorHold * if getcmdwintype() == '' | checktime | endif

Without this 4 check Vim will spout errors if you try to 3 use the "Command Line" (q:) buffer, because 2 this buffer doesn't support the :checktime command. Found 1 this out thanks to kdlv on #vim.

Score: 9

You can manually trigger the checking with 5 :checktime. gvim does this everytime it regains focus, so 4 it is not necessary that often to do the 3 manual checking.

I don't know if there is 2 a different approach (like automatically 1 checking in a certain time interval).

Score: 3

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

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

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

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

More Related questions