Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
I think this is what you mean, but my specific use case is when I check out a different git branch or commit, I want vim to reload the file. – Nathan Long Feb 12 at 18:56
up vote 17 down vote accepted

In your ~/.vimrc:

set autoread
share|improve this answer

Autoread does not work correctly. The following works for me:

You need to first install the script from here.

I got the best results by calling the setup function directly, like so.

let autoreadargs={'autoread':1} 
execute WatchForChanges("*",autoreadargs) 

The reason for this, is that I want to run a ipython/screen/vim setup.

You can easily convert this into an enhanced version of view.

script the process..

mkdir -p ~/bin
cat <<`OUT` > ~/bin/vimviewer
#!/usr/bin/env sh
exec vim -n --cmd "source /home/bryan/.vim/.vimrc.watchforchanges | let autoreadargs={'autoread':1} | execute WatchForChanges('*',autoreadargs)" $@

chmod 755 ~/bin/vimviewer
vimview test.txt
share|improve this answer
If you're using one of the vim GUIs such as gVim or MacVim then the autoread command integrates fine with the rest of the environment. E.g. editing the file in some other program, another editor or from source control, then switching back to vim will autoread the updated files. The problem seems to be with how the terminal version of vim integrates. The terminal version can't tell when you switch between programs either in the GUI or using terminal multiplexers like tmux, so it fails to autoread at relevant times. It would be nice if vim would just subscribe to relevant file-system events. – bames53 May 3 '14 at 23:31

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

share|improve this answer
The autocmd shouldn't be necessary--checktime is supposed to run internally at the same time the CursorHold event is triggered. In other words, you're just making Vim do a second checktime. – Heptite Jun 18 at 16:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .