Technology
 

Diff current buffer and the original file

From Vim Tips Wiki

Tip 1030 Previous Next created October 27, 2005 · complexity intermediate · author Thomas Arendsen Hein · version 6.0


Here is a function/command to see a diff between the currently edited file and its unmodified version in the filesystem. Just put this in your vimrc or in the plugin directory, open a file, make some modifications without saving them, and do :DiffSaved.

function! s:DiffWithSaved()
  let filetype=&ft
  diffthis
  vnew | r # | normal! 1Gdd
  diffthis
  exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
endfunction
com! DiffSaved call s:DiffWithSaved()

To get out of diff view you can use the :diffoff command.

Below is a similar function, adapted to mimic the 'cvs diff' command (cvs must be in the path):

function! s:DiffWithCVSCheckedOut()
  let filetype=&ft
  diffthis
  vnew | r !cvs up -pr BASE #
  1,6d
  diffthis
  exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
endfunction
com! DiffCVS call s:DiffWithCVSCheckedOut()

Same for svn instead of cvs:

function! s:DiffWithSVNCheckedOut()
  let filetype=&ft
  diffthis
  vnew | exe "%!svn cat " . expand("#:p:h")
  diffthis
  exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
endfunction
com! DiffSVN call s:DiffWithSVNCheckedOut()

[edit] Comments

The following suggestion should work, but will only show you the output of the "diff" command on your system. It will NOT use Vim's excellent diff mode, where you can see side-by-side highlighted and folded differences between text in two different windows.

Super-simple non-function command version:

:w !diff % -