2

I use and love the plugin calendar.vim. Those days, I decided to add the following to my vimrc:

autocmd BufEnter *.tex,*.md call matchdelete(4)
autocmd BufLeave *.tex,*.md call matchadd("ErrorMsg", "\\%>80v.\\+", 10, 4)

This way, if I alternate to a buffer that contains a .tex or a .md file, the error after 80chars becomes disabled. If I alternate to a buffer with any other type of file, it's enabled again. The problem is, it makes most of the calendar buffer become marked with error highlighting. Simply adding calendar as file extension in the above code does not work.

Searching through the issues of the plugin's github, I found the following issue raised: https://github.com/itchyny/calendar.vim/issues/98. There, the plugin's author give a code snippet for making it so that the above type of error message for long lines does not display over the calendar:

autocmd BufEnter,FileType *
  \   if &l:filetype ==# 'calendar'
  \ |   match
  \ | else
  \ |   match ErrorMsg '\%>80v.\+'
  \ | endif

But with only that, I loose the ability of also having the error highlighting disabled with tex and md files. I tried the following:

autocmd BufEnter,FileType *
  \   if &l:filetype ==# 'calendar' || &l:filetype ==# 'tex' || &l:filetype ==# 'md'
  \ |   match
  \ | else
  \ |   match ErrorMsg '\%>80v.\+'
  \ | endif

However, that also does not work well. It disables the error highilghting only on the calendar buffer, not on buffers displaying tex or md files.

Then, my question. Is there a way I can have the above code modified such that I could have the error highlighting for over 80chars lines when I navigate my cursor to buffers containing any other files than tex, md or the buffer calendar?

1 Answer 1

1

&l:filetype is not equal to file extension. For .md files, filetype is markdown, not sure what it will be for .tex (my Vim show plaintext) - so this one you will have to check in Your Vim with :set filetype?

So this should look something like this (change filetype for tex):

autocmd BufEnter,FileType *
  \   if index(['calendar', 'tex', 'markdown'], &filetype) > -1
  \ |   setlocal cc=
  \ | else
  \ |   setlocal cc=80
  \ | endif

Edit: Shorten if statement using @Martin Tournoij comment

3
  • 1
    You can shorten that if to something like: if index(&l:filetype, ['calendar', 'tex', 'markdown']) > -1. Jan 15, 2019 at 9:22
  • The current version of your answer gives the error E714: list required. The original code you had there didn't throw an error, but also did not work - it's almost identical to what I had, just changed md with markdown. That was not the problem, unfortunately. It still does not work even for tex (whose type is indeed tex). Being more precise: like my original code, it works for calendar (i.e. alternating from/to a calendar buffer toggles the error highlighting) but not for tex or markdown files.
    – FVb
    Jan 15, 2019 at 19:04
  • Order was wrong in index, and I switched to using colorcolumn, introduced in Vim 7.3. This code works perfectly in my Vim.
    – grodzik
    Jan 16, 2019 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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