History Report a problem
Article Edit this page Discussion

Diff the current buffer with another file

From Vim Tips Wiki

Jump to: navigation, search
 

Tip 1343Previous TipNext Tip

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


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
endfunction
:command! SetDiffEnviron call SetDiffEnviron()

[edit] References

[edit] Comments

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
endfunction

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


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Terminator Wiki
Legend of Zelda Wiki
Flash Gordon
Everquest II Wiki
Yo-Yo Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Diff the current buffer with another file"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation