141

I have seen Vim 80 column layout concerns, but the answer there highlights only actual content that goes over the 80 character mark. I want to have a 100+ column Vim window open with the leftmost 80 columns using the normal background and anything past that using a slightly different background. The Vim window background should be a different color, not just text that goes over the 80 character point. This would indicate how close I am getting to the 80-char point without having to go over it first.

I don't think this is currently possible, but I thought I'd ask just in case.

Maybe it could be done with a plugin?

TextMate Example of Desired Vim Right Margin Highlighting

By selecting "Highlight right margin" in TextMate's general preferences, you can see an example of the desired Vim behavior.

TextMate Right Margin Highlighted Example

Community
  • 1
  • 1
Doug
  • 1,411
  • 2
  • 10
  • 3

6 Answers6

229

If you have Vim >= v7.3, you can simply add this to your .vimrc to highlight 81 and onward (so 80 is your last valid column):

let &colorcolumn=join(range(81,999),",")

If you don't see a highlight, you may not have a ColorColumn highlight color set. Add this (adjust to suit your preferences):

highlight ColorColumn ctermbg=235 guibg=#2c2d27

Now I like to highlight column 80 as well as 120 and onward, so I have separate "warning" and "danger" markers. You can do that thusly:

let &colorcolumn="80,".join(range(120,999),",")

Example

Here's a screenshot of GVim editing my .vimrc.

GVim editing my .vimrc

I use Ubuntu Mono 11 font and the molokai color scheme. You can find my vim config and all of my dotfiles on GitHub.

Justin Force
  • 6,203
  • 5
  • 29
  • 39
  • 3
    colorcolumn is limited to 256 definitions, per `:help colorcolumn`... in addition, my setting seems to be truncated at some length when using your join technique, although I can't be assed to search through vim's source to figure out what that length is. Also, vim parses the value of this setting as a string every time it enters a window, initializes a buffer, etc, so this is kind of an expensive way of achieving the desired result. – cptstubing06 Mar 08 '13 at 04:08
  • I wouldn't call 918 string concatenations expensive. These are computers—they like repetitive tasks. :) If you want to provide the actual bounds, I'd be happy to update my answer. However, I don't think it matters much, so I won't be investigating myself. – Justin Force Mar 16 '13 at 01:00
  • 1
    Thanks for posting this, was exactly what I was looking for! Just one more question, is there a way to not set this rules to a specific type of file (.md)? – David Dias Sep 07 '15 at 16:05
  • 1
    @DavidDias Sure! You can do something like `autocmd FileType markdown let &colorcolumn=""` to unset it completely. – Justin Force Sep 08 '15 at 18:10
  • 1
    updated link to [vim config](https://github.com/justinforce/dotfiles/blob/master/files/nvim/init.vim) – reergymerej Sep 29 '16 at 03:32
  • 1
    @obfk don't touch, if you can avoid it. –  Oct 25 '17 at 12:03
  • 1
    @JustinForce How would you rewrite your solution in "Lua" so that it would work in "Neovim"? – 71GA Apr 13 '22 at 17:45
  • @71GA I wish I knew! This will still work in Neovim (I use it in Neovim), but I do not know how to script Vim with Lua. Or at all, really. I just cobbled this together from examples over time. Good luck out there! – Justin Force Apr 28 '22 at 00:47
81

Highlight 81 column

:set textwidth=80
:set colorcolumn=+1

You may also make some styling:

:hi ColorColumn guibg=#2d2d2d ctermbg=246
Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
Andrew
  • 8,330
  • 11
  • 45
  • 78
  • 2
    This, and it's also possible to have it highlight up to 256 columns, but unfortunately it takes a list of columns to highlight and there's no shorthand for it, so you would have do to something like `:set colorcolumn=+1,+2,+3,+4,+5,+6` etcetera possibly all the way up to `+256` for however wide you think you'd like your right margin. This however will only highlight lines which exist in the buffer, not all lines in the window. – mkomitee Aug 11 '11 at 11:10
  • 3
    @Dyslexic Tangent: Why not highlight the first 80 columns instead? – brice Jan 10 '12 at 10:26
  • 1
    I hadn't thought of that. Set your background to the color you want to use to highlight >80 columns, and set colorcolumn for all columns from 1-80, and set the highlight group for colorcolumn to be the color you want your normal background to be. – mkomitee Jan 11 '12 at 13:20
  • 2
    One question, while I can set the colour properly by typing `:hi colorcolumn ...` it doesn't seem to work when I put it in my .vimrc. Do you know know how to do that? – robbrit Apr 04 '12 at 16:18
  • I'm seeing the same issue as robbrit - If I run the highlight command in ex mode, it looks good, but if I have it in my vimrc file, it destroys my colorscheme... – Bennidhamma Jan 10 '13 at 20:02
  • In vim 7.0 and 7.2, I get “Unknown option: colorcolumn”. It works in vim 7.3. – Vebjorn Ljosa Nov 13 '13 at 17:08
20

Solution proposed in comment above. Highlight your background first then highlight the ColorColumn black as an overlay. Set your colorcolumn to lines 1-80.

hi Normal guibg=#32322f ctermbg=236
hi NonText guibg=#32322f ctermbg=236
hi ColorColumn guibg=#000000 ctermbg=0
let &colorcolumn=join(range(1,80),",")
Conner
  • 30,144
  • 8
  • 52
  • 73
9

try:

:/\%>80v./+

it will mark +80 characters as error

GoTTimw
  • 2,330
  • 6
  • 26
  • 34
  • So clever in a pinch. I like it because it adapts to theme changes. Thanks. – Benjamin Dec 30 '18 at 19:53
  • @GoTTimw Too bad that this can't be applied in a configuration file... – 71GA Apr 19 '22 at 10:10
  • @71GA match Error /\%81v.\+/ thats for older vim in newer i use 'hi ColorColumn guibg=#000000 ctermbg=233 let &colorcolumn="80,".join(range(120,999),",")' to mark single column 80 and then all columns past 120 – GoTTimw May 19 '22 at 14:12
0

I don't think that it's possible to have what you want, but I following this question since I am interested in such a thing myself.

Maybe, I am stating the obvious, but you could setup the highligth in the 70th+ columns to get an indication of how close you get to the 80th column.

René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
0

For those using onedark.vim like me that wants a solution posted by Conner, I added these lines to my vimrc file

let &colorcolumn=join(range(1,80),",")
let g:onedark_color_overrides = {
  \ "black": {"gui": "#2C323C", "cterm": "236", "cterm16": "8"},
  \ "cursor_grey": {"gui": "#282C34", "cterm": "235", "cterm16": "0"}
  \ }

I just swapped the two values, took it from the source code of onedark

yujinyuz
  • 84
  • 1
  • 5