如果我执行git commit
,当Vim打开时,我希望立即进入插入模式。
我注意到编辑时filetype
设置为gitcommit
,所以我想可以通过autocommand来实现这个目标。
au BufRead gitcommit startinsert!
这个方法不起作用,我做错了什么?
解决方案
我选择使用au FileType gitcommit execute "normal! O" | startinsert
来在当前行之前添加新行,然后进入插入模式以准备输入信息。
如果我执行git commit
,当Vim打开时,我希望立即进入插入模式。
我注意到编辑时filetype
设置为gitcommit
,所以我想可以通过autocommand来实现这个目标。
au BufRead gitcommit startinsert!
这个方法不起作用,我做错了什么?
解决方案
我选择使用au FileType gitcommit execute "normal! O" | startinsert
来在当前行之前添加新行,然后进入插入模式以准备输入信息。
export GIT_EDITOR='vim +startinsert'
!
以从消息末尾开始(在使用 Git 钩子进行消息完成时非常有用)。export GIT_EDITOR='vim +startinsert!'
vim +start
。 - jordelver在:au Event pattern command
中,该模式通常是与缓冲区名称匹配的。如果不想使用BufRead gitcommit
,可以使用BufRead COMMIT_EDITMSG
。如果你想要匹配文件类型选项,则使用FileType
事件。
我倾向于编写多行提交信息,并且我有一个来自vimrc_example.vim的自动命令,它执行以下操作
exe "normal! g`\""
每当我进入一个新的缓冲区时,那怎么样?
au FileType gitcommit 1 | startinsert
进入插入模式前如何跳转到第一行?现在我已经测试过了,我想保留它。:)
:set ft?
来查找当前缓冲区的文件类型,我还有其他的autocommands,例如au BufReadPost quickfix setlocal nospell
,它们使用了相同的东西。我已经采取了au FileType gitcommit execute "normal! O" | startinsert
的方式,在上面添加新行并进入插入模式。谢谢 :) - jordelver:execute "normal! 0"
不会添加新行,它只是将光标移动到行首。我的 :1
命令可以移动到第一行。你可以使用 :verbose au BufReadPost quickfix
命令查看为该事件和模式定义的自动命令以及定义它们的文件。同样地,:verbose set ft?
命令也可以使用。 - benjifisher这可能比实际需要的更加复杂,但您可以使用单独的文件类型插件来完成此操作:
$ mkdir -p ~/.vim/ftplugin/gitcommit
$ echo 'startinsert!' > !$/git-commit-insert.vim