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).
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 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.
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 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
I’m a fan of Visual mode in Vim, as it allows to insert text before any given column.
For example, insertion of spaces after the quotation leaders below:
> one
> two
> three
can be done via <Ctrl-V>jjI <Esc>:
> one
> two
> three
as follows:
Start Visual mode with Ctrl-V.
Extend visual selection with jj.
Insert some spaces with I__.
Propagate the change to all the lines of the block selection with Esc.
Now I have a text file that needs some formatting. This is what it looks like:
start() -- xxx
initialize() -- xxx
go() -- xxx
Now I want to align part of this text to arrange it into columns like this:
start() -- xxx
initialize() -- xxx
go() -- xxx
The problem I have is that I cannot insert a different amount of indentation into each line and merely indenting a fixed amount of spaces/tabs is insufficient.
How can you do an indentation where all indented text will have to be aligned at the same column?
Update
I only figured out a rather verbose and unwieldy method:
Find the string position to indent from: \--.
Insert n (let's say 20) spaces before that: 20i <Esc>.
Delete a part of those spaces back to a certain column (let's say 15): d|15.
Save those steps as a macro and repeat the macro as often as necessary.
But this approach is very ugly, though!
I'm much better off without any vim plugins.
Here is my solution:
<Shift-V>jj:!column -ts --
Then insert -- into multiple lines just as you wrote in the question.
You can also append a number of comments at insertion time.
:set virtualedit=all
<Ctrl-V>jjA-- xxx<Esc>
You have to use a specific plugin, you can use either Tabular or Align plugin in this case.
They both allow you to align text on specific characters, like -- in your example. Their syntax is a bit different though. Pick the one that suit you the most.
Without plugin and if you have already entered your comments without emix's solution:
:,+2 s/--/ &
This will ensure all comments are to be shifted leftwise in order to align them properly.
Then blockwise select the column to which you want to align the text, and : 100<
An easy way to align text in columns is to use the Tabular or
Align plugin. If neither of these is ready at hand, one can use
the following somewhat tricky (and a little cumbersome looking) yet
perfectly working (for the case in question) commands.1,2
:let m=0|g/\ze -- /let m=max([m,searchpos(#/,'c')[1]])
:%s//\=repeat(' ',m-col('.'))
The purpose of the first command is to determine the width of the
column to the left of the separator (which I assume to be --
here). The width is calculated as a maximum of the lengths of the text
in the first column among all the lines. The :global command is used
to enumerate the lines containing the separator (the other lines do
not require aligning). The \ze atom located just after the beginning
of the pattern, sets the end of the match at the same position where
it starts (see :help \ze). Changing the borders of the match does
not affect the way :global command works, the pattern is written in
such a manner just to match the needs of the next substitution
command: Since these two commands could share the same pattern, it can
be omitted in the second one.
The command that is run on the matched lines,
:let m=max([m,searchpos(#/,'c')[1]])
calls the searchpos() function to search for the pattern used in the
parent :global command, and to get the column position of the match.
The pattern is referred to as #/ using the last search pattern
register (see :help "/). This takes advantage of the fact that the
:global command updates the / register as soon as it starts
executing. The c flag passed as the second argument in the
searchpos() call allows the match at the first character of a line
(:global positions the cursor at the very beginning of the line to
execute a command on), because it could be that there is no text to
the left of the separator. The searchpos() function returns a list,
the first element of which is the line number of the matched position,
and the second one is the column position. If the command is run on
a line, the line matches the pattern of the containing :global
command. As searchpos() is to look for the same pattern, there is
definitely a match on that line. Therefore, only the column starting
the match is in interest, so it gets extracted from the returning list
by the [1] subscript. This very position equals to the width of the
text in the first column of the line, plus one. Hence, the m variable
is set to the maximum of its current value and that column position.
The second command,
:%s//\=repeat(' ',m-col('.'))
pads the first occurrence of the separator on all of the lines that
contain it, with the number of spaces that is missing to make the text
before the separator to take m characters, minus one. This command
is a global substitution replacing an empty interval just before the
separator (see the comment about the :global command above) with the
result of evaluation of the expression (see :help sub-replace-\=)
repeat(' ',m-col('.'))
The repeat() function repeats its first argument (as string) the
number of times given in the second argument. Since on every
substitution the cursor is moved to the start of the pattern match,
m-col('.') equals exactly to the number of spaces needed to shift
the separator to the right to align columns (col('.') returns the
current column position of the cursor).
1 Below is a one-line version of this pair of commands.
:let m=0|exe'g/\ze -- /let m=max([m,searchpos(#/,"c")[1]])'|%s//\=repeat(' ',m-col('.'))
2 In previous revisions of the answer the commands used
to be as follows.
:let p=[0]|%s/^\ze\(.*\) -- /\=map(p,'max([v:val,len(submatch(1))+1])')[1:0]/
:exe'%s/\ze\%<'.p[0].'c -- /\=repeat(" ",'.p[0].'-col("."))'
Those who are interested in these particular commands can find their
detailed description in this answer’s edit history.
This is a modification on Benoit's answer that has two steps.
First step, block select text search and replace -- with lots of spaces.
'<,'>s/--/ --/
Now all the comments should have lots of spaces, but still be uneven.
Second step, block select the text again and use another regex to match all the characters you want to keep (say the first 20 characters or so) plus all the spaces following, and to replace it with a copy of those first 20 characters:
'<,'>s/\(.\{20}\)\s*/\1/
Not quite as easy as Benoit's, but I couldn't figure out how to make his second step work.