Side-by-side Diffs in a Terminal

Today I set up side by side colored diffs for Mercurial. This may not seem like a big deal, but there were a few problems I encountered:

  • Most solutions online point to using the extdiff extension – this doesn’t work too great with hg qdiff.
  • Side by side diffs require more screen real estate, but when I’m not viewing a diff, I want my terminal window to stay in its corner on my screen, at its usual 92×35.

My final solution involves using aliases, Xterm control sequences to resize my window, and cdiff.

Installing cdiff was easy using pip. Once that was done, I set up my aliases in ~/.hgrc:

[alias]
ddiff = diff
qqdiff = qdiff
diff = !printf '\e[9;1t'; $HG ddiff $@ | cdiff -s -w 0; printf '\e[9;0t'
qdiff = !printf '\e[9;1t'; $HG qqdiff $@ | cdiff -s -w 0; printf '\e[9;0t'

That first two lines “back up” the original diff and qdiff commands, then aliases them to use cdiff!

Before diffing, the aliases printf the Xterm control sequence to maximize the window, and then restore the window after diffing.

The -s flag makes cdiff do a side-by-side diff, and -w 0  makes it use all the available real estate.

That’s it! I’ve been using it all day and absolutely love it, so I thought I’d share.

Cheers!

Leave a Reply

Your email address will not be published.