Vim Recipes ‣ Other Uses of Vim ‣ Viewing Differences Between Files
You have multiple versions of a file and want to see how they differ.
For example, if you're working on a document with somebody else, you want to see the differences between their version and yours.
Vim calls this feature diff, as per UNIX convention. You diff a file.
To diff from the command line invoke Vim as vimdiff, e.g. :vimdiff -o file1 file2. To diff from within Vim use :diffsplit file.
The screen is split horizontally, and each file is shown in its own window. The differences between them are highlighted. By default, a line that exists in one file but not in another are coloured blue; the line in the other buffer that should have held this line is coloured green, and is called a filler line. When a line exists in both buffers it is coloured purple. If characters inside the line differ they are highlighted in red. Identical lines have no highlighting. Identical runs of lines are folded.
To split the screen vertically use :vimdiff file1 file2 or :vert diffsplit file1 file2.
Diffs are traditionally used in programming to compare an old and new version of source code to see what has changed. As suggested above, though, they're invaluable when collaborating on a file with somebody else. For example, you write a file named grant-proposal.txt, then e-mail it to somebody else for input. They make their changes and e-mail it back. You now have two files: grant-proposal.txt and grant-proposal.changes.txt. You use :vimdiff grant-proposal.txt grant-proposal.changes.txt to see your collaborator's suggestions.
It's important to note, though, that diff only works with plain text files. It won't work correctly if you use proprietary, binary formats such as Microsoft Word's .doc. As a general rule, if Vim can view a file it can diff it.
The windows diff feature splits your screen so each pane scrolls in concert with one other. This means that if you scroll to line 20 in the bottom pane, the top pane will automatically scroll to that point, too. This makes comparing long files easier, but you can disable it with :set noscrollbind.
You can jump between the changes with [c to go to the previous change, and ]c for the next.