77

Is there any way to make Vim (or gVim, or both) highlight the right margin of the current buffer?

I have just begun to work with Vim for a while, and find it annoying not to have the right margin visible (say, at column 80).

ib.
  • 25,324
  • 10
  • 71
  • 97
Ha-Duong Nguyen
  • 1,333
  • 1
  • 14
  • 22

4 Answers4

122

Vim 7.3 introduced colorcolumn.

:set colorcolumn=80

It may be easier for you to remember the short form.

:set cc=80
Bruno Bronosky
  • 54,357
  • 9
  • 132
  • 120
wilhelmtell
  • 53,297
  • 19
  • 89
  • 128
  • 2
    Take a look at the second answer here: http://stackoverflow.com/questions/235439/vim-80-column-layout-concerns for a compatibility improvement. – mgold Mar 18 '12 at 02:26
  • 1
    @mgold never reference an answer by number. The order changes based on votes. Alway use the share link. – Bruno Bronosky Mar 26 '15 at 00:10
  • 1
    5 months later I have Googled for this again and again found the link above to be annoying. Here is the answer he is talking about http://stackoverflow.com/a/3765575/117471 which shows how to make your vimrc compatible with both vim >=7.3 and <7.3 – Bruno Bronosky Aug 05 '15 at 14:44
19

There is no simple way to visualize a vertical edge for the textwidth-margin in Vim 7.2 or earlier; starting with version 7.3, there is dedicated colorcolumn option. However, one can highlight all characters beyond the 80-column limit using the :match command:

:match ErrorMsg /\%>80v.\+/

All we need to make it a general solution, is to build the match pattern on the fly to substitute the correct value of the textwidth option:

:autocmd BufWinEnter * call matchadd('ErrorMsg', '\%>'.&l:textwidth.'v.\+', -1)
ib.
  • 25,324
  • 10
  • 71
  • 97
7

I've written a vimscript function in my .vimrc to toggle colorcolumn when I press ,8 (comma followed by 8, where comma is the defined leader for user-defined commands, and eight is my mnemonic key for 'show a margin at the 80th column):

" toggle colored right border after 80 chars
set colorcolumn=81
let s:color_column_old = 0

function! s:ToggleColorColumn()
    if s:color_column_old == 0
        let s:color_column_old = &colorcolumn
        windo let &colorcolumn = 0
    else
        windo let &colorcolumn=s:color_column_old
        let s:color_column_old = 0
    endif
endfunction

nnoremap <Leader>8 :call <SID>ToggleColorColumn()<cr>
Jonathan Hartley
  • 13,922
  • 8
  • 71
  • 77
2

I've rewritten the answer of Jonathan Hartley for the older Vim versions like 7.2 as there is no colorcolumn in older Vims.

highlight OverLength ctermbg=red ctermfg=white guibg=#592929

let s:OverLengthToggleVariable=0

function! ToggleOverLength()
        if s:OverLengthToggleVariable == 0
                match OverLength /\%81v.\+/
                let s:OverLengthToggleVariable=1
        else
                match OverLength //
                let s:OverLengthToggleVariable=0
        endif
endfunction

" I like <leader>h since highlight starts with h.                                                                       
nnoremap <leader>h :call ToggleOverLength()<cr>
Community
  • 1
  • 1
Mateusz Piotrowski
  • 6,087
  • 9
  • 44
  • 71