vim left and right column highlight - vim

I need to highlight anything beyond column 72 with a different background color. What's the best method? Something similar to a visible line margin that some editors do is also good. Is it possible?
Some background:
The syntax is a Pascal like, and works correctly.
Only the Background needs to change. The foreground is already highlighted as it should be. There are cases where a String will be more than 80 characters and will pass the 72 column limit, and should be highlighted as a string.
The coding standards are similar to COBOL and all characters beyond column 72 should by comment lines. These column 72 comments do start with the comment start character, and are highlighted properly.

You can
:match DiffAdd '\%>72v.*'
which will highlight the characters you don't want.
(adapted from here).

You can try this:
:setlocal colorcolumn=72
That'll just highlight the whole column. Use set instead of setlocal if you want it to apply to all tabs/splits.
Check out the help file for more details.

Related

Highlight positions after the EOL character

I am, currently, attempting to replicate the vim feature 'colorcolumn'. In addition to replicating 'colorcolumn', I have ideas that would require replicating 'cursorcolumn' and 'cursorline'. However, all my attempts to match a specific column are dependent on a character occupying that specific column.
To put it another way, I cannot come up with a way to match any position after the EOL ('$') character.
For instance, the following will only highlight column 25 if a character occupies that position. This is, similarly, true for :match, match(), matchadd(), and matchaddpos().
:highlight CC2 ctermbg=green
:syntax match CC2 /\%25v./
I don't want to focus too much on a particular idea, but my present idea for 'colorcolumn' is to have several different columns (which is easy enough; :set cc=10,20,30), but each column would have it's own background color. Say, green at column 80, yellow at 100, and red at 120.
Any other suggestions?
This is not possible, since match works only on the buffer content, so if there is no content, the column can't be matched.
BTW: That is one reason, why the 'colorcolumn' option has been implemented.

Vim substitute highlight colors

When using :%s/foo/bar/c to replace foo with bar, but with the c tag to have a yes or no dialog for each replace, I have really unreadable highlighting. All occurences of the searched word appear in grey background with white text (normal search highlight). However the one occurence I'm asked for is "highlighted" in yellow text without background => easily confused with regular text.
I found a million highlight search coloring things, but nothing to this one subsitute entry you are asked for. How do I change only this one? Thanks for any hint.
It's IncSearch.
From :h hl-IncSearch :
*hl-IncSearch*
IncSearch 'incsearch' highlighting; also used for the text replaced with
":s///c"

Vim colorcolumn interrupted when using wrap

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

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

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