Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect file change, offer to reload file

Tags:

vim

If you want to open the updated file use

:e

It should reload the open file.


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

:au CursorHold * checktime

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

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


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

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

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

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 check Vim will spout errors if you try to use the "Command Line" (q:) buffer, because this buffer doesn't support the :checktime command. Found this out thanks to kdlv on #vim.