2

When you change a file outside vim, vim notices, and prompts you for action: should it reload the file, and discard any changes you have? should it keep your changes?

Often, just re-reading the buffer is correct. Typically, this occurs:

  • Open a file in vim.
  • Hack away, save.
  • Change branches in git.
  • Vim detects the file has changed.

At this point, I really just want the file to get reloaded, as it is saved. (If the buffer isn't saved, the normal prompt is okay.) Me clicking/typing "Load file" is error prone if I have a stray buffer that isn't saved. Seeing that message only for buffers that are modified would be much safer.

Something that does exactly what I want would be great, but I've really got other desires too (diffing a modified buffer would be cool): can you script what happens when a file changes on-disk?

2 Answers 2

1

If :set autoread is not enough, you can indeed script the behavior; on this occasion, the FileChangedShell event is fired, and you can hook into that and influence the behavior. See :help FileChangedShell for details.

0

It sounds like vim's autoread option would do what you're looking for. You can turn that on by adding the following to your vimrc file or typing it into a vim session:

:set autoread

You can get more information about the option by using:

:help 'autoread'

For information on viewing the differences between what's in the vim buffer and what's on disk run:

:help :DiffOrig

That contains some code that you can put into your vimrc file, to define a :DiffOrig command that will do exactly that.

You must log in to answer this question.

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