Skip to content

Vim - Diff

Vim includes a powerful built-in diff utility, often referred to as vimdiff. It allows you to compare two or more files and view their differences side-by-side, with changes highlighted. This is extremely useful for code review, merging changes, or understanding modifications.

This chapter covers:

  • How to start Vim in diff mode.
  • Activating diff mode from within Vim.
  • Navigating between differences.
  • Applying changes from one file to another.

You can start Vim directly in diff mode from your terminal by providing two or more filenames:

vimdiff file1.txt file2.txt [file3.txt ...]

Alternatively, you can use the -d option:

vim -d file1.txt file2.txt

Vim will open with each file in a separate vertically split window. Differences will be highlighted:

  • Lines that are different between files will have their differing text highlighted.
  • Lines that exist in one file but not the other will be shown with a different background color.
  • Common lines will usually not be highlighted, or may have a subtle background. Folded sections might hide unchanged lines to focus on differences.

For example, text unique to one file might be highlighted in red, text unique to another in blue, and modified lines in purple. Filler lines (shown as ---) might appear where lines are missing in one file to maintain alignment.

Activating and Deactivating Diff Mode from Within Vim

Section titled “Activating and Deactivating Diff Mode from Within Vim”

If you are already in Vim and want to compare the current buffer with another file:

To open another file in a new horizontally split window and enter diff mode:

:diffsplit filename.txt

To open the other file in a new vertically split window (often preferred for diffing):

:vert diffsplit filename.txt

If you already have multiple windows open, you can put each one into diff mode by moving to that window and typing:

:diffthis

To turn off diff highlighting for the current window:

:diffoff

To turn it off for all windows in the current tab:

:diffoff!

Several options and commands help navigate diffs:

  • Switching Windows: Use Ctrl+w Ctrl+w to cycle through windows, or Ctrl+w <h/j/k/l> to move to a specific window.
  • Scroll Binding: By default, windows in diff mode are scroll-bound (:set scrollbind). Scrolling one window scrolls the others. You can toggle this with :set scrollbind and :set noscrollbind (or :set scb! to toggle).
  • Updating Diffs: If you make changes, Vim might not update the diff highlighting immediately. Force an update with:
  • :diffupdate
  • Jumping Between Changes:
    • ]c - Jump to the next difference.
    • [c - Jump to the previous difference.

When your cursor is on a highlighted difference, you can easily transfer changes from one buffer to another.

Getting Changes into the Current Window (dp or :diffput)

Section titled “Getting Changes into the Current Window (dp or :diffput)”

To take the changes from the other diffed buffer and apply them to the current buffer (at the current difference block), use:

dp

This is equivalent to :diffput (without arguments, it implies putting from the other buffer).

Putting Changes from Current Window to Another (do or :diffget)

Section titled “Putting Changes from Current Window to Another (do or :diffget)”

To take the changes from the current buffer and apply them to the other diffed buffer (at the current difference block), use:

do

This is equivalent to :diffget OtherBufferName. The do command (diff obtain) is a more convenient shortcut.

If you have more than two files diffed, you might need to specify the buffer name/number with :diffget <buf> or :diffput <buf>.

Practical Application: vimdiff is essential for reviewing code changes before committing, merging branches in version control (many Git tools can integrate Vim as a difftool/mergetool), or comparing configuration files to spot discrepancies.