vimdiff is actually a specialized mode of Vim, not a separate program, and it’s designed to show you differences between files in a way that leverages Vim’s powerful editing capabilities.

Here’s how it works in practice. Let’s say you have two versions of a configuration file, nginx.conf.old and nginx.conf.new. You’d open them like this:

vimdiff nginx.conf.old nginx.conf.new

This splits your terminal into two (or more, if you add more files) vertical panes. Each pane shows one of the files. Lines that are identical appear in both panes, colored normally. Lines that have been added or deleted will appear in one pane but not the other, often highlighted with a distinct color. Lines that have been changed will appear in both panes, but the differing sections will be highlighted, usually in red.

The magic happens when you start navigating. Use ]c to jump to the next difference and [c to jump to the previous one. When you’re on a differing line, you’ll see the changes visually. You can edit either file directly. If you make a change in one file, vimdiff will automatically update the highlighting in the other pane to reflect the new difference.

To incorporate changes from one file into the other, you use Vim’s diff-specific commands. If you’re in the left pane (viewing nginx.conf.old) and want to pull a change from the right pane (viewing nginx.conf.new), you’d type :diffget or, more commonly, do. This will apply the difference from the other buffer to the current one. Conversely, if you want to push a change from the current buffer to the other, you’d use :diffput or dp. This is incredibly powerful because you’re not just seeing the differences; you’re actively merging them with familiar Vim keystrokes.

The underlying mechanism relies on Vim’s internal diff algorithm. When you open files with vimdiff, Vim computes a diff between them and stores this information. It then uses this diff to highlight the differences and to guide the do and dp commands. The highlighting is not just a visual overlay; it’s directly tied to the diff information. When you edit a line, Vim re-calculates the diff for that section and updates the display.

You can also use vimdiff to compare a file against a version in Git or another VCS. For example, to compare your current nginx.conf with the version staged for commit:

vimdiff nginx.conf <(git diff --cached nginx.conf)

This uses process substitution <(...) to feed the output of git diff as a temporary file to vimdiff. The concept extends to comparing against the committed version, or even branches.

The scroll binding is a feature that keeps the corresponding lines in the two panes synchronized as you scroll. This is enabled by default when you launch vimdiff and is crucial for maintaining context when looking at large files. You can toggle this behavior with :diffo scrollbind. Similarly, :diffo cursorbind will synchronize your cursor movement, so moving up or down in one pane moves you to the corresponding line in the other.

If you have more than two files, vimdiff will arrange them in a grid. For three files, it’s typically two vertical panes side-by-side, with the third file opening in a horizontal split above or below the first two, depending on your Vim configuration. Navigating differences across multiple files uses the same ]c and [c commands, cycling through the differences in all open buffers.

The true power of vimdiff lies in its ability to merge changes. When you resolve a difference, you can tell vimdiff that the lines are now identical. For example, after using do or dp, you might have a conflict. You can manually edit the line to a state you’re happy with, and then use :diffupdate to recalculate the diffs and remove the highlighting. This turns vimdiff from a passive viewer into an active, interactive merging tool.

A subtle but important aspect of vimdiff is how it handles whitespace differences. By default, it treats them as significant. If you want to ignore whitespace changes, you can use :set diffopt+=iwhite before starting the diff, or add it to your .vimrc. This will make lines that differ only in whitespace appear as identical, which is often desirable when reviewing code or configuration where indentation might change without affecting logic.

The next logical step after mastering file comparison is understanding how vimdiff interacts with more complex version control workflows, particularly when dealing with merge conflicts in Git.

Want structured learning?

Take the full Vim course →