19

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.

Paul Beckingham
  • 13,460
  • 5
  • 30
  • 67

6 Answers6

32

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

Community
  • 1
  • 1
  • To avoid highlighting the end of line character, you should change the regex to: `/\%>74v.\+/` http://stackoverflow.com/questions/235439/vim-80-column-layout-concerns#comment2448229_235970 – Wex May 08 '14 at 19:09
  • This is working somewhat better but if I do a `:vsplit` the old buffer no longer has the highlight, any ideas? – hakunin Feb 15 '20 at 07:11
  • 1
    I fixed the split issue by adding `WinEnter`. – hakunin Feb 15 '20 at 07:22
  • 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 – Gustavo Adolfo Mejía Dec 02 '20 at 22:51
16

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.

Community
  • 1
  • 1
HS.
  • 2,505
  • 2
  • 20
  • 28
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

SystematicFrank
  • 14,622
  • 5
  • 51
  • 95
1

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.

terminus
  • 12,035
  • 8
  • 30
  • 36
  • Thank you. This (/^.\{120\}.*$/) highlights the whole line - any idea about just highlighting from characters 121 onwards? – Paul Beckingham Dec 27 '08 at 16:58
  • Sorry, no idea. The problem is that it may not be doable with regexps. You know, it would propably require a stack machine. – terminus Dec 27 '08 at 17:07
  • 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 Dec 27 '08 at 17:28
1

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
cmcginty
  • 101,562
  • 37
  • 148
  • 155
0

I love the other answers, but also wanted to be able to toggle them on and off. I don't really know what I'm doing, but after much wrestling with it this morning, I have something that seems to work:

highlight OverLength ctermbg=darkgrey ctermfg=white guibg=#292929

fun! LongLineHighlightInit()
    if !exists("w:llh")
        call LongLineHighlightOn()
    endif
endfunction

fun! LongLineHighlightOn()
    let w:llh = matchadd("OverLength", '\%80v.')
endfunction

fun! LongLineHighlightOff()
    call matchdelete(w:llh)
    let w:llh = 0
endfunction

fun! LongLineHighlightToggle()
    if !exists("w:llh") || w:llh == 0
        call LongLineHighlightOn()
    else
        call LongLineHighlightOff()
    endif
endfunction

augroup LongLineHighlight
    autocmd BufWinEnter * call LongLineHighlightInit()
augroup end

nnoremap <silent> <Leader>8 :call LongLineHighlightToggle()<CR>

Using matchadd and matchdelete (instead of plain 'match') means the highlights don't interfere with anything else that uses match highlighting.

But it also requires that we store the returned match group IDs at a scope which matches the highlighting mechanism, which turns out to be window scope (i.e. two windows on the same buffer can independently use match/matchadd/matchdelete to set different highlights). Hence the "w:" scope variable.

When a new window is created (detected by BufWinEnter event, then checking to see the 'w:llh' variable is undefined), we turn the highlight on by default.

Jonathan Hartley
  • 13,922
  • 8
  • 71
  • 77