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