Git Commit Templates, Vim, And You!

 

The newer versions of git ( >= 1.7.x I believe) have support for using templates in commit messages which is a really handy feature. They are made even moreso by using template tokens in conjunction with ... power of Vim echoes through the valley. So lets take a second to see how we can get this magic working.

In your .gitconfig

Plain # This makes vim jump to the first line when you enter edit mode for the commit [core] editor = vim -c ':normal gg' [commit] # set the filepath to the template # This filename can be whatever you want, I called it this template = ~/.git.commit.template That's it for the git stuff, here you can read the (quite sparse) documentation for commit templates.

What does the template look like?

I dev using Scrum so it's broken down into stories which have tasks so my template looks like this.

PlainS<Story#>:T<Task#>:<Message>

Really simple, you can put anything inside the tags you want.

I have the pooowweeerrrr... of Vim

Using the abundant array of vim motions and text objects we can make this task quick, painless, and have no need for fancy-shmancy plugins.

Down to business. You've just typed git commit and you're in a vim buffer, what do you do now?

  1. f< — Move to the first <...
  2. ca< — Delete template tag and enter insert mode...
  3. tack tack tack — Type whatever is supposed to replace that token...
  4. ESC — Hit escape to get back to Normal mode...
  5. ; — repeat the last motion (f< in this case)...
  6. Rinse
  7. Repeat

 

But that is so much typing!

Yes, yes it is. So this is what we're going to do: we're going to pour on some more vim awesomesauce with a keyboard mapping:

Plain " Place in your vimrc " <leader> is usually \ if you haven't set it nm <leader>T f<ca<<CR>

Now all you have to do is \T and steps 1 and 2 are taken care of it for you so it now looks like this:

  1. git commit
  2. \T
  3. tack tack tack
  4. ESC
  5. ;
  6. Repeat