10

If there are external changes made to a file being edited in Vim I understand that I can reload the content of the file by :edit. But, assume that (internally) I've made changes to the file that I want to preserve. Is there a way to integrate the two sets of changes (external and internal)? From what I understand, :edit simply takes the external changes.

4
  • 3
    Have to read :h autoread? Is that what you're looking for?
    – statox
    Feb 24, 2016 at 16:32
  • 2
    :autoread reads in the external changes but in the event that changes are made to the file that is being automatically read, vim will notify me that there have been changes made externally to the file since I have made my internal edits. I am looking for a way to merge the external changes with the internal ones. Feb 24, 2016 at 16:59
  • 5
    Are you looking for :h :DiffOrig? Feb 24, 2016 at 20:29
  • @ChristianBrabandt, it's never too late, right? This was a great answer. I didn't know that this existed. Apr 28, 2016 at 21:07

1 Answer 1

3

To elaborate on the Christian Brabandt's comment, here is a decomposition of the DiffOrig command:

Full command:

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

Decomposition:

command DiffOrig        " create a new command named DiffOrig
vert new                " open a new buffer splitted vertically
| set bt=nofile         " set buffer type to scratch 
| r #                   " read the alternate buffer (opened file to diff)
| 0d_                   " delete the first line
| diffthis              " diff the current buffer
| wincmd p              " move cursor to opened file 
| diffthis              " diff the current buffer

An alternate version use the ++edit option for the read, it keeps the options values as if editing a file.

References:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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