Comparing two files in Vim

Open the side by side view:

Ctrl+w v

Change between them:

Ctrl+w h or l

Checkout the vimdiff command, part of the vim package, if you want a diff-like view:

vimdiff file1.txt file2.txt

You can also open vim in split-screen mode, with the -O option:-

vim -O file1 [file2 ...]

To then turn on diff mode, you need to run the :diffthis command in each pane.

Another use-case scenario, is if you've already got one file open in vim, and you want to open and compare it against another. Then you can use the following vim commands:-

:vs otherfile (open otherfile in vertical split screen)
:diffthis (turn on diff mode in original file)
Ctrl+w l  (swap to newly opened file)
:diffthis (turn on diff mode in opened file)

You can then turn off diff mode in each pane with the vim command :diffoff.

EDIT
And the other standard one that hasn't been mentioned:-

vim -d file1 [file2 ...]

This is equivalent to calling vimdiff directly.


Or just open the first file in VIM, then :vert diffsplit file2 :vert makes it split the screen vertically.

diffsplit does a diff, and splits the files and scrolls locks them.

Tags:

Vim

Diff