Thomas shares makes


(dir)VimDiff as GIT difftool

diff screenshot

Tig blame + VIM showing all files of a GIT commit in diff-tabs using dirvimdiff

I configured GIT's difftool command to show the changed files as diff-split file tabs I can easily cycle through by pressing gt.

This makes for a good terminal-based meld/beyond compare/... alternative.

Combined with Tig, it allows to quickly inspect a GIT commit's modifications in file context, let's take a look at the quick and simple steps to set it up.

The history of DirVimDiff

After 12+ years of perforce on the job, I get to use GIT on the main codebase again. Back then, I adapted a script called svnvimdiff to show GIT commits. I extended it to show an entire commit in a VIM window with every file in a tab. I called it Gitvimdiff, but actually it doesn't work all that great, so these days I opt to use my dirvimdiff as git difftool. Also, I once wrote a python script to do the same with perforce changelists.

Dirvimdiff is a quite simple hack at the time of writing, it converts the output of diff to a VIM-script that opens tabs with diff-splits of changed files. Then it launches Vim.

Setting up dirvimdiff / vimdiff in GIT

First, download dirvimdiff from my github scriptbox repository, and put it somewhere in your shell search $PATH.

Next, add these chunks to your ~/.gitconfig file:

    tool = vimdiff
[difftool "dirvimdiff"]
    cmd = dirvimdiff $LOCAL $REMOTE
    vimdiff = difftool --dir-diff --no-symlinks --tool=dirvimdiff

Now you can use git vimdiff next to git difftool:

thomas@roper % git difftool HEAD~1
(triggers tool in [diff] -> launch vimdiff one by one)
thomas@roper % git vimdiff HEAD~1
(triggers the [alias] -> launch dirvimdiff, one VIM with tabs)
diff screenshot

VIM showing all files of a GIT commit in diff-tabs using dirvimdiff

Configuring Tig

Tig is an ncurses-based text-mode interface for git. It functions mainly as a GIT repository browser, but can also assist in staging changes for commit at chunk level and act as a pager for output from various GIT commands.

diff screenshot

Walking through GIT commits using Tig blame. Hit F5 to launch dirvimdiff.

To use the dirvimdiff based difftool in tig, you can add this to your ~/.tigrc:

bind status <F4> !git difftool -y %(commit) %(file)
bind generic <F5> !git vimdiff %(commit)~1 %(commit)

Liked something? Worked on something similar? Let me know what you think on Mastodon!
You can use your Mastodon account to reply to this post.

Reply to post

You can respond to this post with an account on the Fediverse or Mastodon. Since Mastodon is decentralized, you can use your existing account or create your account on a server of your choice.

Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.

Learn how @carlschwan wrote the code that loads Mastodon posts into this webpage here.

Follow me on Mastodon!