在新的Vim窗口中编写Git提交消息,然后在Vim中提交所有内容

时间:2010-08-20 00:06:12

标签: git vim map mapping shortcut

我有Git的这些Vim映射。下面的大多数命令都有效,但最后一个命令 g m 根本不起作用。我想打开一个新的Vim(最好根据默认的终端行为加载我的git commit消息模板),编辑并保存我的消息,然后提交。

任何想法以另一种方式来解决这个问题?

" git shortcuts
"" show diff in new window
if has("gui_running")
  noremap gd :!git diff <BAR> gvim -<CR><CR>
else
  noremap gd :!git diff <BAR> vim -<CR><CR>
endif
noremap gs :!git status<CR>
noremap ga :!git add %<CR>
"" edit commit message in new window
if has("gui_running")
  noremap gm :!gvim __vim_gitcommitmessage && git commit -F __vim_gitcommitmessage && rm __vim_gitcommitmessage<CR>
else
  noremap gm :!vim __vim_gitcommitmessage && git commit -F __vim_gitcommitmessage && rm __vim_gitcommitmessage<CR>
endif

更新

根据VonC的建议,我现在正在使用fugitive.vim。我替换了以前的Vim绑定,并会向使用Vim和Git的任何人推荐这个插件。

" fugitive shortcuts
noremap ggs :Gstatus<cr>
noremap ggc :Gcommit<cr>
noremap gga :Gwrite<cr>
noremap ggl :Glog<cr>
noremap ggd :Gdiff<cr>

2 个答案:

答案 0 :(得分:4)

Vim有一些Git包装器脚本,它封装了常见的Git命令,并允许通过临时缓冲区提交:

符合费用的是fugitive.vim:请参阅its doc

:MinSCMCommitTracked[!]         (Default mapping: \s<C-c>)
  

打开临时缓冲区,供您输入提交消息。写入提交缓冲区并提交所有跟踪的文件。

            Used SCM commands ~
            hg  : commit
            git : commit -a
            bzr : commit

:MinSCMCommitAll[!]             (Default mapping: \sc)
  

打开临时缓冲区,供您输入提交消息。编写提交缓冲区,并提交当前存储库的工作目录中的所有文件。

     

此命令与|:MinSCMCommitTracked |不同将未跟踪的文件添加到当前存储库。

            Used SCM commands ~
            hg  : commit -A
            git : add -a && commit -a
            bzr : add && commit

源可用,您可以在自己的Vim脚本中重用所需的部分。

答案 1 :(得分:1)

编辑文件然后告诉git将其用作提交消息究竟是什么意思?为什么你不能只使用正常的操作模式 - 运行git commit,让它生成vim,编写消息,保存并退出提交?或者,如果您需要模板git commit -e -F <template file>