3

I my ~/.vimrc I use this syn for long lines

augroup longLines                                                                                   
     au!
     au! filetype zsh,sh,python,vim,c,cpp
         \ syn match ColorColumn /\%>80v.\+/ containedin=ALL
 augroup END 

but this overwrite other syn, with

ss

without

ss

Why the synoverwrite other highlight?
this is notorious in the last lines

 sys.exit(1)
 import settings

have different colors, with syn, the lines lost normal highlight

5
  • 2
    Uhmm, okey .... Is there a question in there somewhere?
    – Rook
    Dec 31, 2011 at 2:14
  • In vim7.3 I use :set cc=80
    – kev
    Dec 31, 2011 at 2:35
  • @kev I don't like cc is very ugly, always show the column 80, my syn only is showed when the line have more of 80 chars
    – JuanPablo
    Dec 31, 2011 at 2:47
  • @ldigas the question is, why the syn overwrite other hi ?
    – JuanPablo
    Dec 31, 2011 at 2:49
  • You can highlight with underline
    – kev
    Dec 31, 2011 at 3:02

2 Answers 2

2

I use the following code:

highlight TooLongLine term=reverse ctermfg=Yellow ctermbg=Red
autocmd BufEnter,WinEnter * if &tw && !exists('b:DO_NOT_2MATCH') |
            \                 execute '2match TooLongLine /\S\%>'.(&tw+1).'v/' |
            \               endif
autocmd BufLeave,WinLeave * 2match

command -nargs=0 -bar Dm let b:DO_NOT_2MATCH=1 | 2match
command -nargs=0 -bar Sm execute '2match TooLongLine /\S\%>'.(&tw+1).'v/' |
            \            silent! unlet b:DO_NOT_2MATCH

If you don’t want to be able to remove this highlighting, depend on textwidth and insist on highlighting spaces that go beyond the limit, then you can truncate this to just

2match TooLongLine /.\%>80v/

This solution uses match-highlight that does not scrap syntax highlighting, but always overrides it.

0

I realize you asked this quite some time ago, but in case if other people ask too, perhaps you could try using the matchadd() function, instead, like this:

hi def longLine gui=reverse    "or guibg=pink, or whatever you prefer
augroup longLines
    au!
    au! filetype zsh,sh,python,vim,c,cpp
                \ call matchadd("longLine", "\\%>80v", 0, 9999)
augroup END

Most importantly, make sure that you do NOT set the guifg of whatever highlight group you decide to use. That would overwrite your syntax highlighting.

Another important part (for me, at least) is to use matchadd with 0 as the third parameter so that your Search highlighting remains effective and doesn't get overtaken by the longLine highlighting.

The fourth parameter can be omitted. It's just a constant so that you can :call matchdelete(9999) to easily remove the highlighting again later, if you want.

See :h matchadd and :h matchdelete

Your Answer

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

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