0

I found this where I can highlight code if it exceeds a certain width. However, I don't want that for text files (like LaTeX). Thus, I tried (in my .vimrc):

if (&ft=='python' || &ft!='r')
    highlight OverLength ctermbg=red ctermfg=white guibg=#FFD9D9
    match OverLength /\%81v.\+/
endif

What am I doing wrong? All code/text is highlighted, even for other types. My LaTeX files are .Rnw and thus set ft yields rnoweb.

Community
  • 1
  • 1
Xiphias
  • 3,668
  • 4
  • 22
  • 40

2 Answers2

1

The .vimrc is read only once on startup. So it's a right place to define a highlight (without adding any conditional):

highlight OverLength ctermbg=red ctermfg=white guibg=#FFD9D9

But if you want to make a match each time you open a file (with or without a conditional), you have to wrap your :match command inside an autocommand (in your .vimrc), for example:

au BufReadPost * if &ft=='python' || &ft!='r' | match OverLength /\%81v.\+/ | endif
yolenoyer
  • 7,117
  • 1
  • 20
  • 47
  • I copied that but still get highlighting every time. Is there any other trap? – Xiphias May 24 '16 at 13:58
  • @Xiphias `&ft != 'r'` will match too many things. I think the condition is incorrect. BTW, I'd have gone for a `is_a_text_ft()` function. (Well, [I did actually](https://github.com/LucHermitte/lh-vim-lib/blob/master/autoload/lh/ft.vim#L53)) – Luc Hermitte May 24 '16 at 14:25
  • @Luc Hermitte: Ha, shame on me. I just swapped `==` and `!=` but kept `||` instead of `&&`. Now it works :-) Thanks for your reference, but that code is above my vimscript abilities. I'll stick with the above :-) – Xiphias May 24 '16 at 14:42
  • 1
    @Xiphias, you could still simplify it to `if &ft !~ '\v^$|text|latex|tex|html|docbk|help|mail|man|xhtml|markdown|gitcommit'` – Luc Hermitte May 24 '16 at 15:37
0

Vim has syntax files for this type of file specific syntax highlighting.

You should create python.vim under your ~/.vim/syntax/ directory (on non-windows machines). Then add the following to python.vim

highlight OverLength ctermbg=red ctermfg=white guibg=#FFD9D9
match OverLength /\%81v.\+/

This way your vimrc remains clean and doesn't get cluttered with lot of filetype specific code.

See :h syn-files to read more about syntax files.

ronakg
  • 3,552
  • 16
  • 36