How can I disable vim's textwidth for specific lines?

3

1

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?

jwir3

Posted 2017-10-11T18:26:40.413

Reputation: 381

Answers

3

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.

Alternative

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

Ingo Karkat

Posted 2017-10-11T18:26:40.413

Reputation: 19 513

Awesome. That plugin was exactly what I was looking for. Thank you! – jwir3 – 2017-10-13T17:23:39.463