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.
Related
For more comfortable work with YAML/Ansible I wanna highlight pairs of spaces with different colors in a row.
For example where we write " - name" (six spaces here in the beginning of string) first pair of spaces will be yellow, next two will be red and so on.
But I can't understand how to write it in my .vimrc. Someone can help?
These aren’t exactly the two-space highlighting your looking for, but vim-indent-guides and indentLine both can get you the essential feature of highlighting columns of indent.
The alternative I think would be to create n match groups where group i matches 2 spaces (after the first 2(i-1) spaces), and then color those differently.
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).
How would I make vim highlight all the words that are in my dictionary when opening a text file (at startup)?
My preferred way would be to add some 2 or 3 lines settings/fun/autocmd to my vimrc, but if it is not possible, what would be the plugin offering exactly this dictionary word highlight function?
Thanks in advance.
I tried various approaches such as creating custom dictionaries and then
redefining the highlighting for Normal and SpellBad words. I also tried
marking the desired words as being “rare” (since Vim uses different
highlighting for rare words) but those ideas didn’t work out. Here's the best
I could come up with:
Define highlighting
First, define how you want the words to be highlighted. In this example, I
want all the numbers from one to ten to be highlighted so I call my group,
“Numbers” and tell Vim that I want these words to appear in red with either
the terminal or the GUI version.
highlight Numbers ctermfg=red guifg=red
Option 1: Syntax
If there are a lot of words, use the syntax command to define the keywords
to be highlighted:
syntax on
syntax keyword Numbers one two three four five six seven eight nine ten One Two Three Four Five Six Seven Eight Nine Ten
Option 2: Match
Note that using syntax option, you need to include different permutations of
upper and lower case. If you don’t want to do that, you could instead use the
match keyword which operates on regular expression patterns rather than a
list of words. Use the \c option to ignore case.
match Numbers /\c\<one\>\|\<two\>\|\<three\>\|\<four\>\|\<five\>\|\<six\>\|\<seven\>\|\<eight\>\|\<nine\>\|\<ten\>/
The drawback to using match is that Vim has to keep evaluating the match
pattern for changes in the text. This becomes computationally expensive if the
regular expression pattern is too long (lots of words). This would cause
Vim to become too slow.
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.
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.