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

前端 未结 14 1780
刺人心
刺人心 2020-12-07 07:33

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?

14条回答
  •  借酒劲吻你
    2020-12-07 07:58

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

提交回复
热议问题