Vim syntax coloring: How do I highlight long lines only?

11,412

Solution 1

I needed the autocomand to work for me:

augroup vimrc_autocmds
  autocmd BufEnter * highlight OverLength ctermbg=darkgrey guibg=#111111
  autocmd BufEnter * match OverLength /\%75v.*/
augroup END

Also like the idea of using 75 if you are aiming at 80 columns in average.

Taken from:

http://blog.ezyang.com/2010/03/vim-textwidth/

Possible reason why it fails without BufEnter: highlight + match can only be used once. Multiple usage means that old ones are overridden. How to add multiple highlights

Solution 2

I have this in my vimrc.
I found it here: Vim 80 column layout concerns

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

You might want to adjust the colors to your preferences.

Solution 3

Since I do not like the Vim 7.3 column marker, I just use the highlight text after column 80... at least that is what I want 95% of the time.

For the other 5% of the time, I wrote this small extension to also have a quick way to disable the highlight:

https://gist.github.com/fgarcia/9704429#file-long_lines-vim

Solution 4

I use the following method:

hi gitError ctermbg=Red
match gitError /^.*\s$/
2match gitError /^.\{120\}.*$/

(These match some git pre-commit hooks)

The second line should be of interrest to you.

Solution 5

This uses an autocommand to adjust the OverLength value to match your file type.

" highlight lines longer than `textwidth` size for each filetype
autocmd FileType *
    \ if &textwidth |
    \    exec 'match OverLength /\%' . string(&textwidth+2) . 'v.*/' |
    \ endif
Share:
11,412

Related videos on Youtube

Paul Beckingham
Author by

Paul Beckingham

Typist. Perishable vertebrate.

Updated on April 19, 2022

Comments

  • Paul Beckingham
    Paul Beckingham about 2 years

    I would like vim to color "long" lines for me. Using 80 columns as an example, I would like to highlight lines that exceed that length. Here is roughly what I think the .vimrc file should contain, although it (1) doesn't work, and (2) uses Perl's regex syntax to illustrate my point, because I don't know Vim's well enough:

    ...
    highlight Excess ctermbg=0
    au Syntax * syn match Excess /.{80,}$/
    ...
    

    This (in my mind at least) should mark lines that exceed 80 columns. What I would ideally like is the ability to color only the part of the line that exceeds 80 columns, so if a line is 85 columns, then the 81st through the 85th columns would be highlighted.

    I'm sure Vim can do this, just not with me at the helm.

    • PEZ
      PEZ over 15 years
      +1. Great question! Now, I have no clue about the answer, but I'll stay tuned.
    • Martin Ueding
      Martin Ueding over 11 years
      Maybe colorcolumn might be something for you.
  • Paul Beckingham
    Paul Beckingham over 15 years
    Thank you. This (/^.\{120\}.*$/) highlights the whole line - any idea about just highlighting from characters 121 onwards?
  • terminus
    terminus over 15 years
    Sorry, no idea. The problem is that it may not be doable with regexps. You know, it would propably require a stack machine.
  • PEZ
    PEZ over 15 years
    You can highlight only the 80 first chars. I know, not exactly what you want, but you'll see the excess quite clearly anyway.
  • PEZ
    PEZ over 15 years
    +1.Now, if someone could explain why that works that would help me a lot because I don't get it.
  • gravitation
    gravitation almost 15 years
    This only works for the first file you open in any given buffer
  • Eric Hu
    Eric Hu over 11 years
    @BrandonThomson is there a way around that? Or an alternative way?
  • Mu Mind
    Mu Mind over 11 years
    @EricHu yep, that's what ciro's answer does.
  • Yep_It's_Me
    Yep_It's_Me about 10 years
    I was just looking for a quick way to toggle that. Your plugin is awesome.
  • Yep_It's_Me
    Yep_It's_Me about 10 years
    Though I would like it if switching tabs didn't re-enable the highlighting if I had it turned off.
  • SystematicFrank
    SystematicFrank about 10 years
    I think that should be solved by using BufRead (maybe BufAdd??) instead the BufEnter in the first lines. Let me know if that works for you
  • Yep_It's_Me
    Yep_It's_Me about 10 years
    Thanks. BufRead fixed it.
  • Wex
    Wex almost 10 years
    To avoid highlighting the end of line character, you should change the regex to: /\%>74v.\+/ stackoverflow.com/questions/235439/…
  • hakunin
    hakunin about 4 years
    This is working somewhat better but if I do a :vsplit the old buffer no longer has the highlight, any ideas?
  • hakunin
    hakunin about 4 years
    I fixed the split issue by adding WinEnter.
  • Gustavo Adolfo Mejía
    Gustavo Adolfo Mejía over 3 years
    I'm using this Overlength for specific filetypes like this autocmd Filetype c,python highlight... autocmd Filetype c,python match... but I had problem when i open another file with :e or :find or :NERDTree My solution was setting a default match with this autocmd Filetype * match OverLength // between the two lines
  • Lohmar ASHAR
    Lohmar ASHAR almost 3 years
    This works, I have it in my .vimrc. My question is ... how can I temporarily turn it off ?! I hate it when I'm editing minimized js files.
  • ngovanmao
    ngovanmao over 2 years
    This is exactly what I'm looking for. The way you set a variable per window is also a good trick to learn. Thank you!