after/ftplugin/gitcommit.vim

Don’t limit ourselves to plain ASCII encoding:

scriptencoding utf-8

Enforce vim script implementation to reduce ambiguities and improve robustness:

scriptversion 4

Switch to insert mode immediately when opening a new commit message:

augroup jnrowe_gitcommit
    autocmd!
    autocmd BufEnter */COMMIT_EDITMSG if len(getline('.')) == 0 |
    \       startinsert |
    \   endif
augroup END

Note

The assumption here is that if the first line is blank this is a new commit message, otherwise we’re amending a previous message. This may fail if you use a custom commit template.

Configure maps to insert common metadata in to commit messages:

call keymaps#mnemonic_map('Trailer', #{buffer: v:true, local: v:true})

for s:type in ['Acked', 'Co-authored', 'Reviewed', 'Signed-off', 'Tested']
    execute printf('nnoremap <silent> <buffer> [Trailer]%s ' ..
    \              ':call filetypes#add_git_trailer("%s", "%s")<CR>',
    \              tolower(s:type[0]), s:type, g:user_email)
    execute printf('nnoremap <silent> <buffer> [Trailer]q%s ' ..
    \              ':call filetypes#add_git_trailer("%s")<CR>',
    \              tolower(s:type[0]), s:type)
endfor

Tip

This adds two bindings for each type. For example, using [trailer]a with add an Acked-by trailer with the default user, whereas the [trailer]qa binding will query for the user data.

Querying user for git commit trailer value

Add a mapping for Sponsored-by header:

nnoremap <silent> <buffer> [Trailer]qp
\   :call filetypes#add_git_trailer('Sponsored')<CR>

A few co-workers are now decorating their commits with emojis for expressing their types, and I’ll try to do so when committing to those projects:

for [s:key, s:char] in [['new', '🌟'], ['del', '❌'], ['fix', '🐛'],
\                       ['ref', '⟳'], ['tool', '🔨']]
    silent execute 'iabbrev <buffer> _' .. s:key .. ' ' .. s:char
endfor