git - 将 git commit 消息的主题行限制为 50 个字符

标签 git vim

我经常使用 vim 来格式化我的 git 提交消息。我在 increasing 中看到的趋势popularity是提交消息的第一行应限制为 50 个字符,然后后续行应限制为 72 个字符。

I already know how to make my commit wrap at 72 characters based on my vimrc file :

syntax on
au FileType gitcommit set tw=72

有没有办法让 vim 在第一行 50 个字符处自动换行,然后在 72 个字符处自动换行?

同样好的答案可以在第一行突出显示第 50 列之后的所有内容,以表明我的标题太长...

最佳答案

您可以使用 CursorMovedCursorMovedI 自动命令根据光标当前所在的行设置所需的文本宽度(或任何其他设置):

augroup gitsetup
        autocmd!

        " Only set these commands up for git commits
        autocmd FileType gitcommit
                \ autocmd CursorMoved,CursorMovedI *
                        \ let &l:textwidth = line('.') == 1 ? 50 : 72
augroup end

基本逻辑很简单:let &l:textwidth = line('.') == 1 ? 50 : 72,虽然嵌套的自动命令让它看起来很时髦。如果您愿意,您可以将其中的一些提取到脚本本地函数 (fun!s:setup_git()) 并调用它。

顺便说一句,&:l 语法与 setlocal 相同(但是对于 setlocal 我们不能使用这样的表达式如右侧所示,只有一个简单的字符串)。

一些相关问题:


请注意,默认的 gitcommit.vim 语法文件已经在 50 个字符后停止突出显示第一行。来自 /usr/share/vim/vim80/syntax/gitcommit.vim:

syn match   gitcommitSummary    "^.\{0,50\}" contained containedin=gitcommitFirstLine nextgroup=gitcommitOverflow contains=@Spell
[..]
hi def link gitcommitSummary        Keyword

只有前 50 行被突出显示为“关键字”(在我的配色方案中为浅棕色),之后不会应用任何突出显示。

如果还有:

syn match   gitcommitOverflow   ".*" contained contains=@Spell
[..]
"hi def link gitcommitOverflow      Error

注意它是如何被注释掉的,可能是因为它有点太自以为是了。不过,您可以轻松地将它添加到您的 vimrc 中:

augroup gitsetup
        autocmd!

        " Only set these commands up for git commits
        autocmd FileType gitcommit
                \  hi def link gitcommitOverflow Error
                \| autocmd CursorMoved,CursorMovedI *
                        \  let &l:textwidth = line('.') == 1 ? 50 : 72
augroup end

这将使 50 个字符后的所有内容都显示为错误(如果需要,您也可以通过选择不同的高亮组来使用不那么突兀的颜色)。

关于git - 将 git commit 消息的主题行限制为 50 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43929991/

相关文章:

git - Jenkins Multibranch Pipeline 缺乏对 PathRestriction 触发器的支持

git - 无法克隆 Git 存储库,即使我将公钥添加到 GIthub

git - 如何查看工作目录和暂存索引之间的差异?

python - 如何在 Syntastic for Vim 中将 Python 允许的最大行长度设置为 120?

python - Mac OSX 10.10.5 上 Vim/Tmux 的电力线安装问题

svn:使用vim合并冲突

eclipse - 如何使用 GitHub 设置 Eclipse/EGit?

svn - 使用 git-svn 在 svn 中轻松 merge

python - 如何在 Vim 中转置文件中行和列的内容?

linux - 从其他选项卡彻底杀死所有 vim 进程