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

发布于 2024-09-15 08:50:30 字数 1187 浏览 11 评论 0原文

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

有其他方法来解决这个问题的想法吗?

" 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>

I have these Vim mappings for Git. Most of the commands below work, but the last one, gm, doesn't work at all. I want to open a new Vim (preferrably with my git commit message template already loaded, as per the default terminal behavior), edit and save my message, then commit.

Any ideas for another way to approach this?

" 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

Update:

As per VonC's suggestion, I am now using fugitive.vim. I replaced my previous Vim bindings, and would recommend this plugin to anyone who uses Vim and Git.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱桃奶球 2024-09-22 08:50:30

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

一个符合要求的脚本是 fugitive.vim:参见 其文档

: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 脚本中重用所需的部分。

Vim has some Git wrapper scripts which encapsulates common Git commands, and allows committing through a temporary buffer:

One that could fit the bill would be fugitive.vim: see its doc:

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

Opens temporary buffer for you to enter commit message. Write the commit buffer and all tracked files are committed.

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

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

Opens temporary buffer for you to enter commit message. Write the commit buffer and all files in a working directory of a current repository are committed.

This command is different from |:MinSCMCommitTracked| in adding untracked files to the current repository.

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

The source being available, you could reuse the part you need in your own Vim script.

凶凌 2024-09-22 08:50:30

编辑文件然后告诉 git 使用它作为提交消息到底有什么意义?为什么不能使用正常的操作模式 - 运行 git commit,让它生成 vim,写入消息,保存并退出以提交?或者,如果您需要模板,git commit -e -F <模板文件>

What exactly is the point of editing a file then telling git to use it as the commit message? Why can't you just use the normal mode of operation - run git commit, let it spawn vim, write the message, save and quit to commit? Or if you need a template, git commit -e -F <template file>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文