[ Home page ]


cdiff: coloured diff

This is a simple bash alias to give colour-coded differences between two files. I originally wrote it for viewing the differences between XML files where it is desirable to be able to find the changes quickly but to view a large amount of context either side, but I now use it for viewing differences between all kinds of files.

(For the benefit of U.S. users of search engines, I will now point out that cdiff produces colored diffs. It uses the diff utility and has colors)

Usage

Simply source cdiff into your shell. This is best achieved by copying it into .bashrc . The output of cdiff can be viewed by piping into less -R or less -r. The -R is needed to handle the colour codes.

Get cdiff or cut and paste it:

#
#  cdiff: coloured diff format
#
#  Paul Warren <pdw@ex-parrot.com> 
#  12/01/2001
#

#  colour for added lines (bright yellow)
diffnew=`tput setf 6``tput bold`

#  colour for removed lines (bright red)
diffold=`tput setf 4``tput bold`

#  colour for unchanged lines (bright black, aka dark grey)
diffsame=`tput setf 0``tput bold`

#  reset - original pair, unset all attributes
reset=`tput op``tput sgr0`

alias cdiff="diff --changed-group-format='%<%>'  \
	--new-group-format='%>' \
	--old-group-format='%<' \
	--new-line-format='${diffnew}+ %l
$reset' \
	--old-line-format='${diffold}- %l
$reset' \
	--unchanged-line-format='${diffsame}  %l${reset}
' "

		

Paul Warren   23:14:06 09-Feb-2004