124

Scenario: I have opened Vim and pasted some text. I open a second tab with :tabe and paste some other text in there.

Goal: I would like a third tab with a output equivalent to writing both texts to files and opening them with vimdiff.

The closest I can find is "diff the current buffer against a file", but not diffing two open but unsaved buffers.

3
  • 1
    what's the tabe command?
    – hhafez
    Commented Jul 8, 2009 at 1:36
  • 1
    @hhafez - open a new buffer in a new tab
    – Rook
    Commented Jul 8, 2009 at 1:40
  • 3
    It's short for :tabedit, aka :tabnew.
    – TSomKes
    Commented Jul 8, 2009 at 1:56

4 Answers 4

194

I suggest opening the second file in the same tab instead of a new one.

Here's what I usually do:

:edit file1
:diffthis
:vnew
:edit file2
:diffthis

The :vnew command splits the current view vertically so you can open the second file there. The :diffthis (or short: :difft) command is then applied to each view.

2
  • 1
    Fwiw, MacVim doesn't seem to have this by default, but I think you should be able to go to the terminal and type vimdiff file1.txt file2.txt and knock yourself out.
    – ruffin
    Commented Mar 23, 2012 at 14:55
  • 3
    This is awesometastic. The edit fileN parts can be replaced with just pasting from the buffer, which lets you diff two chunks of text without pasting each into a tmp file (something that meld lets you do, but visual diff tools on the Mac are lacking in). It worked in MacVim for me out of the box.
    – yshavit
    Commented Nov 15, 2013 at 16:23
38

I would suggest trying :diffthis or :diffsplit

4
  • 10
    :vert diffsplit makes for a more traditional diff-view than without :vert
    – ephemient
    Commented Jul 8, 2009 at 20:18
  • 7
    You can :set diffopt+=vertical to make :vert the default.
    – graywh
    Commented Oct 30, 2009 at 22:05
  • 3
    Also, to scroll two windows simultaneously: :set scrollbind in both. Commented Dec 28, 2012 at 23:08
  • Does not work if I have to tabs, like asked in the question. If I do :diffsplit I get two connected buffers.
    – erik
    Commented Apr 28, 2015 at 13:29
27

When you have two files opened in vertical splitt, run

:windo diffthis

2
  • 9
    use :diffoff to turn off diff mode
    – A B
    Commented May 24, 2013 at 20:10
  • 2
    But how do I change from two tabs to a vertical split?
    – erik
    Commented Apr 28, 2015 at 13:30
4

The content of all tabs are inside the buffers. Look at the buffers:

:buffers

Find the right number for the content which should be diffed with your current tab content.

Open the buffer inside your current tab (f.e. buffer number 4)

:sb 4

Or do for vertical view:

:vertical sb 4

Then you can simple diff the content with

:windo diffthis

If you finished diff analysis you can input:

:windo diffoff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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