How to Compare and Merge Files in Linux Terminal Using vimdiff

When managing configuration files, resolving Git merge conflicts, or auditing source code alterations in a Linux server environment without a graphical desktop, text-based diff tools are indispensable. While standard diff or sdiff print differences sequentially or in static columns, vimdiff provides an interactive, full-screen side-by-side editing interface powered by Vim. With syntax highlighting, real-time folding of identical code blocks, and dedicated merge commands, vimdiff streamlines complex multi-file reconciliations directly from the shell.

Launching vimdiff from the Command Line

vimdiff is typically bundled with standard Vim installations (such as the vim-enhanced or vim package on Debian, Ubuntu, CentOS, Fedora, and Arch). You can invoke vimdiff directly or via the -d flag in Vim:

# Basic syntax for two files
vimdiff file1.conf file2.conf

# Equivalent standard Vim command
vim -d file1.conf file2.conf

# Comparing three files simultaneously
vimdiff base.py local.py remote.py

Upon launch, your terminal window splits vertically into two (or three) synchronized viewport panes. Line numbers align automatically, identical regions collapse into collapsible folds, and divergent text is colour-coded according to your current terminal colour palette.

Navigating Between Panes and Differences

Navigating effectively in vimdiff requires mastering a handful of specialized keyboard shortcuts:

  • Switching between split windows: Press Ctrl + w followed by w to cycle through active panes, or use Ctrl + w followed by directional arrow keys or Vim motion keys (h, j, k, l).
  • Jumping to the next difference: Press ]c in Normal mode to jump your cursor forward to the next changed block.
  • Jumping to the previous difference: Press [c in Normal mode to retreat backwards to the previous changed block.
  • Expanding or collapsing folds: By default, identical lines are hidden under fold bars (e.g. +-- 45 lines: ...). Place your cursor on a fold and press zo to open it, or zc to close it.

Interactive Merging: Pulling and Pushing Changes

The primary advantage of vimdiff over read-only tools like sdiff is its ability to push or pull changes across panes interactively without manually copy-pasting code:

  • Pulling changes (do / diff obtain): To pull the difference block from the adjacent pane into your active working file, position your cursor anywhere within the highlighted diff chunk and type:
    do
    (Short for “diff obtain”)
  • Pushing changes (dp / diff put): To overwrite the adjacent file’s code chunk with the block from your active pane, position your cursor on the chunk and type:
    dp
    (Short for “diff put”)
  • Updating diff calculations: If you manually edit a line inside a buffer and vimdiff does not immediately recalculate highlighting, type:
    :diffupdate

Configuring vimdiff as Your Default Git Merge Tool

You can configure Git to automatically open vimdiff whenever a merge conflict arises during a branch rebase or pull:

  1. Open your terminal and configure Git’s diff and merge tool parameters:
    git config --global diff.tool vimdiff
    git config --global merge.tool vimdiff
    git config --global mergetool.prompt false
  2. Whenever Git encounters conflicting commits, trigger the resolution environment by running:
    git mergetool
  3. Git will launch vimdiff displaying the LOCAL, BASE, and REMOTE versions alongside the merge output buffer at the bottom. Use do and dp to select desired blocks.

Saving Changes and Exiting Cleanly

Because multiple files are loaded in vimdiff, closing them individually can become tedious. Use the following standard commands from Normal mode:

  • Save and exit all files: Type :wqa (write, quit all) and press Enter to commit changes to all open files and exit.
  • Discard all alterations: If an unintended merge mistake occurred, abandon all modifications safely without saving by typing :qa! and pressing Enter.
  • Save active buffer only: Type :w to write only the active window’s file to disk.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.