4

when a file is changed and I'm working on it, vim prompts me two choices:

W11: Warning: File "foo.bar" has changed since editing started
See ":help W11" for more info.
[O]K, (L)oad File:

Is there a way to make it show me a diff between the buffer contents and what's on disk?

1 Answer 1

8

Place the following in your .vimrc file taken from :h :DiffOrig

command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis | wincmd p | diffthis

Then when prompted hit o for ok and then execute :DiffOrig. You will be presented with the diff.

If you decide that you want to take the file from disk execute :e! to reload the file from disk.

Quick overview of :DiffOrig

Vertically split a new buffer and read in the file in question from disk and mark both buffers to be diffed.

Glory of detail for :DiffOrig

  • :vert {cmd} will execute any {cmd} and any splits will be split vertically.
  • new open a new split with a new buffer
  • set bt=nofile set 'buftype' to nofile so no file will be written to disk
  • r is short for :read {file}. r # reads in to the buffer the alternative file, which is the buffer that was just split from. This alternative file is the file in question being read from disk.
  • 0d_ which is short for 0delete _. When the alternative file is read in it leaves a blank line at top. 0delete _ will remove the top line into the black hole register so it does not mess with other registers.
  • diffthis set buffer to diffed.
  • windcmd p switches back to the previous window. This is the same as <c-w>p in normal mode.
  • diffthis this sets the changed buffer to be included into the diff set.

For more information:

:h :DiffOrig
:h :e
:h :diff
:h vert
:h new
:h r
:h d
:h "_
:h windcmd
:h diffthis
:h :c_#
2
  • Very nice answer. Now if there was a way to make it automatic... :) (i.e., be prompted with a third choice when W11 is emitted) Commented Dec 13, 2011 at 15:35
  • @Andrea Spadaccini: Sorry I am not aware of anyway to make this automatic. Commented Dec 13, 2011 at 16:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.