Vim colorcolumn interrupted when using wrap - vim

When using set colorcolumn=80 in conjunction with set wrap, the colored vertical column is interrupted, since wrapped lines are still considered as one single line. I think a solution might be to highlight columns 160, 240, ... as well, but I don't know enough Vimscript to implement this.
Please have a look at the following .gif animation to see what I mean:
Is it possible to display the colorcolumn as a continuous vertical bar?

It doesn't appear that vim provides a way of doing this by default. However,
the colorcolumn option allows you to specify more than one column to highlight. (in a comma seperated list) So I think this should do what you want:
set colorcolumn=80,160,240

Related

It is possible to set color columns with different values in vim?

I know that I can set textwidth in vim so than I can automatically wrap my code (or text) when that value is reached.
I also know that I can color the first column after textwidth limit with set colorcolumn=+1.
Also I know that I can color more columns with set colorcolumn=+1,+2,+3.
Finally I know that I can set the color of the column with hi colorcolumn ctermbg=10.
Can I change the color of each columns with different values?
No, there is only one highlight group named ColorColumn. People have wished for different groups also for folding, but none of that is yet implemented.
You can emulate color columns (but only visible in those lines that have that many columns) via :match. For example:
:2match ErrorMsg /\%40v./
:3match WarningMsg /\%60v./

Highlight multiple positions in VIM independant of the content of the buffer

I would like to highlight certain positions in VIM. The solution should work for empty files.
Ideally, the command should work like this (The form is just to get the idea across):
set colorposition=((12,12),(14,12)), ((1,1),(1,1))
This command would, in this case, highlight (line 12, column 12) to (line 14, column 12), as well as the first position at (line 1, column 1).
One possible solution I found is using the command match.
It works like this:
let us say we would like to color the position in (column 3, line 4). We can use a certain highlight group and the command match:
highlight highlightgroup ctermbg=darkred
match highlightgroup /\%3c\%4l/
Multiple positions can be chained together using the operator \|. Highlighting position 3,4 and 1,1 would be:
match highlightgroup /\%3c\%4l\|\%1c\%1l/
The caveat is that one can only highlight positions inside the existing buffer. If one wants to highlight something at a specific position, where no text exists, the command match will not work.
A related option is available since Vim 7.3. To set the color for a whole column, e.g. 80, one can use colorcolumn.
The command colorcolumn is indifferent to the text in the buffer and works even for empty files, but it only colorizes whole columns, e.g.
set colorcolumn=80
Edit
To clarify what my goals are and to address what has been mentioned by Ingo in the answer section:
I work a lot with Fortran 77. Sometimes fixed form source code can become difficult to handle, if a certain number of IF THEN, ELSE, DO, END DO sections are used. I would like to introduce markings for every level, let's say beginning in the 81st column.
SUBROUTINE SUB(I,J)
C THE COLORCOLUMN IS VISIBLE AT C C
C=0
IF(I .GT. 0) THEN VISUAL_MARK1
IF(J. LT. 1) THEN VISUAL_MARK2
C=2
END IF VISUAL_MARK2
END IF VISUAL_MARK1
WRITE(*,*) I,J,C
END SUBROUTINE
Why would you highlight cells where no text exists?
Because Vim is a text editor, that's not supported. As you've found out, :match only highlights matching, i.e. existing text. The 'colorcolumn' is an aid for not exceeding a certain width, and as such is visible in all lines. :set virtualedit=all allows to you address non-existing positions with the cursor, but that doesn't highlight anything. The only ugly workaround I can think of is adding actual whitespace to the buffer to match those positions (and then removing them on :write).

Color background in Macvim editor

