91

Is it possible to view two files side-by-side in Vim? If so, how can I set up my editor to do this, and is there a way to diff between the two files within Vim?

I am aware of the :next and :prev commands, but this is not what I'm after. It would really be nice to view the two files in tandem.

Zaid
  • 10,642

5 Answers5

75

Open the side by side view:

Ctrl+w v

Change between them:

Ctrl+w h or l

You can then open another file for comparison in one side by entering a command such as:

:e file2.txt

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

vimdiff file1.txt file2.txt
maxschlepzig
  • 57,532
  • 3
    Is there a way to lock scrolling between the two windows? – Zaid Aug 30 '10 at 09:37
  • 1
    Yes, check out http://vimdoc.sourceforge.net/htmldoc/options.html#%27scrollbind%27 - with vimdiff it is the default. – maxschlepzig Aug 30 '10 at 10:17
  • 1
    I usually use "diff file1 file2" on the command line. – djangofan Oct 24 '12 at 21:01
  • 3
    @Zaid Use vimdiff file1.txt file2.txt (see below) – Eduardo Cuomo Jun 19 '17 at 18:22
  • 1
    This does not answer the question. The question was how to compare two different files in vim. This answer describes how to split the view of a single file vertically. Which shows two views of the same file. This answer also suggests using vimdiff, which DOES provide a solution to comparing two files in a vim-like environment, however again, not what op was asking for. Question was "Is it possible to view two files side-by-side in Vim", not in vimdiff. – Dave Feb 16 '22 at 21:11
  • @Dave well, this answer assumes that the reader already knows how to open a file in Vim (e.g. using :e). With that knowledge it's pretty obvious that you can open the file you want to compare when navigating to the other site. Funny, how an answer that allegedly doesn't answer the question was accepted and currently is the highest voted one. – maxschlepzig Feb 17 '22 at 21:17
  • You might want to add to your answer, that one can open the windows using :split or :vsplit as well as per your steps (just for other options). In either case, once one has their two windows open in vim, run :diffthis in each window for a typical side by side diff. Use diffoff to disable diff mode (acts on the active window, so if only run once, the other window is still selected for diffing, so just :e file3.txt and then :diffthis to diff the original window with file1.txt to file3.txt). Using :diffthis activates scroll lock and between the windows and highlights differences. – Chris Jun 08 '23 at 00:25
  • Also worth noting for those less familiar with vim, what one is diffing is the loaded buffers. The buffers may not be the same as the file on disk, unless the buffer is loaded with a file from the disk and has had no edits made. If edits are made, to compare on disk files, one has to write the edited buffer to the file with :w in some form. To know you are looking at 'as per the file on disk' i suggest using vim -O file1 file2 – Chris Jun 08 '23 at 00:29
  • Just to add vimdiff file1 file2 or nvim -d file1 file2 are even better options. – Chris Jun 08 '23 at 00:41
69

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.

Alex Leach
  • 7,910
22

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.

Bart
  • 2,221
Dustin
  • 221
7

While it has already been answered how to start the diff, it's also important how to stop it in all windows. For completeness, I repeat the comment from @Bernhard.

Start and Stop Diff of two files opened in 2 Windows (works in both, vertical and horizontal split):

:windo diffthis
:windo diffoff

this can be shortened to either

:windo difft
:windo diffo

or

:windo difft
:diffo!

Be aware that opened windows for showing plugin content lead to issues. So close stuff like NERDtree, minibufexplorer++ etc before.

Custom commands: To ease up things you can add custom commands to your ~/.vimrc:

command! Difft [ClosePluginWindow |] windo diffthis
command! Diffo windo diffoff

with [ClosePluginWindow |] being optional to close plugin windows you usually use. For NERDtree e.g. this would be NERDTreeClose |.

Credits go to @cxw and @Jordi Freixa.

Wolfson
  • 175
0

For those of you using neovim:

nvim -d file1 file2 [file3 [file4]]

Reference: https://neovim.io/doc/user/diff.html