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

when the file is modified and I am working on it, vim offers me two options:

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 do this to show me the difference between the contents of the buffer and what is on the disk?

+4
source share
1 answer

Put in the .vimrc file taken from :h :DiffOrig

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

Then, when prompted, press o for ok, and then do :DiffOrig . You will be presented with diff.

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

Quick Review :DiffOrig

Separate the new buffer vertically and read from the disk in the corresponding file and mark both buffers that need to be distinguished.

Glory details for :DiffOrig

  • :vert {cmd} will execute any {cmd}, and any splits will be split vertically.
  • new open a new section with a new buffer
  • set bt=nofile set 'buftype' to nofile , so the file will not be written to disk
  • r not suitable for :read {file} . r # reads into the buffer an alternate file, which is the buffer just split. This alternate file is the file in question, read from disk.
  • 0d_ , which is short for 0delete _ . When an alternate file is read, it leaves an empty line at the top. 0delete _ will delete the top line in the black hole register so that it does not mess with other registers.
  • diffthis set the buffer to distinguish.
  • windcmd p switches back to the previous window. This is the same as <cw>p in normal mode.
  • diffthis this sets up a modified buffer for inclusion in the diff set.

For more information:

 :h :DiffOrig :h :e :h :diff :h vert :h new :hr :hd :h "_ :h windcmd :h diffthis :h :c_# 
+8
source

Source: https://habr.com/ru/post/1386125/


All Articles