Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: show diff on W11 (file changed since editing started)

Tags:

vim

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?

like image 274
Andrea Spadaccini Avatar asked Feb 22 '23 03:02

Andrea Spadaccini


1 Answers

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_#
like image 118
Peter Rincker Avatar answered Feb 24 '23 17:02

Peter Rincker