How to highlight in Vim sequences of two symbols? - vim

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.

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.

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

auto highlight dictionary words when opening text file in vim

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.

vim : Highlight tab with one color and spaces with another ?

How can I highlight tabs with one color and spaces with another in vim ?
I know how to highlight only tabs or only spaces. And I don't know how to select the colors separately for both spaces and tabs.
A simple solution would be to do something like:
:match Error /\t/
:2match Todo / /
Where Error and Todo are highlight groups from :highlight. This is going to take up two of your three matches and will only be temporary.
Theoretically you could use matchadd() or a combination of highlight groups and :syntax match commands in your .vimrc to make this more permanent but your question doesn't really specify if that's what you want.

Replace colon with tab to make columns

I have word lists where the word or expression in Spanish is separated by its translation with a colon (":"). I want to make two columns, one for Spanish, the other for English. In vim I tried with
:%s/:/^I^I^I/g
But it does not give the desired output. The different columns are not aligned.
When deleting the colon by hand and inserting the number of tabs with the same amount of tab strokes, it always ends up aligned.
Any idea how to solve this, preferably in vim?
On a Linux/*nix system I use column(1)
:%!column -s':' -t
followed by
:%retab!
I'd probable do a
:s/:/^I/g
followed by a
:set ts=25
Where 25 is the length of the longest word expected + 2. So, if you expect the longest word of your input (on the left side of the colon) to be 12 characters, I'd choose something around 14 instead.
With Tabular.vim it's quite easy, just type :Tab /:\zs and it does the rest.
When in insert mode a setting of yours makes tab to reach a nth column. This is set with the 'softtabstop' settings if I am right.
For those tasks you could use a plugin like Align.vim or Tabularize.
Another option is to insert a huge number of spaces, and then use Visual Block with < operator as many times as necessary, if you have to do it once. Otherwise prefer reusable methods.

Resources