Comparing buffers with vimdiff


Run time:

Vim’s diff mode allows us to easily compare the contents of two (or more) buffers. We can start Vim in diff mode using the vimdiff command, or if Vim is already running we can switch to diff mode using the :diffthis command. The beauty of the :diffthis command is that it works with unnamed buffers, whereas vimdiff can only work with files.


Using vimdiff to compare two files

We can call vimdiff from the command line giving it two or more filenames. vimdiff launches Vim, creates a window for each specified file, and highlights the differences between them.

In the video demonstration, one of the files that I wanted to compare was on my local filesystem, while the other file was in a textarea on a webpage. Before I could use vimdiff, I had to copy the text from that textarea into a local file. I did so using my system clipboard, then running the command:

pbpaste >

The pbpaste command is specific to os x, but similar commands are available on other systems.

We can now compare the two drafts by running:


I’m not keen on this workflow for a couple of reasons. I have no further use for the temporary file, so I’d have to clean up afterwards by removing it. Also, I usually have an instance of Vim open already, and I’d prefer a workflow that didn’t require me to open a fresh instance of Vim.

Using :diffthis to compare two buffers

The :diffthis command allows us to compare two (or more) buffers that are open in an existing Vim session. If we have two split windows containing buffers that we want to compare, then we can diff them by running:

:windo diffthis

We can turn diff mode off just as easily, by running:

:windo diffoff

The nice thing about this technique is that we can use an unnamed buffer. There’s no need to write the text to a temporary file. In the video demonstration, the buffer on the left corresponds to a file on my local filesystem, while the buffer on the right has no associated file. This technique would work just as well if neither buffer had been saved to disk, which makes it pretty flexible.

Getting around in diff mode

In diff mode, Vim provides two handy motions for getting around: [c and ]c jump back and forward between changes. If you want to resolve the differences between files, you can do so using the :diffget and :diffput commands. Check out episodes 32 and 33 for a detailed demonstration of how to use these commands.

Further reading


Browse similar content

Level-up your Vim


Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify


Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..