8

I once was given these materials to enable this helper for vim to show kind of a vertical line in order to help enforce PEP8 rules on line length limit at column 80:

"   Long lines highlighting.
nnoremap <Leader>H :call<SID>LongLineHLToggle()<cr>
hi OverLength ctermbg=none cterm=none
match OverLength /\%>80v/
fun! s:LongLineHLToggle()
    if !&diff
        if !exists('w:longlinehl')
            let w:longlinehl = matchadd('ErrorMsg', '.\%>80v', 0)
            echo "Long lines highlighted"
        else
            call matchdelete(w:longlinehl)
            unl w:longlinehl
            echo "Long lines unhighlighted"
        endif
    endif
endfunction

In action:

long line highlighter toggler in action

I use all of this for marking the 80th column.

Given enough time and effort I very much could have a similar facility in vim for marking text far beyond column 72 only when in comment. I guess it has been already coded by somebody somewhere at some point. Do you already know where to find that snippet?

  • You can find out if something is highlighted as a comment with synID() and synIDattr(). Something like: if ((synIDattr(synID(line("."), 72, 0), "name") =~ 'comment\c') – Tumbler41 Jul 12 '17 at 23:08
  • Am I supposed to link that to a nnoremap or autocmd or something? Writing that to the : console threw E110 and E15 🙁. – john Jul 15 '17 at 0:50
10
+50

Try a contained syntax region:

syn match TooLong contained containedin=pythonComment /\%50c.*/
hi link TooLong Error

Instead of 50, use 80 or 72 or whichever column you prefer.

enter image description here

  • Which vim version is that? – john Jul 13 '17 at 8:17
  • 1
    8.0.586, using molokai as the colorscheme. – muru Jul 13 '17 at 9:05
  • Like vim --version presenting VIM - Vi IMproved 8.0.586 ...? Am not sure of what am doing, but I think is not working for me as yours. Mine is telling VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Mar 10 2017 00:54:29) ..., that is the vanilla VIM of Debian Jessie. – john Jul 13 '17 at 9:23
  • There's nothing particularly new in this, all features I'm using here are pretty old, and should work with Jessie's 7.4.488 (because this works on CentOS 7's Vim 7.4.160 as well). Say, did you test this in a Python file? Since you mentioned PEP8, I used the containedin=pythonComment. If you're using some other filetype, you'll have to look up what the syntax region for comments is and use that instead. – muru Jul 13 '17 at 9:29
  • 2
    @uprego Run those commands after you open a Python file. Simply adding those lines to a vimrc won't work, since syntax files loaded later would likely clear any current rules. Use an autocmd or ~/.vim/after/syntax/python.vim. – muru Jul 13 '17 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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