What’s the Difference?

Changes between different versions of a file are often subtle, and a tool that lets you view precise differences at a glance could save hours of work. Vim integrates the well known Unix diff command into a very sophisticated visualization interface invoked through its vimdiff command.

There are two equivalent ways to invoke this feature: as a standalone command and as an option to Vim:

$vimdiff old_file new_file
$vim -d old_file new_file

Typically, the first file to be compared is an old version of a file, and the second is a newer version, but that is by convention only. Indeed, it’s possible to make a case for reversing the order.

Figure 15-5 shows an example of vimdiff output. Because of limited real estate, we’ve squeezed the width and turned off Vim’s wrap option to allow illustration of the differences.

vimdiff results
Figure 15-5. vimdiff results

Though the figure does not convey the full impact of the visual content (particularly because colors are reduced to gray), it shows some key characteristic behaviors:

  • On line 4, you can see a dark block on the left line that isn’t on the right line. This is a highlighted word indicating a difference between the two lines. Similarly, on line 32, the righthand line contains a highlighted word that is not on the left.

  • On line 11 of both sides, Vim has created a 15-line fold. These 15 lines in both files are identical, so Vim folds them ...

Get Learning the vi and Vim Editors, 7th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.