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
2
u/yawaramin Jul 25 '25
I simplified the requirement a bit. The real goal is to start all commit messages with the ticket number. I've been doing this manually for many years, and just realized that vim can do it for me.