Using Vim to View Git Commits

I spend a lot of my time in Vim and Git and this is an amazing combination of the two. While I love this command, I think we can do even better (jump to my version).

What is going on?

$ git log | vim -R -

Running this command, executes git log and opens a copy of Vim populated with the results of git log. Once you’re in Vim, if you place your cursor over a commit (e.g. 083279917ae3bbe8bfc25b2bf785acbbee302415) and click K, you’ll be temporarily shown the result of running git show on the commit.

Why does this work?

When you’re in Vim, the default behavior when you click K is to take the word under the cursor and look it up using the man program. This behavior can be modified by setting the value of keywordprg to something other than man.

Vim automatically sets keywordprg for some filetypes, including the filetype git. When the filetype is set to git, keywordprg is set to git show.

How does Vim know it is Git?

This is a great question that I don’t know the answer to. I’m guessing Vim recognizes the standard format of git log.

Can We Improve This?

I don’t love the standard format for git log. I have an improved version of git log that I prefer to use. This made me wonder if we can get this same commit view behavior in Vim with a log format I prefer. This is what I came up with.

Notes about Neovim

If you’re using Neovim, you’ll find that when you view a commit you can only see the end of the commit and you can not scroll back up. Instead you see the message “Press ENTER or type command to continue”.

This is due to how Neovim handles bang/system commands, as the author of Neovim, Thiago de Arruda, writes in this comment on Neovim Issue 1496

This is not a bug, it is the new behavior of bang commands: We no longer spawn the program with it’s stdout connected to Nvim tty, instead we open a pipe, read output and display to the user. This is the only way the bang commands will be consistent across UIs, so programs designed to be used interactively from the terminal will no longer work from inside nvim.

If you’re interested in learning more about this, these two issues have some good information.

this is very cool! and makes me think of a few ideas with keywordprg, but for git history viewing i still think the gv plugin is better (colors, diffs open in splits, more options, more interactive, etc) https://github.com/junegunn/gv.vim