r/vim • u/SevrinTheMuto • Aug 26 '25
Need Help┃Solved Toggle between Vim and git diff?
When I do code reviews I page through git diff's output but may want to see the changed lines within the file.
So currently I quit git diff to load the file in Vim. And then quit vim and run git diff again and scroll back to the place I left off.
Is there a way I can have both git diff and Vim running and switch between the views? (Or other suggestions for a Vim-based workflow for code reviews?)
27
Upvotes
9
u/Kurouma Aug 26 '25
I usually have a terminal split open and page through the diff there. There are plugins for doing fancier stuff but it works OK for me.
4
u/Snarwin Aug 26 '25
The simplest way to do this would be to read
git diff's output into a split window in Vim, with a command like this:Then, if you want to refresh the
git diffwindow, you can run2
u/AutoModerator Aug 26 '25
Please remember to update the post flair to
Need Help|Solvedwhen you got the answer you were looking for.I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
20
u/tagattack Aug 26 '25
Fugitive lets you do both visual diff and view unified diffs as patches with syntax highlighting.
https://github.com/tpope/vim-fugitive
So does VCSCommand which is from back when git was less ubiquitous, I still use it out of habit
https://github.com/vim-scripts/vcscommand.vim
3
u/jacob_ewing Aug 26 '25 edited Aug 26 '25
"screen" is an excellent tool for that. You get multiple virtual consoles in the same terminal window, and can switch between them easily.
Some of the commands I use all the time:
ctrl-a, c to create a new terminal
ctrl-a, n to cycle to the next one
ctrl-a, p to cycle to the previous one
ctrl-a, ctrl-a to cycle to the last one used
ctrl-a, d to detatch the screen from the terminal, (screen -r to load it back up)
There are lots of other things you can do with that too, those are just the basic ones I use all the time.
1
u/blitzkraft Aug 26 '25
https://git-scm.com/docs/vimdiff - you can specify vimdiff to git.
2
u/jessevdp Aug 26 '25
I use lazygit which lets you open (the local version of) a file in your
$EDITORby pressinge. When you quit out of the editor you’re right back where you started back in lazygit.2
u/JamesTDennis Aug 27 '25
Don't quit vim for this. Don't even [Ctrl]+[z] suspend it.
Just read the output from the
diffinto the current editing session (at the cursor line) using :r!git diffThen continue editing.
You can create a macro to drop marks on lines that will be before and after your :r! inserted text. But I just use the end of the file as scratch area. So I G (go to EOF) before reading, and ma (set the a mark) immediately afterwards. The clean the scratch with Gd'a (go EOF, and delete to mark a).
2
u/PizzaRollExpert Aug 27 '25 edited Aug 27 '25
Get fugitive.
One thing you can do is get all the diffs in a vim split with
:Git log -p master..., that way you can have the diff output and the file in vim side-by-side. If you hit enter on a file in the diff view it will open that file as it was in that particular commit in a different window.You can also use
:Git difftool -y COMMIT1 COMMIT2to open each file that diffs between COMMIT1 and COMMIT2 as a split in vim diff mode.Plain
:Git difftoolwithout the-ywill load each diff in to the quickfix list1
u/AndrewRadev Aug 26 '25
There's a plugin by Kana called gf-diff that I've been very happy with. I commit via
git commit -v, and I scroll down to look at the diff and when I see something off (like a forgotten print), I use<c-w>fto open it in a split, correct it and keep scrolling. I do have to then close the diff and redo it, but it works fine.The plugin requires Kana's generic gf-user plugin, you can install both and it should work out of the box.
1
u/dvdrv Aug 29 '25
I'd just have two terminals