我经常使用 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 列之后的所有内容,以表明我的标题太长...
最佳答案
您可以使用 CursorMoved
和 CursorMovedI
自动命令根据光标当前所在的行设置所需的文本宽度(或任何其他设置):
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
我们不能使用这样的表达式如右侧所示,只有一个简单的字符串)。
一些相关问题:
- Line number specific text-width setting .
- Set paste mode when at beginning of line but auto-indent mode when anywhere else on the line?
请注意,默认的 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/