Monday, September 14, 2009

Diffing From Inside Vim

To compare two files without resorting to launching vidiff from the command-line, you can do the following.

1) open one of the two files in a standard buffer
2) type in :vert diffsplit filename

Put the name of the file you'll be comparing in place of filename. Prefixing the command with vert gives you a vertical split. If you'd prefer a horizontal split, simply omit the vert.


Anonymous said...

I use the following mapping to make it quick and easy. This diffs from version saved to disk; usefull when I want to make sure I have not made any errant keystrokes before saving.

"DiffOrig Mapping.
function! DiffOrig()
if &diff
wincmd p | bdel | diffoff
vert new | set bt=nofile | r # | 0d_ | diffthis | wincmd p | diffthis
map -leader-do :call DiffOrig() -cr-

Unknown said...

A couple of useful (related) scripts:

DirDiff makes it easy to diff two directories with vim.

VCSCommand includes a VCSVimDiff command which will diff against a version of the current file in the VCS of your choice.

Unknown said...

If you have 2 buffers open, running ':diffthis' in each buffer will also produce the vimdiff effect. This is handy if you have data that has not been saved to a file.

Greg Whitescarver said...

After you close one of the buffers, the remaining document will still be in 'diff' mode, with folding, ugly highlighting, etc. :diffoff does the trick.

Miguel said...

Hi guys,

Running VIM under putty or other terminal window, if you accidentally roll the mouse-wheel will cause the actual terminal buffer to be scrolled.
I found out this can be overridden by setting:

:set mouse=a

Which will make VIM mouse sensitive even if run within a terminal window.
Rolling the mouse-wheel will make the actual vim buffer to scroll.;)
Didn´t find a way to contribute to your blog so I am leaving this comment.


Miguel Mattos
+55 51 9198-9914