I have a diff file generated by
git diff HEAD~2 > alldiff.
Is it possible to view the diff for any one file using alldiff?
For example, somecmd alldiff file1withchanges will use alldiff to show changes in file1withchanges in vimdiff.
- 26,070
- 3
- 29
- 65
- 21
- 1
1 Answers
TIL about this: Is it possible to use vimdiff's side by side view with a regular diff file?
:help diffpatch is your answer.
You just need to say
:diffpatch alldiff
Other answer before I knew that^
I'm not normally one to recommend plugins, but I recommend using vim-fugitive. There is a command, :GDiffsplit, that does exactly what you want.
In the buffer you're interested in, simply say :GDiffsplit HEAD~2 and fugitive will do the rest:
- Get git to create the diff
- open a temp buffer with a copy of the file
- Apply the diff
- run
:diffthison the two buffers
Here is me trying to write that as a vimscript function
function! do_the_fugitive_thing(revision)
" Get git to create the diff
let my_file = expand("%")
let my_patch = "/tmp/ari.patch"
system("git diff ".a:revision." -- ".my_file." > ")
" open a temp buffer with a copy of the file
vsp temp_buffer
set filetype=nofile
execute "read ".my_file
" Apply the diff
system("patch < ".my_patch)
edit! % " reload buffer after patch application
" run :diffthis on the two buffers
bufdo diffthis
endfunction
Let me know if this works! Or if you don't know vimscript enough then don't worry about it and just use the plugin...
- 772
- 5
- 15
:diffpatchis that it doesn't work if there are multiple diffs in the same file, which is likely to be the case for the result ofgit diff. – D. Ben Knoble Jan 26 '22 at 14:09git diffwithout a specific filepath for diff creation in the first place… – Ari Sweedler Jan 26 '22 at 18:28