Skip to content

Instantly share code, notes, and snippets.

@romainl
Last active June 21, 2022 07:43
Show Gist options
  • Save romainl/7198a63faffdadd741e4ae81ae6dd9e6 to your computer and use it in GitHub Desktop.
Save romainl/7198a63faffdadd741e4ae81ae6dd9e6 to your computer and use it in GitHub Desktop.
:DiffOrig but smarter

:DiffOrig but smarter

This is an enhanced version of the snippet provided under :help diff-original-file.

Where the original :DiffOrig only shows differences between the buffer in memory and the file on disk, :Diff can be used in two ways:

  • against the file on disk, like the original, with:

    :Diff
    
  • against an arbitrary Git revision of the current file, with:

    :Diff HEAD
    

My Vim-related gists.

function! Diff(spec)
vertical new
setlocal bufhidden=wipe buftype=nofile nobuflisted noswapfile
let cmd = "++edit #"
if len(a:spec)
let cmd = "!git -C " . shellescape(fnamemodify(finddir('.git', '.;'), ':p:h:h')) . " show " . a:spec . ":#"
endif
execute "read " . cmd
silent 0d_
diffthis
wincmd p
diffthis
endfunction
command! -nargs=? Diff call Diff(<q-args>)
@PeterRincker
Copy link

I have modified :Diff to take <mods>, set 'filetype, and do :diffoff! when the scratch buffer is wiped.

@romainl
Copy link
Author

romainl commented Jul 1, 2020

@peterrinker great!

@homogulosus
Copy link

Nice! I have made this into a small plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment