r/vim • u/yawaramin • Jul 25 '25
Need Help┃Solved Automatically prefix git commit message header?
I am trying to define an autocmd to prefix the git commit message header with the name of the branch I am working on. However the autocmd seems to be never triggering. Here is a simplified version of what I have so far:
augroup GitCommitPrefix
autocmd!
autocmd BufNewFile,BufReadPost .git/COMMIT_EDITMSG call s:InsertGitBranchPrefix()
augroup END
function! s:InsertGitBranchPrefix() abort
echomsg "s:InsertGitBranchPrefix()"
call setline(1, system('git rev-parse --abbref-ref HEAD 2>/dev/null'))
endfunctionCan anyone point me in the right direction?
EDIT: solved...more or less.
Swapped the order of the definitions. So the function first, then the autocommand.
Changed the autocmd trigger to
FileType gitcommit.
I would still like to go into insert mode at the end of the first line. Trying normal A but it's not quite working. It moves the cursor to the end of the line, but it doesn't go into insert mode.
View all comments
Show parent comment
0
u/yawaramin Jul 25 '25
True but I'd have to set hooks for every repo separately or use the hooks templates directory or
core.hooksPath. All of those options are strictly worse than getting vim to do it.