Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature Request: Visual Block mode #21

@davehouser1

Description

@davehouser1

@h-jangra Nice work on all the features you implemented so far. This one would be really cool.
Not sure if this is possible, but anyway to implement visual block mode? Or as I like to call it vertical selection. I noticed the last release had a mention of a feature for CTRL q for visual block, but it does not seem to do anything.
If the feature is not implemented, here is a recap of what it is and how it should operate
Example text:

1 dog
2 cat
3 moose

First of all you will need to implement an override for CTRL v so we can use visual mode.
Next, an explanation of expectations. From the text above, if I put my cursor on the o in dog on line 1 and then select Ctrl v, then use j once o on line 1 and and a in cat on line 2 would be selected. Then if I use j again, o from dog on line one, a from cat on line 2, and the first o from moose will be selected. Now I can an actions like y to yank. After hitting y, then cursor will move back to the starting position of o in dog on line one, and o, a, and o will be in my clipboard. Now if I move to line 4, and paste, I see the following

1 dog
2 cat
3 moose
4 o
5 a
6 o

This should also support the same process and take into account using all direction for visual block mode.

From vim documentation:

10.5 Visual block mode

With CTRL-V you can start selection of a rectangular area of text. There are
a few commands that do something special with the text block.

There is something special about using the "$" command in Visual block mode.
When the last motion command used was "$", all lines in the Visual selection
will extend until the end of the line, also when the line with the cursor is
shorter. This remains effective until you use a motion command that moves the
cursor horizontally. Thus using "j" keeps it, "h" stops it.

INSERTING TEXT

The command "I{string}" inserts the text {string} in each line, just
left of the visual block. You start by pressing CTRL-V to enter visual block
mode. Now you move the cursor to define your block. Next you type I to enter
Insert mode, followed by the text to insert. As you type, the text appears on
the first line only.
After you press to end the insert, the text will magically be
inserted in the rest of the lines contained in the visual selection. Example:

    include one ~
    include two ~
    include three ~
    include four ~

Move the cursor to the "o" of "one" and press CTRL-V. Move it down with "3j"
to "four". You now have a block selection that spans four lines. Now type: >

    Imain.<Esc>

The result:

    include main.one ~
    include main.two ~
    include main.three ~
    include main.four ~

If the block spans short lines that do not extend into the block, the text is
not inserted in that line. For example, make a Visual block selection that
includes the word "long" in the first and last line of this text, and thus has
no text selected in the second line:

    This is a long line ~
    short ~
    Any other long line ~

              ^^^^ selected block

Now use the command "Ivery ". 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.
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, 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_", 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" 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions