You can use the CursorMoved
and CursorMovedI
autocommands to set the desired textwidth (or any other setting) based on the line the cursor is currently on:
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
The basic logic is simple: let &l:textwidth = line('.') == 1 ? 50 : 72
, although the nested autocommands make it look rather funky. You could extract some of it out to a script-local function (fun! s:setup_git()
) and call that, if you prefer.
The &:l
syntax is the same as setlocal
, by the way (but with setlocal
we can't use an expression such as on the right-hand-side, only a simple string).
Some related questions:
Note that the default gitcommit.vim
syntax file already stops highlighting the first line after 50 characters. From /usr/share/vim/vim80/syntax/gitcommit.vim
:
syn match gitcommitSummary "^.\{0,50\}" contained containedin=gitcommitFirstLine nextgroup=gitcommitOverflow contains=@Spell
[..]
hi def link gitcommitSummary Keyword
Only the first 50 lines get highlighted as a "Keyword" (light brown in my colour scheme), after that no highlighting is applied.
If also has:
syn match gitcommitOverflow ".*" contained contains=@Spell
[..]
"hi def link gitcommitOverflow Error
Notice how it's commented out, probably because it's a bit too opinionated. You can easily add this to your vimrc though:
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
Which will make everything after 50 characters show up as an error (you could, if you want, also use less obtrusive colours by choosing a different highlight group).