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]

PARAMETERS

file1
    The first file to compare.

file2
    The second file to compare.

file3
    An optional third file to compare. Results in a three-way diff.

-d[N]
    Open Vim in diff mode with N windows (default is 2). If N is not specified, it defaults to 2.

-f
    Foreground: don't fork when starting as a GUI.

-o[N]
    Open N windows, one for each file (default is 2). If N is not specified, it defaults to 2.

-R
    Readonly mode. Disables writing to files.

--version
    Display version information and exit.

+[num]
    Start editing at line 'num' in each file.

+/pattern
    Start editing at the first occurrence of 'pattern' in each file.

DESCRIPTION

The vimdiff command launches the Vim text editor in a special 'diff mode', enabling users to visually compare two or three files side-by-side. This is exceptionally useful for identifying differences between versions of a file, merging changes from one file into another, and resolving conflicts after version control operations (e.g., after a Git merge). The editor highlights differences using colors and folds unchanged regions, providing a clear and concise view of the modifications. Within vimdiff, standard Vim editing commands are available, allowing users to navigate, edit, and merge changes directly in the interface. Changes made within vimdiff persist to the edited files upon saving. The user can customize the appearance and behavior of the diff mode through Vim configuration files (e.g., .vimrc). The vimdiff command is a powerful tool for developers, system administrators, and anyone who needs to compare and merge text-based files effectively.

CAVEATS

vimdiff relies on Vim being properly installed and configured. It can be challenging for users unfamiliar with Vim's editing model. Large files can take a significant amount of time to process and display. Binary files can cause unexpected behavior.

MERGING CHANGES

Within vimdiff, you can merge changes from one buffer to another using commands like ':diffget' (or 'dg') to pull changes and ':diffput' (or 'dp') to push changes. These commands selectively apply changes from the other file into the current file.

You can also use normal Vim motions to copy and paste between diff windows.

NAVIGATION

Use ']c' and '[c' to jump to the next and previous change respectively.
Use 'zo' to open a folded block of unchanged text and 'zc' to close the block.

CUSTOMIZATION

vimdiff's appearance and behavior can be heavily customized using your ~/.vimrc file. You can change the color scheme used for highlighting differences, set custom diff options, and define macros to streamline common merging tasks.

HISTORY

vimdiff is built upon the Vim text editor, an enhanced clone of the vi editor. The 'diff mode' functionality was added to Vim to provide a more user-friendly and interactive way to compare and merge files than traditional command-line diff tools. Its widespread use is in software development, system administration, and documentation, where tracking and merging changes are critical workflows.

SEE ALSO

diff(1), patch(1), vim(1), gvim(1)

Copied to clipboard