Skip to content

Instantly share code, notes, and snippets.

@romainl
Last active December 24, 2024 10:29
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

@matveyt
Copy link

matveyt commented Dec 24, 2024

This is quite an old gist but I guess some people may still use it. So it's probably worth noting that finddir() trick is not needed. Instead, we can force git to figure path itself:

let cmd = '!git -C #:p:h:S show '..a:spec..':./#:t:S'

-C option makes git to change its working directory first; then it's okay to use "single dot" as relative path inside any commit too.

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