I have a setting (namely highlight the 81st character on the line) that I would like to turn off when there is a diff. This is easy enough as a search through the autocmds results in:
|FilterWritePre| starting to write a file for a filter command or diff
|FilterWritePost| after writing a file for a filter command or diff
So I end up with:
" Highlight the 81st character on the line if it's not the quickfix.
autocmd BufReadPost * if (&filetype != 'qf') |
\ let b:ColorColumnID = matchadd('ColorColumn', '\%81v.') |
\ endif
" When diffing take out the coloring because it looks like diff highlighting.
autocmd FilterWritePost * if (&diff && exists('b:ColorColumnID')) |
\ call matchdelete(b:ColorColumnID) |
\ endif
However, I would also like to turn the setting back on when I leave diff mode. As far as I can tell, there's no autocmd to detect this, and I can't think of any other robust ways to do it. Obviously I could check &diff
on every cursor move or something like that, but I'm hoping for something a little less hacky. Any ideas?
:set
command, maybe you could try to use theOptionSet
event. With this event, the pattern is matched again the full name of an option, and Vim should populate the internal variablesv:option_old
andv:option_new
with the old and new values of the option. Maybe something like this would be a start:autocmd OptionSet diff if v:option_new ==# 'diff' | {disable custom highlighting} | else | {enable custom highlighting} | endif
.matchadd()
is local to the window, not to the buffer, so maybew:
would be better thanb:
for the scope of theColorColumnID
variable.