I have textwidth set to 80 characters, and I have it set to 75 characters for gitcommit files.

The thing is, the git commits at my company always have a last line that indicates some metadata about the commit (whether to bump the version number, the ticket associated, etc...). What I'd like to do is turn off the textwidth for this last line. This last line will always begin with "(patch)", "(minor)", or "(major)" (for semver autopublishing).

I currently have it so that this metadata line is highlighted, using the following in my syntax file:

syn match   autoPublishLine     "^\((patch)\|(minor)\|(major)\).*"
hi def link autoPublishLine             Special

This works as I expect it to, but what I'd really like to do is also unset the textwidth for this line, so it doesn't wrap words for this one line.

Any idea how I might go about this?


As a quick hack, you can adapt the 'textwidth' value whenever the cursor moves:

:autocmd CursorMoved,CursorMovedI <buffer> let &l:textwidth = (getline('.') =~# '^\((patch)\|(minor)\|(major)\)' ? 0 : 75)

You can put that command into ~/.vim/ftplugin/gitcommit_textwidth.vim so that it automatically applies to Git commit messages.


To avoid the duplication of the pattern, my OnSyntaxChange plugin enables you to set up :autocmds that react to changes in the underlying syntax group. It basically works like the above solution, but on a far more elaborate level.

call OnSyntaxChange#Install('AutoPublish', '^autoPublishLine$', 1, 'a')
autocmd User SyntaxAutoPublishEnterA setlocal textwidth=0
autocmd User SyntaxAutoPublishLeaveA setlocal textwidth=75
  • Awesome. That plugin was exactly what I was looking for. Thank you! – jwir3 Oct 13 '17 at 17:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.