Any other long line ~
^^^^ selected block
Now use the command "Ivery <Esc>". The result is:
This is a very long line ~
short ~
Any other very long line ~
In the short line no text was inserted.
If the string you insert contains a newline, the "I" acts just like a Normal
insert command and affects only the first line of the block.
The "A" command works the same way, except that it appends after the right
side of the block. And it does insert text in a short line. Thus you can
make a choice whether you do or don't want to append text to a short line.
There is one special case for "A": Select a Visual block and then use "$"
to make the block extend to the end of each line. Using "A" now will append
the text to the end of each line.
Using the same example from above, and then typing "$A XXX<Esc>, you get
this result:
This is a long line XXX ~
short XXX ~
Any other long line XXX ~
This really requires using the "$" command. Vim remembers that it was used.
Making the same selection by moving the cursor to the end of the longest line
with other movement commands will not have the same result.
CHANGING TEXT
The Visual block "c" command deletes the block and then throws you into Insert
mode to enable you to type in a string. The string will be inserted in each
line in the block.
Starting with the same selection of the "long" words as above, then typing
"c_LONG_<Esc>", you get this:
This is a _LONG_ line ~
short ~
Any other _LONG_ line ~
Just like with "I" the short line is not changed. Also, you can't enter a
newline in the new text.
The "C" command deletes text from the left edge of the block to the end of
line. It then puts you in Insert mode so that you can type in a string,
which is added to the end of each line.
Starting with the same text again, and typing "Cnew text<Esc>" you get:
This is a new text ~
short ~
Any other new text ~
Notice that, even though only the "long" word was selected, the text after it
is deleted as well. Thus only the location of the left edge of the visual
block really matters.
Again, short lines that do not reach into the block are excluded.
Other commands that change the characters in the block:
~ swap case (a -> A and A -> a)
U make uppercase (a -> A and A -> A)
u make lowercase (a -> a and A -> a)
FILLING WITH A CHARACTER
To fill the whole block with one character, use the "r" command. Again,
starting with the same example text from above, and then typing "rx":
This is a xxxx line ~
short ~
Any other xxxx line ~
Note:
If you want to include characters beyond the end of the line in the
block, check out the 'virtualedit' feature in chapter 25.
SHIFTING
The command ">" shifts the selected text to the right one shift amount,
inserting whitespace. The starting point for this shift is the left edge of
the visual block.
With the same example again, ">" gives this result:
This is a long line ~
short ~
Any other long line ~
The shift amount is specified with the 'shiftwidth' option. To change it to
use 4 spaces: >
:set shiftwidth=4
The "<" command removes one shift amount of whitespace at the left
edge of the block. This command is limited by the amount of text that is
there; so if there is less than a shift amount of whitespace available, it
removes what it can.
JOINING LINES
The "J" command joins all selected lines together into one line. Thus it
removes the line breaks. Actually, the line break, leading white space and
trailing white space is replaced by one space. Two spaces are used after a
line ending (that can be changed with the 'joinspaces' option).
Let's use the example that we got so familiar with now. The result of
using the "J" command:
This is a long line short Any other long line ~
The "J" command doesn't require a blockwise selection. It works with "v" and
"V" selection in exactly the same way.
If you don't want the white space to be changed, use the "gJ" command.
==============================================================================