126

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.

Machavity
  • 28,730
  • 25
  • 78
  • 91
jennifer
  • 1,263
  • 2
  • 9
  • 4

5 Answers5

324

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
UncleZeiv
  • 17,080
  • 6
  • 46
  • 74
  • 5
    I 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. – Alex Hart Sep 18 '12 at 12:48
  • 1
    Note the `highlight` setting must be set _after_ any `colorscheme` commands as that would override your highlight color. – c4urself Feb 15 '13 at 18:00
  • 8
    I went with bright, burn-your-brain-red... 'cause you know... line length – Ron Dahlgren Apr 30 '13 at 01:26
  • Do 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. – Alice Dec 05 '13 at 17:14
  • 1
    It would be good to mention that `textwidth` also causes vim to wrap when you are typing. – chicks Jan 19 '15 at 23:07
  • 1
    @chutsu The Vim color chart is at: http://codeyarns.com/2011/07/29/vim-chart-of-color-names/ – Ashwin Nanjappa Sep 11 '15 at 01:04
  • 1
    Columns 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` – Leonid Vasilev Sep 18 '20 at 08:16
17

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.

Community
  • 1
  • 1
Sam
  • 750
  • 2
  • 10
  • 19
  • The vim script representation of the leader key. See [Show current key setting](http://stackoverflow.com/questions/10389205/show-current-leader-key-setting) – Rod Jun 06 '13 at 13:27
  • @Will, [Learn Vimscript the Hard Way](http://learnvimscriptthehardway.stevelosh.com/) is a good resource to learn about the vim rabbit hole. – jazzabeanie Sep 13 '16 at 05:52
14

There is another way to notify about the long line.

highlight OverLength ctermbg=red ctermfg=white guibg=#592929 <br>
match OverLength /\%81v.*/

Vim 80 column layout concerns

Stefan
  • 1,113
  • 9
  • 19
Brian
  • 1,565
  • 5
  • 17
  • 24
8

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/.

Pierre-Antoine LaFayette
  • 23,202
  • 8
  • 49
  • 57
4

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)
michael
  • 10,929
  • 2
  • 24
  • 25