2

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.

1 Answer 1

0

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 :diffthis on 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...

4
  • The problem with :diffpatch is that it doesn't work if there are multiple diffs in the same file, which is likely to be the case for the result of git diff.
    – D. Ben Knoble
    Commented Jan 26, 2022 at 14:09
  • @D.BenKnoble good point. Although I likely suspect OP doesn’t even want to use git diff without a specific filepath for diff creation in the first place… Commented Jan 26, 2022 at 18:28
  • Given that that is precisely the premise of the question, I would not assume that…
    – D. Ben Knoble
    Commented Jan 26, 2022 at 20:58
  • @D.BenKnoble when you’re right, you’re right 😂… literally in the title… hard to pull myself out of the context of achieving the solution (the vimdiff window) and back to the ask! When the question said patch I didn’t think it implied a git diff. It no longer says patch tho. Commented Jan 27, 2022 at 4:32

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.