34

Most of the text editors and IDEs that I use these days have a feature where they can display a line at a certain character length in the text buffer. It's useful when you want to keep lines in a file under a certain length.

Is there any way to get Vim to do this, preferably using the already defined textwidth value? Lines will automatically wrap at that point, but I'd really like to be able to see where it is as well.

In case it matters, I'm mainly using gVim on Windows, but I'd love it if the solution works across Vim versions.

5 Answers 5

55

for (g)vim, use the following:

set colorcolumn=80

or whatever width you wish. Works in both vim & gvim. I have mine within an IF so it's conditional based on which type of files I edit.

You may also use a +x/-x to base position of the column +/- from &textwidth.

set textwidth=80
set colorcolumn=-2

would effective draw the colored bar at char position 78. Of course, you may or may not set textwidth yourself, so it might be 0 (default). I use the absolute position form.

You may also change the color used if you wish:

highlight ColorColumn ctermbg=green guibg=orange

(I don't recommend THOSE colors though)

This option was added in (g)vim 7.3.

4
  • What version of Vim is this supported in? It's not working for me in gvim 7.2.
    – Herms
    Nov 5, 2010 at 16:53
  • I'm running 7.3. I just pulled the source code and looked, this command was introduced in 7.3.
    – lornix
    Nov 5, 2010 at 17:18
  • Works for me on Ubuntu 12.04 with vim 7.3. gvim 7.3 for windows has been available since at least October of 2010. @Herms can you make this the answer?
    – poindexter
    Jan 3, 2013 at 15:40
  • It would be nice if this worked when using multiple windows side-by-side. My monitor is wide enough to show several windows in a row, each one > 80 chars but colorColumn only works on the first (leftmost) window.
    – Eno
    Mar 16, 2015 at 16:28
3

There's a snippet at Google Code you can try:

augroup vimrc_autocmds
au!
    autocmd BufRead * highlight OverLength ctermbg=red ctermfg=white guibg=#592929 
    autocmd BufRead * match OverLength /\%81v.*/
augroup END
5
  • Hmm, that thread suggests that windows' gvim already supports it, but I can't figure out how to turn it on.
    – Herms
    Aug 13, 2009 at 19:41
  • There is some code to add to your gvimrc file futher down in the thread.
    – EBGreen
    Aug 13, 2009 at 19:46
  • seems like that just changes the background for characters that go beyond that line. I'd rather have an always-visible guide like other editors have.
    – Herms
    Aug 13, 2009 at 20:11
  • Due to the nature of the gui I'm not positive that is currently possible.
    – EBGreen
    Aug 13, 2009 at 20:14
  • This doesn't adapt to textwidth like requested in the question. Sep 4, 2018 at 10:11
3

I like lornix' answer a lot but I don't want to highlight the column all the time, only when at least one line exceeds the length limit:

showing column when lines too long

Here's how I do it for my Haskell files:

augroup HaskellCommands
autocmd!
  " When a Haskell file is read or the text changes in normal or insert mode,
  " draw a column marking the maximum line length if a line exceeds this length
  autocmd BufRead,TextChanged,TextChangedI *.hs call ShowColumnIfLineTooLong(80)
augroup END

" Color the column marking the lengthLimit when the longest line in the file
" exceeds the lengthLimit
function! ShowColumnIfLineTooLong(lengthLimit)
  " See https://stackoverflow.com/questions/2075276/longest-line-in-vim#2982789
  let maxLineLength = max(map(getline(1,'$'), 'len(v:val)'))

  if maxLineLength > a:lengthLimit
    highlight ColorColumn ctermbg=red guibg=red
    " Draw the vertical line at the first letter that exceeds the limit
    execute "set colorcolumn=" . (a:lengthLimit + 1)
  else
    set colorcolumn=""
  endif
endfunction
2

Per a StackOverflow answer:

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

Adjust to suit your taste.

2
  • Hm, not quite what I'm looking for (only displays when characters go past the line), but it's better than nothing.
    – Herms
    Aug 13, 2009 at 20:05
  • This doesn't adapt to textwidth like requested in the question. Sep 4, 2018 at 10:12
0

This was often discussed on #vim, and in some forums. As far as things stand now, it is not possible. So the mentioned solution is your only option, afaik.

The thing is, vim can do anything with places where there are characters (them being letters, numbers or just plain whitespace). But it cannot paint a background in a different colour (like you would like), if there is nothing there. And before you type something, there isn't anything there, so it cannot draw a line/margin.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .