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?

  • I don't know if it would work, because it depends on how you toggle the diff mode in a window, but if you use the :set command, maybe you could try to use the OptionSet event. With this event, the pattern is matched again the full name of an option, and Vim should populate the internal variables v:option_old and v: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. – user9433424 Jan 20 '17 at 22:46
  • 1
    Also, not sure, but I think the highlighting created by matchadd() is local to the window, not to the buffer, so maybe w: would be better than b: for the scope of the ColorColumnID variable. – user9433424 Jan 20 '17 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.