Vim syntax coloring: How do I highlight long lines only?
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
Related videos on Youtube
Comments
-
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 over 15 years+1. Great question! Now, I have no clue about the answer, but I'll stay tuned.
-
Martin Ueding over 11 yearsMaybe colorcolumn might be something for you.
-
-
Paul Beckingham over 15 yearsThank you. This (/^.\{120\}.*$/) highlights the whole line - any idea about just highlighting from characters 121 onwards?
-
terminus over 15 yearsSorry, no idea. The problem is that it may not be doable with regexps. You know, it would propably require a stack machine.
-
PEZ over 15 yearsYou can highlight only the 80 first chars. I know, not exactly what you want, but you'll see the excess quite clearly anyway.
-
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 almost 15 yearsThis only works for the first file you open in any given buffer
-
Eric Hu over 11 years@BrandonThomson is there a way around that? Or an alternative way?
-
Mu Mind over 11 years@EricHu yep, that's what ciro's answer does.
-
Yep_It's_Me about 10 yearsI was just looking for a quick way to toggle that. Your plugin is awesome.
-
Yep_It's_Me about 10 yearsThough I would like it if switching tabs didn't re-enable the highlighting if I had it turned off.
-
SystematicFrank about 10 yearsI 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 about 10 yearsThanks. BufRead fixed it.
-
Wex almost 10 yearsTo avoid highlighting the end of line character, you should change the regex to:
/\%>74v.\+/
stackoverflow.com/questions/235439/… -
hakunin about 4 yearsThis is working somewhat better but if I do a
:vsplit
the old buffer no longer has the highlight, any ideas? -
hakunin about 4 yearsI fixed the split issue by adding
WinEnter
. -
Gustavo Adolfo Mejía over 3 yearsI'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 thisautocmd Filetype * match OverLength //
between the two lines -
Lohmar ASHAR almost 3 yearsThis 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 over 2 yearsThis is exactly what I'm looking for. The way you set a variable per window is also a good trick to learn. Thank you!