Quick diff for two pieces of text in vim

Sometimes you need to see the difference between two pieces of console output. When I research, this can be two stacktraces, but also /proc//maps and smaps output; really anything. Of course, there’s diff, but the diff output is not very visual. Also, diff doesn’t do diffing between more than two files.

This can be done reasonably simple in vim. Here’s how to do that:
1. start vi; vi
2. do a vertical split using a new buffer; :vnew
3. open the first (left side) file; :r path/file or goto insert mode (esc i) and paste text.
4. goto the second window: ctrl+w ctrl+w
5. open the second (right side) file; :r path/file or goto insert mode (esc i) and paste text.
6. diff the two windows; :windo diffthis
7. turn diff mode off; :windo diffoff

You can also expand your diffing to three windows:
1. goto the rightside; ctrl+w l
2. change new window placement to the right side; :set splitright
3. do another virtual split; :vnew
4. open another file or paste text
5. diff again; : windo diffthis

I wrote this down for myself, but hopefully this helps other people too.

  1. Nice. Guess I have been spoiled with iTerm2 for the Mac – I often resort to splitting the screen vertically with another session to do just that… Always good to have another method.

  2. Thank you for reading Stephen. Could you elaborate on how you diff with iTerm2? I use iTerm2 too, but I am not aware of a true diff function (with which I mean that it indicates where the differences are).

  3. Ivan Saez said:

    Nice feature of vim! I’ve been in need of comparing three files. This vim feature will come in handy. Thanks for sharing.
    By the way: for comparing two files I use ‘diff -y’. The -y is the side-by-side option of diff.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: