开发者

How can I view multiple git diffs side by side in vim

I'd like to be able to run a comman开发者_运维百科d that opens up a git diff in vim, with a tab for each file in the diff set.

So if for example I've changed files foo.txt and bar.txt in my working tree and I ran the command I would see vim open with two tabs. The first tab would contain a side-by-side diff between foo.txt in my working tree and foo.txt in the repository, and the second tab would contain a side-by-side diff for bar.txt.

Anyone got any ideas?


Adapted Benjamin Bannier + Dave Kirby's answer above for fugitive users.

Because I use fugitive.vim, I adapted the above for my most frequent use case, looking at the diff between the last 2 commits:

vim -p $(git diff --name-only HEAD~1 HEAD) -c "tabdo :Gdiff HEAD~1"

Loading all the changes into tabs is so much better than going through them sequentially with git difftool.


The way I would do this (though it isn't a single command)

  1. Open files with changes in new vim tabs:

    vim -p $(git diff --name-only)

  2. For every buffer get the diff to your current HEAD with the vcscommand vim plugin

    :VCSVimDiff

This gives a nice view of the difference, though not in patch form.

For anything else I would stick to git diff.

EDIT

Like Dave writes below, steps 1 and 2 can be combined by using

vim -p $(git diff --name-only) -c "tabdo VCSVimDiff"


Although it doesn't do exactly what you want, git difftool is probably your best bet. The out of the box behavior of 'git difftool --tool=vimdiff --no-prompt HEAD' is to launch vimdiff sequentially for each file in the working dir with changes.


I came up with the following script to achieve the behaviour git difftoll + vimdiff provides, but using tabs:

#!/bin/bash

commit1=$1
commit2=$2

files=`git diff ${commit1} ${commit2} --name-only`

cmd="set nosplitright | set nobackup | set noswapfile"

EXE="| silent exe"

for f in $files; do
    if [ -z "$commit2" ]; then
        cmd+="$EXE ':tabnew $f'"
    else
        cmd+="$EXE ':tabnew | f <${commit2}>/${f} | setlocal buftype=nofile | 0read ! git show ${commit2}:$f'"
    fi
    cmd+="$EXE ':0 | filetype detect'"
    cmd+="$EXE ':vnew | f <${commit1}>/${f} | setlocal buftype=nofile | 0read ! git show ${commit1}:$f'"
    cmd+="$EXE ':0 | filetype detect'"
    cmd+="$EXE ':windo diffth'"
done
cmd+="$EXE 'bd 1 | tabfirst'"

vim -c "$cmd"

Call it like this: vimgd <commit1> <commit2>


This simple plugin has worked for me: TabMultiDiff. It basically creates a diff tab for each pair of files passed to vim. The tab is named after the second file in each pair.

Note it is not plugin manager compatible, so you need to manually install it by copying tab-multi-diff.vim to .vim/plugin.

Here is a screenshot of comparing two pairs of simple files (aaa/aab and aac/aad). Note that I also use vim-scripts/diffchar.vim which is why individual characters are highlighted.

How can I view multiple git diffs side by side in vim

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