I like the set colorcolumn=80 option which highlights the column at 80 characters, however I find it annoying when working with multiple split windows.

I'd like to have an option to enable or disable the colored column by using the <leader>c shortcut. I wrote a function, however my Vimscript knowledge is very limited. My only question is do you see anything wrong with this function or approach? or should I go with it?

" Show a colored column at 80 characters
function! ColorColumn()
if &colorcolumn == ""
    set colorcolumn=80
    set colorcolumn=

nmap <silent> <leader>c :call ColorColumn()<CR>

1 Answer 1


The only big flaw I see in your code is the fact that you don't use the non recursive version of nmap (See here for more details).

Also just for the sake to not use a function and make it a one liner I came up with this mapping which should do the same job:

nnoremap <leader>c :execute "set colorcolumn=" . (&colorcolumn == "" ? "80" : "")<CR>

We use execute to execute the command created on the fly and a ternary expression to return "" or 80 depending on the current state of colorcolumn


If you really want to keep it under 80 characters you could use something like this:

nnoremap <silent> <leader>c :execute "set colorcolumn="
                  \ . (&colorcolumn == "" ? "80" : "")<CR>

But I think it's less readable than the one line version.

  • 1
    Thanks. I like your mapping better, but I added the <silent> stuff to hide the confirmation in the status. Commented Oct 5, 2018 at 17:08
  • @ZoltanKing Yup I didn't put the <silent> modifier because it was easier to test the mapping without it but that makes a nice addition :)
    – statox
    Commented Oct 5, 2018 at 17:10
  • statox, sorry for bothering you again. It would be possible to break your code on two lines? I would like to keep it below 80 charachters. Thanks! Commented Oct 5, 2018 at 17:15
  • 3
    @ZoltanKing You're not bothering me: helping other Vimmers is pretty much the only goal of this site :) See my edit for the splitted version.
    – statox
    Commented Oct 5, 2018 at 17:22
  • 1
    Ok, thank you. I reduced the word "colorcolumn" to just "cc" and now the one line fits well. Commented Oct 5, 2018 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.