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

前端 未结 14 1806
刺人心
刺人心 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:47

    Changes you just edited [buffer], i.e. those that differ from last saved version (in working directory), these may differ with last index version (Git). I mapped both:

    " Find diff inbetween currrent buffer and ... A{last index version} vs B{last saved version in working directory}
    "  - A{last index version}: the file as you last commited it
        " git diff to vimdiff against the index version of the file:
        nnoremap gd :Gvdiff:echo "currentBuffer vs lastIndexVersion (last commited)"
    "  - B{last saved version in working directory}: the file you last :w,
    "   not neccesary commited it (not commited for sure if it is in NO git project)
        " https://vim.fandom.com/wiki/Diff_current_buffer_and_the_original_file
        nnoremap gd2 :DiffSaved:echo "currentBuffer vs lastSaved (not neccesary equal to last commited)"
        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()
    

    Example of vimdiff vs Gdiff.

    • vimdiff:
    • Gdiff: Commiting changes, you see only Gdiff, what might, or might have same changes as vimdiff:

    Furthermore, to easy vimdiff homonym file in other path:

        " vimdiff homonym file 
       nnoremap dh  :vsplit %:p:h/../__/%:t  :windo diffthis
            "E.g."$ vim /path01/proj02_pg064/processorder.php
            ":vsplit %:p:h/../proj01_pg05/%:t | :windo diffthis
    

提交回复
热议问题