Basically, The background behind the line numbers are darker than the background behind the code, as you can see from the picture below. The background behind the codes has a slightly lighter color than that behind the line numbers. I hope I describe this well enough for you to understand.
I am just wondering what code should I write onto .vimrc file in order to capture the same effect ?
You will need to put the command highlight in your .vimrc followed by certain arguments. To learn about the arguments that highlight takes, type :help :highlight and press Return. This will open the help for the highlight command.
This is the particular format of highlight that you want:
:hi[ghlight] [default] {group-name} {key}={arg} ..
Add a highlight group, or change the highlighting for
an existing group.
See |highlight-args| for the {key}={arg} arguments.
See |:highlight-default| for the optional [default]
argument.
You can find the correct {group-name} to use with :help highlight-groups. As romainl said, the group names for the line numbers at the left are LineNr and CursorLineNr.
And what to put for the {key}={arg} arguments? Well, check :help highlight-args as the help text suggested. The key represents what aspect of the text should be changed (e.g. italic or not, or the foreground color), and the arg represents what it should be changed to (e.g. italic, or bright red). In your case, you want to change the background color, which is controlled by ctermbg for terminals and guibg for GUIs.
Run :highlight Normal to learn the background color you want. You should see something like guibg=grey10. So try setting the guibg (background color in the GUI) of your line numbers to the color grey10 in your .vimrc:
highlight LineNr guibg=grey10
highlight CursorLineNr guibg=grey10
Edit
It sounds like you want to change the code background color, not the line number background color. To do that, write the same command, but use the highlight group that represents normal code instead of the LineNr and CursorLineNr highlight groups that represent line numbers. If you look at highlight-groups help, you can see that the highlight group for code in general is called Normal. So write
highlight Normal guibg=grey
, changing grey to whatever color you want.
If you aren’t sure what color you want, and you just know that you want it to be the same as some other color, run :highlight {group-name} to look up the settings for that group name, and look for the background color key within it. For example, if you want to make the code background color the same as the line number background color, run :highlight LineNr. You will see something like guibg=#242424, which would mean to use #242424 as the color.
You can find LineNr and CursorLineNr listed under :help highlight-groups.
You will need to add/change the corresponding lines in your colorscheme.

highlight fixed width block background color in vim

I have an input file for a fortran code that needs input text in specific lines and within specific columns. I'd like to highlight the background of these fields when I type them in Vim.
I'm able to either specify a set of rows by
:highlight row ctermbg=green guibg=green
:match row /\%>5l.\%<9l/
or a specific set of columns by
:highlight col ctermbg=grey guibg=grey
:match col /\%>40c.\%<50c/
Is there a way to specify the row and column width for each field and highlight it a different color?
You can combine the line and column restrictions to highlight a block:
:match block /\%>5l\%>3c.\%<8c\%<9l/
Note that \%c matches byte indices, not actual characters. Unless your Fortran code can only contain printable ASCII characters without <Tab>, you'd better match the screen width with \%v (what Vim calls virtual column).
For different matches, you have :match, :2match, and :3match. These are meant for interactive use; if you want to add the highlighting via a mapping, custom command, or autocmd, you should prefer the matchadd() / matchdelete() functions. They are slightly more involved to use, (you need to store the returned IDs to be able to delete them later), but you can use an arbitrary number of them.

Limiting text width does not work as expected if not inserting at the end of a line

I know we can use :set tw=80 to limit the text width. However, if we insert text before the end, this function doesn't work at all.
For example, let's say we type "Would you like to have responses to your questions sent to you via email?"
If we continue to input after "?", tw works fine. But if we insert before say "have", it doesn't break off the line even if it exceeds the specified text width.
Is there any way to make this work in the latter case?
You may try
:set fo+=a
which reformats your paragraph as you type.
See :h fo-table.
This might be configurable using formatoptions or formatexpr, but I'm not sure how.
Another solution is to manually format using gq on a visual line selection i.e. add a breakline if the text width is >= tw.
Type in whatever text you like inside another block of text, thus making it 80 chars or more in width
Select the text you think is misformatted using V (Shiftv)
gq
From :h gq:
Format the lines that {motion} moves over.
Formatting is done with one of three methods:
1. If 'formatexpr' is not empty the expression is
evaluated. This can differ for each buffer.
2. If 'formatprg' is not empty an external program
is used.
3. Otherwise formatting is done internally.
In the third case the 'textwidth' option controls the
length of each formatted line (see below).
If the 'textwidth' option is 0, the formatted line
length is the screen width (with a maximum width of
79).
The 'formatoptions' option controls the type of
formatting fo-table.
[..]
For more information on how Vim's textwidth works, take a look at
How to use Vim’s textwidth like a pro

Resources