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.
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 ...
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
Of course, this can be easily modified to other ...
You can clear all the lines by just setting cc to an empty value like so:
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,...
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>...
You're on the right track, you just need to add the same function to the BufEnter autocmd:
if &textwidth == 0
autocmd OptionSet textwidth call s:SetColorColumn()
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)
One strange thing about vimscript is that it automatically converts string to numbers when some
operations are applied.
For example, if you try this:
You will get 'hi' as the output, but if you try this:
You will get nothing.
This happens because the condition of an 'if' sentence has to be an ...
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 ...
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.
:hi ColorColumn ctermbg=lightcyan guibg=blue
My environment is a color-xterm so I usually populate the "ctermbg" ...
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'
b). By splitting the option's value on the comma(s). See :help split():
for cc in split(&cc, ',')
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; ...
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 ...
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...
this is what I have in my .vimrc:
" Make 81st column stand out
highlight ColorColumn ctermbg=...
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.
%>80v%<3l.* match everything after column 80 in first 2 lines
%>80v.*%$ match everything after column 80 in last line