sof
sof

Reputation: 9649

Mark a block of characters in VI/M ex command

It's quite straitforward for VI/M to mark a block of lines from Mth line to Nth line ready to delete, cut & past, or copy & paste.

:M,N d
:M,N m p
:M,N t p 

If it's further required for VI/M to mark a block of characters from Ith character of Mth line to Jth character of Nth line, is it possible to accomplish similarly to the above?

@EDIT

Except the next answer asked for visual block mode, how about the option on typing a succinct ex command?

@EDIT 2

To clarify the meaning of a block of characters:

@SOLUTION

Selecting abitrary zipzag area of successive characters from line M, column I to line N, column J in ex mode exactly like in visual character mode :

mark:

:normal! MggI|vNggJ|

delete:

:normal! MggI|vNggJ|d

yank:

:normal! MggI|vNggJ|y

move to line X column Y

:normal! MggI|vNggJ|dXggY|p

copy to line X column Y

:normal! MggI|vNggJ|yXggY|p

@SOLUTION 2

Selecting abitrary square block of characters from line M, column I to line N, column J in ex mode exactly like in visual block mode :

mark:

:execute "normal! MggI|\<C-v>NggJ|"

delete:

:execute "normal! MggI|\<C-v>NggJ|d"

yank:

:execute "normal! MggI|\<C-v>NggJ|y"

move to line X column Y

:execute "normal! MggI|\<C-v>NggJ|dXggY|p"

copy to line X column Y

:execute "normal! MggI|\<C-v>NggJ|yXggY|p"

Upvotes: 3

Views: 5305

Answers (2)

ZyX
ZyX

Reputation: 53624

You can use visual block mode from an ex command mode using normal!: for example, to select a block (line, column) from (42, 10) to (54, 20) and yank it (both lines must have at least 20 characters or virtualedit=block should be set):

execute "normal! 42gg10|\<C-v>54gg20|y"

. It is very straightforward way to do this, useful only in scripts.

Note that this command has at least following side-effects:

  1. Setting marks '<, '>, '[, '], ''.
  2. Moving a cursor.
  3. Changing registers @", @0.
  4. Adding one item to the jumplist.
  5. Overwriting previous visual selection.
  6. Altering v:count and v:count1 variables.

Upvotes: 4

njbair
njbair

Reputation: 1982

Ctrl+V enables visual block mode, then you can use the arrow keys to select the block.

Upvotes: 3

Related Questions