Search code examples
diffvimpreview

Can I see changes before I save my file in Vim?


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?


Solution

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

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