views:

1110

answers:

7

I use Vim. I open a file. I edit it and I want to see what I've edited before I save it.

How can I do this in Vim?

+22  A: 
:w !diff % -
chaos
That's pretty awesome.
sharth
+17  A: 

http://vim.wikia.com/wiki/Diff_current_buffer_and_the_original_file

sharth
This is state of the art for vim. Forget the other answers.
Luc Hermitte
Might want to give RCSVers.vim a try: http://www.vim.org/scripts/script.php?script_id=563
JD
+2  A: 

Source the following and use :DIFF command

function! s:diff()
    let tmpa = tempname()
    let tmpb = tempname()
    earlier 100h
    exec 'w '.tmpa
    later 100h
    exec 'w '.tmpb
    update
    exec 'tabnew '.tmpa
    diffthis
    vert split
    exec 'edit '.tmpb
    diffthis
endfunction
command! -nargs=0 DIFF call <SID>diff()
Mykola Golubyev
+3  A: 

I've always likes diffchanges - nice, simple, works.

ldigas
+1  A: 

from vimrc_example.vim:

" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
if !exists(":DiffOrig")
  command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
          \ | wincmd p | diffthis
endif
0x89
+1  A: 

Not exactly what you're looking for but SCMDiff.vim is really cool. One keypress, and it diff-highlights your current file with the head revision in a source control repo. It's meant to work with many SCMS. I use it with perforce.

prestomation
+1  A: 

I can recommend the histwin plugin.

While it doesn't diff to the current saved version of the file (like the other answers), it can vimdiff changes since you started edting, and even replay your changes in order. The difference shows if you save intermediately.

Additionally, it displays a list of all undo history branches and allows you to switch or diff between them.

PS: While the plugin doesn't automatically track moments in the edit history since every file change, you can explicitly "tag" the moment when you save the file such that you can later vimdiff with it, if you want that. Maybe this could be automated?

catchmeifyoutry