Vim Tips Wiki

Diff the current buffer with another file

1,624pages on
this wiki
Add New Page
Talk0 Share
Tip 1343 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Michael Hooreman · version n/a

This tip is deprecated for the following reasons:

As suggested in the comments, this functionality is built-in to Vim, with the :diffthis, :diffsplit, :diffoff, etc. commands

If you are already in Vim, there are two methods to do a vimdiff:

  • Run another Vim with command line vimdiff.
  • Open a new window (C-w s) and then enter appropriate commands in the two buffers.

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

Add the following to your vimrc file:

function! SetDiffEnviron()
  set diff
  set scrollbind
  set scrollopt=ver,jump,hor
  set nowrap
  set fdm=diff
:command! SetDiffEnviron call SetDiffEnviron()



How does this differ from the :diffthis command?

Might I suggest the following commands instead:

:sp <filename>
:windo diffthis

For variety, you could try :vs instead of :sp

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

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

As of Vim 7, there is also :diffoff which is very similar to the DiffOff function above.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.