Vim Tips Wiki
No edit summary
 
(First clean up)
Line 9: Line 9:
 
|rating=61/22
 
|rating=61/22
 
|text=
 
|text=
RichiH asked on #vim for a way to see a diff between the currently edited file and its unmodified version in the filesystem, here is a function/command to do this. Just put this in your .vimrc or in the plugins directory, open a file, make some modifications without saving them and do :Diff (or choose a better name if you like)
+
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 plugins directory, open a file, make some modifications without saving them and do :DiffWithSaved (or choose a better name if you like)
 
 
   
  +
<pre>
 
function! s:DiffWithSaved()
 
function! s:DiffWithSaved()
 
let filetype=&amp;ft
 
diffthis
+
diffthis
 
" new | r &#35; | normal 1Gdd &gt;-- Uncomment this for horizontal split
 
new | r &#35; | normal 1Gdd
+
vnew | r &#35; | normal 1Gdd
 
diffthis
 
 
exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
diffthis
 
 
setlocal bt=nofile bh=wipe nobl noswf ro
 
 
 
endfunction
 
endfunction
 
 
com! Diff call s:DiffWithSaved()
 
com! Diff call s:DiffWithSaved()
  +
</pre>
   
 
To get out off diff view you can use the :diffoff command on vim7 or the nodiff.vim script from {{Script|id=1198}}.
 
 
To get out off diff view you can use the :diffoff command on vim7 or the nodiff.vim script from [/scripts/script.php?script_id=1198 vimscript &#35;1198]
 
 
   
 
}}
 
}}
   
 
== Comments ==
 
== Comments ==
nice tip...
 
you can also use this to split vertically:
 
 
vnew | 0r &#35;
 
 
 
 
sertac
 
, October 27, 2005 10:35
 
----
 
Helpful tip indeed..
 
 
If you change the function to,
 
function! s:DiffWithSaved()
 
let filetype=&amp;ft
 
diffthis
 
" new | r &#35; | normal 1Gdd - for horizontal split
 
vnew | r &#35; | normal 1Gdd
 
diffthis
 
exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
 
endfunction
 
com! Diff call s:DiffWithSaved()
 
 
then the filetype is also set (for syntax highlighting)
 
 
- Karthick
 
 
'''Anonymous'''
 
, October 27, 2005 21:02
 
----
 
Very nice tip
 
 
 
shankarpdy--AT--yahoo.com
 
, October 27, 2005 21:17
 
----
 
many thanks to Anonymous
 
his additions make this command even more useful :)
 
 
RichiH
 
, October 28, 2005 4:30
 
----
 
 
I'm bored of typing all the time ciwyes&lt;esc&gt; and ciwno&lt;esc&gt; when editing config files. So I define:
 
I'm bored of typing all the time ciwyes&lt;esc&gt; and ciwno&lt;esc&gt; when editing config files. So I define:
  +
<pre>
 
 
function s:ToggleYesNo()
 
function s:ToggleYesNo()
let w=expand("&lt;cword&gt;")
+
let w=expand("&lt;cword&gt;")
if w == "yes"
+
if w == "yes"
let w="no"
+
let w="no"
elseif w == "no"
+
elseif w == "no"
let w="yes"
+
let w="yes"
else
+
else
let w=""
+
let w=""
endif
+
endif
if w!=""
+
if w!=""
exec "normal! \"_ciw\&lt;C-R&gt;=w\&lt;cr&gt;"
+
exec "normal! \"_ciw\&lt;C-R&gt;=w\&lt;cr&gt;"
endif
+
endif
 
endfunc
 
endfunc
 
nnoremap gy :call &lt;SID&gt;ToggleYesNo()&lt;cr&gt;
 
nnoremap gy :call &lt;SID&gt;ToggleYesNo()&lt;cr&gt;
  +
</pre>
 
 
vim at bertram dash scharpf dot de
 
, October 30, 2005 11:08
 
 
----
 
----
Nice tip!
 
 
 
I made a similar function, adapted to mimic the 'cvs diff' command (cvs must be in the path):
 
I made a similar function, adapted to mimic the 'cvs diff' command (cvs must be in the path):
  +
<pre>
 
function! s:DiffWithCheckedOut()
 
function! s:DiffWithCheckedOut()
let filetype=&amp;ft
+
let filetype=&amp;ft
diffthis
+
diffthis
" new | r &#35; | normal 1Gdd - for horizontal split
+
" new | r &#35; | normal 1Gdd - for horizontal split
vnew | r !cvs up -pr BASE &#35;
+
vnew | r !cvs up -pr BASE &#35;
normal 1Gd6d
+
normal 1Gd6d
diffthis
+
diffthis
exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
+
exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
 
endfunction
 
endfunction
 
com! DiffCVS call s:DiffWithCheckedOut()
 
com! DiffCVS call s:DiffWithCheckedOut()
  +
</pre>
 
--
 
Ipkiss
 
 
ipkiss _at_ via _dot_ ecp _dot_ fr
 
, November 4, 2005 10:51
 
----
 
 
<!-- parsed by vimtips.py in 0.732250 seconds-->
 
<!-- parsed by vimtips.py in 0.732250 seconds-->

Revision as of 18:12, 21 July 2007

Previous TipNext Tip

Tip: #1030 - Diff current buffer and the original file

Created: October 27, 2005 10:15 Complexity: intermediate Author: Thomas Arendsen Hein - thomas--AT--intevation.de Version: 6.0 Karma: 61/22 Imported from: Tip#1030

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 plugins directory, open a file, make some modifications without saving them and do :DiffWithSaved (or choose a better name if you like)

function! s:DiffWithSaved() 
    let filetype=&ft 
    diffthis 
    " new | r # | normal 1Gdd  >-- Uncomment this for horizontal split 
    vnew | r # | normal 1Gdd 
    diffthis 
    exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype 
endfunction 
com! Diff call s:DiffWithSaved() 

To get out off diff view you can use the :diffoff command on vim7 or the nodiff.vim script from script#1198.

Comments

I'm bored of typing all the time ciwyes<esc> and ciwno<esc> when editing config files. So I define:

function s:ToggleYesNo() 
    let w=expand("<cword>") 
    if w == "yes" 
        let w="no" 
    elseif w == "no" 
        let w="yes" 
    else 
        let w="" 
    endif 
    if w!="" 
        exec "normal! \"_ciw\<C-R>=w\<cr>" 
    endif 
endfunc 
nnoremap gy :call <SID>ToggleYesNo()<cr> 

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

function! s:DiffWithCheckedOut() 
    let filetype=&ft 
    diffthis 
    " new | r # | normal 1Gdd - for horizontal split 
    vnew | r !cvs up -pr BASE # 
    normal 1Gd6d 
    diffthis 
    exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype 
endfunction 
com! DiffCVS call s:DiffWithCheckedOut()