LinuxCommandLibrary

vimdiff

Visually compare and merge file differences

TLDR

Open two files and show the differences

$ vimdiff [path/to/file1] [path/to/file2]
copy

Move the cursor to the window on the left|right
$ <Ctrl w>[<h>|<l>]
copy

Jump to the previous difference
$ <[><c>
copy

Jump to the next difference
$ <]><c>
copy

Copy the highlighted difference from the other window to the current window
$ <d><o>
copy

Copy the highlighted difference from the current window to the other window
$ <d><p>
copy

Update all highlights and folds
$ <:>diffupdate
copy

Toggle the highlighted code fold
$ <z><a>
copy

SYNOPSIS

vimdiff [options] file1 file2 [file3 [file4]]

PARAMETERS

file1 file2 [file3 [file4]]
    
The paths to the files you wish to compare. vimdiff supports comparing two, three, or four files simultaneously.

-O
    
Opens windows vertically, splitting them side-by-side. This is often the default behavior for vimdiff when comparing two files.

-o
    
Opens windows horizontally, splitting them one above the other.

-R
    
Opens all files in read-only mode, preventing accidental modifications while reviewing differences.

-c {command}
    
Executes the specified {command} after the first file has been loaded. This is useful for setting specific Vim options or executing functions at startup.

--clean
    
Starts Vim in a 'clean' mode, ignoring the user's .vimrc and plugins. Useful for troubleshooting or ensuring a consistent diff view across different environments.

--diff
    
This flag explicitly tells Vim to start in diff mode. vimdiff is typically a script wrapper that automatically passes this flag to the underlying vim executable.

DESCRIPTION

vimdiff is a powerful and interactive tool built upon the Vim text editor, specifically designed for comparing and merging differences between two, three, or even four files. It provides a visual representation of file disparities, highlighting lines that have been added, deleted, or changed. Unlike simple command-line `diff`, vimdiff allows users to navigate between differences, directly edit files, and interactively transfer changes (merge) from one buffer to another using intuitive commands like `:diffput` and `:diffget`. It typically opens files side-by-side or stacked, automatically scrolling them together to maintain alignment. This makes vimdiff an indispensable tool for tasks such as code review, resolving merge conflicts in version control systems, and efficiently understanding modifications between file versions within a highly editable environment.

CAVEATS

  • vimdiff requires Vim to be compiled with the +diff feature enabled. Most modern Vim installations include this by default.
  • For very large files or an extremely high number of differences, vimdiff can consume significant system resources (CPU and memory), potentially leading to slower performance.
  • The comparison is primarily line-based. While it highlights character-level differences within a line, the primary unit of change detection is the line itself.
  • Merging complex changes, especially in three or four-way diffs, requires careful attention and understanding of the file contents to avoid unintended data loss or incorrect merges.

INTERACTIVE DIFF COMMANDS

vimdiff offers several crucial commands for navigating and merging differences within the editor:
- dp or :diffput: Copies the current difference from the active buffer to the corresponding buffer.
- do or :diffget: Retrieves the current difference from the corresponding buffer into the active buffer.
- ]c: Jumps to the next difference (change) block.
- [c: Jumps to the previous difference (change) block.
- zo: Opens (unfolds) a folded difference block.
- zc: Closes (folds) a difference block.
- :diffupdate: Re-scans the files for differences and updates the display, useful if one of the files has been externally modified.

THREE-WAY AND FOUR-WAY DIFFS

While commonly used for two files, vimdiff fully supports three-way and four-way comparisons, which are particularly useful for resolving complex merge conflicts where a common ancestor file might be involved. This allows for a more informed merge decision by visualizing the changes from a common base.

HISTORY

The diff mode, which vimdiff leverages, was introduced in Vim version 6.0, released in September 2001. Prior to this, users typically relied on external `diff` tools and manually applied changes within Vim. The integration of a native, interactive diff and merge capability significantly enhanced Vim's utility for developers and system administrators, making it a robust solution for managing code versions and resolving conflicts directly within the editor environment. Its development was a natural progression to provide a more visual and editable alternative to traditional command-line diff utilities.

SEE ALSO

diff(1), patch(1), vim(1), meld(1), kdiff3(1)

Copied to clipboard