Wikia

Vim Tips Wiki

Diff current buffer and the original file

Talk0
1,610pages on
this wiki
Revision as of 06:04, July 13, 2012 by JohnBot (Talk | contribs)

Tip 1030 Printable Monobook Previous Next

created 2005 · complexity intermediate · author Thomas Arendsen Hein · version 6.0


Here is a function and 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()

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 % -

Here is a version for git in Linux:

function! s:DiffWithGITCheckedOut()
  let filetype=&ft
  diffthis
  vnew | exe "%!git diff " . expand("#:p:h") . "| patch -p 1 -Rs -o /dev/stdout"
  exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
  diffthis
endfunction
com! DiffGIT call s:DiffWithGITCheckedOut()

Maybe this tip was useful in 2005, but the vimrc_example.vim has evolved since then: see :help :DiffOrig --Tonymec 22:39, December 23, 2009 (UTC)

Around Wikia's network

Random Wiki