20

Is there a way to save the changes I made to my vim buffer as a patch file for the original file, without saving it as a separate file and using diff?

1
  • 2
    There's a new SE beta site for Vi and Vim, you might want to check it out.
    – Roflo
    Jun 11, 2015 at 18:22

2 Answers 2

21

It's possible to do this without a plugin using the w command, so the buffer contents can be used in a shell command:

:w !diff -au "%" - > changes.patch

(% is substituted with the path of the file being edited, - reads the buffer from stdin)

1
  • 2
    You can use diff -u "%" - since diff understands a dash as meaning read from stdin
    – roaima
    Jun 11, 2015 at 17:23
1

You could also filter the buffer through diff using the %! syntax:

:%! diff -au "%" -

This will fill the buffer with the diff, rather than writing it to disk; you may want to set readonly so you don't clobber the original by accident (OTOH, that shouldn't be a serious problem because you have the file under version control, right?). Of course, it helps to have the inverse command:

:%! patch -o - "%"

That transforms your diff back into the modified file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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