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?
2 Answers
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)
-
2You can use
diff -u "%" -
sincediff
understands a dash as meaning read from stdin– roaimaJun 11, 2015 at 17:23
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.