5

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?

5
  • 1
    Do you have a line in your config that activates this behaviour for all files? If so, you could make an exception for git.
    – jasonwryan
    Commented Oct 12, 2018 at 23:24
  • 3
    I'm pretty sure you have to configure it as it does not ship like that.
    – jasonwryan
    Commented Oct 12, 2018 at 23:29
  • I'll cheat: vim.wikia.com/wiki/… - you can adjust the actual postion on the line to fit your default line length (presumably 78).
    – jasonwryan
    Commented Oct 12, 2018 at 23:36
  • That's the page I got the idea from :) I wanted to make it work exactly like pressing ggA every time I commit. It's really frustrating how I can't just put that string in a configuration file, but instead have to learn a completely and weirdly documented new language.
    – l0b0
    Commented Oct 12, 2018 at 23:41
  • @user938271 That's weird: It works, but the cursor is invisible before I start typing or moving it somehow.
    – l0b0
    Commented Oct 12, 2018 at 23:56

2 Answers 2

6

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.

1
  • Fantastic! It's weird how such a simple command is so hard to automate.
    – l0b0
    Commented Oct 23, 2018 at 7:06
2

You can tell git to invoke vim with the startinsert command. Put this in your ~/.bashrc file:

export GIT_EDITOR="vim -c 'startinsert'"

But be warned that having to press escape when you have to commit a merge gets annoying, because there's already a generated commit message for those.

1
  • ah nice way to make it like annoying is using alt, alt+: has the same effect as pressing : in normal mode~
    – Fuseteam
    Commented Mar 26, 2022 at 4:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .