92

You can display a ruler at a specific line using the :set colorcolumn (:set cc for short) option which is only available in Vim 7.3 or later. set colorcolumn=80 This will set the background color of that column to red, giving you a visual ruler to work from. If you want to use a different color other than red (which really stands out), you can change the ...


24

As an addendum to Kevin's answer, you can have multiple colorcolumns. When I code, I sometimes have a "soft" limit at 80 columns and a "hard" limit at 120 columns. So I want a line at 80, and then every column after 120 to be colored. I do this with let &colorcolumn="80,".join(range(120,999),",") Of course, this can be easily modified to other ...


10

You can clear all the lines by just setting cc to an empty value like so: :set cc= You also can create a command or mapping to toggle between the two, &cc refers to the cc setting, we need to use let since we want to use an expression: command! ToggleCC :let &cc = &cc == '' ? '1,4,21' : '' nnoremap <F2> :let &cc = &cc == '' ? '1,...


9

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>...


4

You're on the right track, you just need to add the same function to the BufEnter autocmd: set textwidth=0 function! s:SetColorColumn() if &textwidth == 0 setlocal colorcolumn=80 else setlocal colorcolumn=+0 endif endfunction augroup colorcolumn autocmd! autocmd OptionSet textwidth call s:SetColorColumn() ...


4

The solution I'm using nowadays is: matchadd('ColorColumn', '\%81v\s*\zs\S', 100) That version highlights the first non-space character after column 80, which I find more useful.


4

After digging around a bit on the internet, I found this page which achieves a similar effect but is more specific in the match. The fix is to add a . after the v. call matchadd('ColorColumn', '\%81v.', 100)


3

One strange thing about vimscript is that it automatically converts string to numbers when some operations are applied. For example, if you try this: if '123foo' echo 'hi' endif You will get 'hi' as the output, but if you try this: if 'foo' echo 'hi' endif You will get nothing. This happens because the condition of an 'if' sentence has to be an ...


2

Well, doing this in vimscript is not that hard. For example, the following command will do what you describe to the line your cursor is on: :exec 'norm '.(&cc - strlen(getline('.'))).'A=' The way this works is it determines the length of the current line and subtracts that from the current value of 'colorcolumn'. This evaluates to a string, and is ...


2

I mostly do coding in vim (version 7.4) and I have added the below two lines in my .vimrc to maintain a "colorcolumn" for 100 character length. :set colorcolumn=100 :hi ColorColumn ctermbg=lightcyan guibg=blue My environment is a color-xterm so I usually populate the "ctermbg" ...


1

How can I a) discover that I have more than one value, and b) access that 2nd value? a). By checking if the option contains a comma. I'd do so with a regular expression comparison. See :help =~: if &cc =~ ',' echo 'Multiple colorcolumns' endif b). By splitting the option's value on the comma(s). See :help split(): for cc in split(&cc, ',') ...


1

What you are expected to do: set cc=+0 Why your stuff doesn't work: because ordinary :set is executed once, while :autocmd FileType is executed upon each FileType event. Note: your autocmd has all possible "novice bugs" in it. Before doing any stuff with auto-commands you're strongly advised to learn the docs. In particular, always use augroup; ...


1

The colorcolumn setting accepts multiple values, for example set colorcolumn=30,60 will highlight column 30 and 60. You can use this to display a single unbroken line for wrapped lines; for example for a 130-column line on a 40 column display you would use set colorcolumn=30,70,110 to highlight the first three columns. You can set this automatically with a ...


1

I can't help but feel that you are trying to limit the colorcolumn to the 81st (or so) column only when there actually is a line that goes beyond that column. I'm sorry, if I got your question wrong, because I'm a bit confused about rows vs columns... Anyway: this is what I have in my .vimrc: " Make 81st column stand out highlight ColorColumn ctermbg=...


1

Hightlight first and last 2 lines after column 80: :autocmd VimEnter,WinEnter * :match Error /\v%>80v%<3l.*|%>80v.*%$|%>80v.*(\n.*%$)@=/ You need to open a new window or reopen vim for this to work. Explanation: %>80v%<3l.* match everything after column 80 in first 2 lines %>80v.*%$ match everything after column 80 in last line %&...


Only top voted, non community-wiki answers of a minimum length are eligible