Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Vim script to show git commit diff in vertical split while writing commit messages
" Put this in your .vimrc and whenever you `git commit` you'll see the diff of your commit next to your commit message.
" For the most accurate diffs, use `git config --global commit.verbose true`
" BufRead seems more appropriate here but for some reason the final `wincmd p` doesn't work if we do that.
autocmd VimEnter COMMIT_EDITMSG call OpenCommitMessageDiff()
function OpenCommitMessageDiff()
" Save the contents of the z register
let old_z = getreg("z")
let old_z_type = getregtype("z")
call cursor(1, 0)
let diff_start = search("^diff --git")
if diff_start == 0
" There's no diff in the commit message; generate our own.
let @z = system("git diff --cached -M -C")
" Yank diff from the bottom of the commit message into the z register
:.,$yank z
call cursor(1, 0)
" Depending on the width of the window, split vertically or horiziontal
" 144 is wide enough to display two 72 character commit messages
if winwidth(0) >= 144
" Paste into a new buffer
normal! V"zP
" Restore the z register
call setreg("z", old_z, old_z_type)
" Configure the buffer
set filetype=diff noswapfile nomodified readonly
silent file [Changes\ to\ be\ committed]
" Get back to the commit message
wincmd p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment