Skip to content

Instantly share code, notes, and snippets.

@PeterRincker
Forked from romainl/diff.md
Last active October 18, 2021 08:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PeterRincker/69b536f303f648cc21ec2ff2282f8c4a to your computer and use it in GitHub Desktop.
Save PeterRincker/69b536f303f648cc21ec2ff2282f8c4a 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(mods, spec)
let mods = a:mods
if !len(mods) && &diffopt =~ 'vertical'
let mods = 'vertical'
endif
execute mods . ' 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_
let &filetype = getbufvar('#', '&filetype')
augroup Diff
autocmd!
autocmd BufWipeout <buffer> diffoff!
augroup END
diffthis
wincmd p
diffthis
endfunction
command! -nargs=? Diff call Diff(<q-mods>, <q-args>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment