Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 17 down vote accepted

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..

share|improve this answer
    
Is there a way to lock scrolling between the two windows? –  Zaid Aug 30 '10 at 9:37
    
Yes, check out vimdoc.sourceforge.net/htmldoc/options.html#%27scrollbind%27 - with vimdiff it is the default. –  maxschlepzig Aug 30 '10 at 10:17
    
I usually use "diff file1 file2" on the command line. –  djangofan Oct 24 '12 at 21:01

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.

share|improve this answer
4  
:windo :diffthis can be used instead of the last three commands. –  Bernhard Oct 24 '12 at 11:12

You can also use the vimdiff command.

share|improve this answer
2  
Maxschlepzip already gave that answer 10 months ago. Please scan existing answers to see if you can throw in add anything useful rather than duplicating content. –  Caleb Jun 28 '11 at 21:49
    
@Caleb Thanks! I will scan, the answers better for the next time. –  Ákos Kovács Jun 29 '11 at 17:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.