How to start Vim in insert mode at end of first line when committing Git changes

configurationgitvim

What would be the best way to set Vim up to always put the cursor in insert mode at the end of the first line (to account for commit message templates) when running git commit? This would basically do something identical to pressing ggA every time. Ideally this should be a Vim configuration (presumably in ~/.vim/after/ftplugin/gitcommit.vim), because I rely on $VISUAL rather than configuring editors for everything separately.


This almost works:

call feedkeys('ggA', 'int')

However, when running echo 'some text' >/tmp/COMMIT_EDITMSG && vim -Nu NONE --cmd 'filetype plugin on' /tmp/COMMIT_EDITMSG the cursor is on the status line until I press something:

Vim with missing cursor


1 | startinsert! works for echo 'some text' >/tmp/COMMIT_EDITMSG && vim -Nu NONE --cmd 'filetype plugin on' /tmp/COMMIT_EDITMSG, but when running git commit -t /tmp/COMMIT_EDITMSG it breaks completely – the commit message is not shown and the commit template is shown below the status line:

<code>git commit</code> broken view

After pressing right arrow the commit message and cursor shows up, and the editor is in insert mode, but the cursor is at the second character rather than at the end of line:

<code>git commit</code> after moving cursor

Do I need to add something to the configuration to tell Vim to show the actual contents of the buffer?

Best Answer

Adapting one of the autocommands given in the Vim Wikia, this seems to work fine with git commit -t /tmp/COMMIT_EDITMSG for me:

" ~/.vim/ftplugin/gitcommit.vim
au! VimEnter COMMIT_EDITMSG exec 'norm gg' | startinsert!

I used exec 'norm gg' | instead 1 | because :1 | is equivalent to :1p | and there's a small delay as the line is printed.

Related Question