for (g)vim, use the following:
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.
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
Per a StackOverflow answer:
highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/
Adjust to suit your taste.
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:
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
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.