Make Vim display a line at the edge of the set textwidth

26

8

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.

Herms

Posted 2009-08-13T19:24:11.643

Reputation: 7 644

Answers

43

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.

lornix

Posted 2009-08-13T19:24:11.643

Reputation: 9 633

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 – 2015-03-16T16:28:13.077

What version of Vim is this supported in? It's not working for me in gvim 7.2. – Herms – 2010-11-05T16:53:51.390

I'm running 7.3. I just pulled the source code and looked, this command was introduced in 7.3. – lornix – 2010-11-05T17:18:56.527

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 – 2013-01-03T15:40:05.557

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

mcandre

Posted 2009-08-13T19:24:11.643

Reputation: 2 696

This doesn't adapt to textwidth like requested in the question. – Steven Roose – 2018-09-04T10:11:50.610

Hmm, that thread suggests that windows' gvim already supports it, but I can't figure out how to turn it on. – Herms – 2009-08-13T19:41:17.500

There is some code to add to your gvimrc file futher down in the thread. – EBGreen – 2009-08-13T19:46:20.843

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 – 2009-08-13T20:11:30.563

Due to the nature of the gui I'm not positive that is currently possible. – EBGreen – 2009-08-13T20:14:23.397

2

Per a StackOverflow answer:

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

Adjust to suit your taste.

jtimberman

Posted 2009-08-13T19:24:11.643

Reputation: 20 109

This doesn't adapt to textwidth like requested in the question. – Steven Roose – 2018-09-04T10:12:08.013

Hm, not quite what I'm looking for (only displays when characters go past the line), but it's better than nothing. – Herms – 2009-08-13T20:05:43.183

1

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

Matthias Braun

Posted 2009-08-13T19:24:11.643

Reputation: 572

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.

Rook

Posted 2009-08-13T19:24:11.643

Reputation: 21 622