Use vimdiff to see the difference between multiple files

I was just reminded of the Unix/Linux vimdiff command that lets you visually see the differences between multiple files. I normally just use vimdiff to look at the differences between two files, but I know it can also work with three files.

A quick vimdiff example with two files

To demonstrate how vimdiff works, here's a quick example. Suppose you have one file named getty1 that has these contents:

Four score and seven years ago
our fathers brought forth,
upon this continent,
a new nation,
conceived in Liberty,
and dedicated to the proposition
that all men are created equal.

and then you have another file named getty2 that has these contents:

Four score and seven years ago   (just say "87"?)
our fathers brought forth,
upon this continent,
a new nation,
conceived in Liberty,
and dedicated to the proposition  (paradigm?)
that all men are created equal.

and you want to visually see the differences between these files. Just issue this vimdiff command:

vimdiff getty1 getty2

and then you'll be presented with this view:

vimdiff - Visually see the differences between multiple files with vimdiff

I intentionally kept these files short so they would fit here, but as you can see, vimdiff shows the file differences in the pink and red colors. The pink lines are different between the files, and the red shows the actual differences.

More on vimdiff as a file difference viewer

One cool thing about vimdiff is that when you scroll through much larger files, vimdiff scrolls both files at the same time, just as you'd expect with any file difference viewer.

Another cool thing about vimdiff is that you can edit the files that are being displayed. Not only that, but if you're editing one file to make it look more like the other file, vimdiff dynamically updates its display, so the red and pink highlights change as you type.

A final cool thing about vimdiff is under the covers ... if you use the Linux ls command to look at the vim* files in your /usr/bin directory, you might see something like this:

-rwxr-xr-x  1 root  wheel  2819728 Feb 10 20:54 /usr/bin/vim
lrwxr-xr-x  1 root  wheel        3 Apr 28 18:17 /usr/bin/vimdiff -> vim

This shows that vimdiff is really ... drumroll ... an alias for the vim command. That's right, it's the exact same vim command you know and love. It's just smart enough to recognize that when you invoke it using "vimdiff" instead of "vim" that you want to use this file difference viewing capability.

I hope this brief "Using vimdiff to see the difference between two or more files" tip has been helpful. The next time you need a file difference viewer, give vimdiff a try.