How to show vertical line to wrap the line in Vim?
Solution 1
Edit: For Vim >=7.3 see answer below.
Unfortunately vim has no mechanism to display a vertical line after a column like you want (unlike, say, TextMate). However, there are alternative visual indicators that you can use to show that a line is too long.
Here's what I use (you can put this in your .vimrc
):
nnoremap <Leader>H :call<SID>LongLineHLToggle()<cr>
hi OverLength ctermbg=none cterm=none
match OverLength /\%>80v/
fun! s:LongLineHLToggle()
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
endfunction
So then you can use <Leader>H
to toggle columns over 80 being highlighted.
Solution 2
New in Vim 7.3:
'colorcolumn' is a comma separated list of screen columns that are highlighted with ColorColumn. Useful to align text. Will make screen redrawing slower. The screen column can be an absolute number, or a number preceded with '+' or '-', which is added to or subtracted from 'textwidth'.
Example from the docs:
:set colorcolumn=+1 " highlight column after 'textwidth'
:set colorcolumn=+1,+2,+3 " highlight three columns after 'textwidth'
:highlight ColorColumn ctermbg=lightgrey guibg=lightgrey
You can use absolute numbers as well:
:set colorcolumn=80
Solution 3
There is another way to notify about the long line.
highlight OverLength ctermbg=red ctermfg=white guibg=#592929 <br>
match OverLength /\%81v.*/
Solution 4
I use match ErrorMsg '\%>80v.\+'
which will highlight anything over 80 chars with red.
I put that command in my python.vim and ruby.vim under ~/.vim/after/ftplugin/.
Solution 5
Several answers here http://vim.wikia.com/wiki/Highlight_long_lines simple autocommand
:au BufWinEnter * let w:m1=matchadd('Search', '\%<81v.\%>77v', -1)
:au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)
jennifer
Updated on February 15, 2021Comments
-
jennifer about 3 years
I'm interested in finding a way to show a vertical line at column 80 in Vim (not GVim).
I've used
set wrap
, but I just want to show a vertical line so I can wrap the long line myself.-
Trevor Boyd Smith over 7 yearsPossible duplicate of Vim 80 column layout concerns
-
-
Alex Hart over 11 yearsI think it's good to note here the color is automatically determined by your highlight color unless you manually set it as in the example.
-
c4urself about 11 yearsNote the
highlight
setting must be set after anycolorscheme
commands as that would override your highlight color. -
Ron Dahlgren about 11 yearsI went with bright, burn-your-brain-red... 'cause you know... line length
-
thulashi almost 11 yearsThe vim script representation of the leader key. See Show current <leader> key setting
-
Alice over 10 yearsDo you know of a way to have two different colors for the colorcolumn? I'd like to have two: a plain one at 80 chars for code, and a very faint one at 72 chars for flowing text/comments.
-
chicks over 9 yearsIt would be good to mention that
textwidth
also causes vim to wrap when you are typing. -
Markus Joschko over 8 years@chutsu The Vim color chart is at: codeyarns.com/2011/07/29/vim-chart-of-color-names
-
jazzabeanie over 7 years@Will, Learn Vimscript the Hard Way is a good resource to learn about the vim rabbit hole.
-
Leonid Vasilev over 3 yearsColumns in Vim have one-based indices, so if you want highlight column after max text width using absolute numbers it should be
set colorcolumn=81
-
aerijman over 2 yearsThen
set colorcolumn=0
to hide it