OneAndOneIs2

'cuz multiple steps into one is cool :)

If you work on a large codebase, you'll probably find yourself coming across code where you think "Who wrote this? When? Why?" and wishing you knew more about what lead to what you're looking at being written.

If you use a version control system, you can probably find out. If you're using git, it's easy: You use git blame, a helpful function that tells you the commit for every single line of code in a file.

You then look at that commit, and between the commit message and looking at all the code that was written as part of it, you stand a better chance of working out the "Who/why/when" problem.

But that's a bit tedious. So since we all use vim at work, I came up with something nicer: Press a shortcut key, and vim will show you the commit responsible for your current line. The whole process from above, instantly available in your text editor.

It was surprisingly simple, too. All it needs is:

In your .vimrc:" Get the commit responsible for the current line
nmap <f4> :call BlameCurrentLine()<cr>
" Get the current line number & file name, view the git commit that inserted it
fun! BlameCurrentLine()
let lnum = line(".")
let file = @%
exec "!gitBlameFromLineNo " lnum file
endfun

This maps the functionality to the F4 function key. Feel free to pick another.. All it does is get your current line & file, and pass it to a command which does the git stuff.

You could do the rest with a simple bash one-liner, but for easier maintainability, I stuck it into a little Perl script in /usr/local/bin/: