Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #1343 - Diff the current buffer with another file

Created: September 26, 2006 0:23 Complexity: intermediate Author: Michael Hooreman Version: n/a Karma: 68/38 Imported from: Tip#1343

If you already run vim, and you want to do a vimdiff, you have by default 2 ways to do it:

- Running another vim with command line vimdiff

- Opening a new window [C-w s] and then making a lot of commands in the two buffers


This tip provides a function (MHooremanSetDiffEnviron) to set the "diff" environment in buffer. So, if you want to diff the right and the left window, you have to run :MHooremanSetDiffEnviron into the right and the left buffer.


To add MHooremanSetDiffEnviron, add the following in your vimrc file:


function! MHooremanSetDiffEnviron()

set diff 
set scrollbind 
set scrollopt=ver,jump,hor 
set nowrap 
set fdm=diff 

endfunction

command! MHooremanSetDiffEnviron call MHooremanSetDiffEnviron()

Comments

How annoyingly vain.

Anonymous , September 26, 2006 1:21


How does this differ from the 'diffthis' command?

vimtips--AT--crustynet.org.uk , September 26, 2006 3:47


Might I suggest the following commands instead:

sp <filename>
windo diffthis

For a bit of variety, you could even try :vs instead of :sp

LazyKnight , September 26, 2006 5:15


Getting into 'diff' mode is quite easy, as other comments have shown. Turning off 'diff' mode (without quitting VIM or deleting the buffers) is more complex. I use the following function:

" Switch off diff-mode of all currently open windows. command! DiffOff call DiffOff()

function! DiffOff()

windo set nodiff 
windo set noscrollbind 
" Reset, then restore the 'foldmethod' and 'foldcolumn' settings by 
" re-setting the filetype. This triggers the filetype-based folding to be 
" reactivated, and ( contrary to a simple :e!) works even on modified 
" buffers. 
windo set foldmethod=manual 
windo set foldcolumn=0 
windo unlet! b:did_ftplugin | let &filetype = &filetype 

endfunction

Ingo Karkat , September 26, 2006 6:13


as of vim 7 there is also :diffoff (:help :diffoff) which works very similar to the previous poster's function.

Anonymous , September 26, 2006 8:14


WTH is a MHooreman?

Anonymous , September 26, 2006 9:05


"WTH is a MHooreman": I prefix all my funcs by MHooreman to avoid redefinig not personnal functions

Anonymous , September 27, 2006 4:57


I think the easiest way to diff a currently open file with another is the ":diffsplit" command. It loads the file and sets all the right options. Personally I like to use ":vert diffsplit".

Anonymous , September 27, 2006 22:59


Advertisement