在 Vim 中自动包装很长的 Git 提交消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11023194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically wrap long Git commit messages in Vim
提问by mgilson
Using Git, my editor for commits is Vim. I'm constantly doing a poor job of remembering to wrap lines and so I get long lines (see this answerfor example). Is there a way using some git config
or .vimrc
magic that I can get Vim to automatically wrap lines at 72 columns?
使用 Git,我的提交编辑器是 Vim。我经常在记住换行方面做得很差,所以我排得很长(例如,参见这个答案)。有没有办法使用一些git config
或.vimrc
魔法让 Vim 自动换行 72 列?
回答by Abe Voelker
Add this to your .vimrc
:
将此添加到您的.vimrc
:
au FileType gitcommit setlocal tw=72
回答by David Cain
While the other answers solve this problem perfectly well, I highly recommend you install Tim Pope's fugitive.vim.
虽然其他答案很好地解决了这个问题,但我强烈建议您安装 Tim Pope 的fugitive.vim。
fugitive.vim is an excellent plugin that brings Git functionality to Vim. It has a whole host of features that aren't relevant to your question, but are worth checking out. However, it can also help you remember to keep your commit messages the proper length:
fugitive.vim 是一个优秀的插件,它为 Vim 带来了 Git 功能。它有很多与您的问题无关的功能,但值得一试。但是,它还可以帮助您记住将提交消息保持适当的长度:
回答by JBallin
回答by Raymond Wu Won
Several of the options from the earlier posts work, except I noticed inconsistencies between different systems.
早期帖子中的几个选项有效,但我注意到不同系统之间存在不一致。
Fedora 28 (recently upgraded from F26) was easy once I realised :version inside git-commit/git-tag showed it was pointing to .virc files (weird*) so I simply copied my ~/.vimrc into ~/.virc[except, see below].
Fedora 28(最近从 F26 升级)很容易,一旦我意识到 :version 里面的 git-commit/git-tag 显示它指向 .virc 文件(奇怪的*)所以我简单地将我的 ~/.vi mrc复制到~/. virc[除外,见下文]。
macOS 10.13.4 with vim 8.0 from brewworks just fine off /usr/share/vim/vim80/ftplugin/gitcommit.vim according to :verbose :set tw=?.
macOS 10.13.4 和来自brew 的vim 8.0根据:verbose :set tw=? .
CentOS 7.4 with vim 7.4 (git version 1.8.3.1) for some reason though didn't appear to be making use of the textwidth line in its supplied gitcommit.vim so I went for a quick and dirty workaround (to save me from dealing with multiple files) in ~/.vimrc:
CentOS 7.4 带有 vim 7.4(git 版本 1.8.3.1)出于某种原因,虽然似乎没有使用其提供的 gitcommit.vim 中的 textwidth 行,所以我进行了快速而肮脏的解决方法(为了避免我处理多个文件)在 ~/.vimrc 中:
nmap <F2> :set textwidth=72<CR>
inoremap <F2> <Esc>:set textwidth=72<CR>a
That seems to work well enough and is easy to remember - I mostly only pull from there anyway and have kind of given up messing around any more with the old versions of git and vim.
这似乎工作得很好并且很容易记住 - 我基本上只是从那里拉出来,并且已经放弃了对旧版本的 git 和 vim 的混乱。
Otherwise, I (temporarily) went for Chip Hogg's suggestionafter Abe Voelker's answer: autocmd FileType gitcommit setlocal textwidth=72
否则,在 Abe Voelker 的回答之后,我(暂时)采纳了 Chip Hogg 的建议:autocmd FileType gitcommit setlocal textwidth=72
I don't think it makes much of a difference inside git-commit but it's possibly better to be safe (especially if that line ends up getting copied throughout a vimrc). Filetype is of course set to on, as it is in many example vimrcs.
我认为它在 git-commit 内部没有太大区别,但安全可能更好(特别是如果该行最终在整个 vimrc 中被复制)。文件类型当然设置为 on,就像在许多示例 vimrcs 中一样。
* However,I was still curious as to why vim's ruler wasn't showing, so I looked at :help ruler which informs +cmdline_info (displayed after :version) needs to be set at compile time. Running :ver in vim outside of git-commit revealed different settings and a different compiled time, suggesting that git was perhaps calling the system copy of vim instead of the user one.
*但是,我仍然很好奇为什么 vim 的标尺没有显示,所以我查看了 :help 标尺,它通知 +cmdline_info(在 :version 之后显示)需要在编译时设置。在 git-commit 之外的 vim 中运行 :ver 显示不同的设置和不同的编译时间,这表明 git 可能正在调用 vim 的系统副本而不是用户副本。
So what I should have done at the beginning to all this was run git config --global core.editor "vim"except I didn't because I had assumed it was a redundant step. Doing this first on every git installation might save a lot of hassle from the start!
所以我在开始时应该做的就是运行git config --global core.editor "vim"除非我没有,因为我认为这是一个多余的步骤。在每次 git 安装时先执行此操作可能会从一开始就省去很多麻烦!
回答by sur
Here's a git hook for auto-wrapping that will work with any editor: https://github.com/surabhigupta/AutoWrapSeventyTwo
这是一个用于自动包装的 git 钩子,适用于任何编辑器:https: //github.com/surabhigupta/AutoWrapSeventyTwo
回答by cambunctious
In addition to other answers, use gqip
to reformat a paragraph while editing.
除了其他答案,用于gqip
在编辑时重新格式化段落。