throwing a block with vi

By Paul Murphy, author of The Unix Guide to Defenestration

It is possible to do block mode copy and paste in vi using nothing more than a VT100 terminal but in real life only people in the late stages of emacs deprivation try it.

The easy way to copy a block of text, say columns A to B from rows X to Y in the source file to the file you're working with in vi is to use the stream editor, sed. The basic syntax is:

!! sed -n '/X/,/Y/s/AB/C/p' source_file

X and Y are patterns or line numbers but the rest of it gets a little tricky.

The easiest thing is to use a positional notation -patterns work too, they're just harder to follow. Using substitution here is inefficient and unnecessary - I do it because it's an extremely flexible construction that applies to many other things too, and this level of inefficiency doesn't matter to a SPARC.

It's best to use vi to prepare your sed command because almost all vi commands can be preceeded by a number indicating how often to repeat it. Thus: "30i.[esc]" inserts 30 periods. So if A=30 and B=60 you could type:
% vi sed.cmd
i
s/^[esc]a30.[esc]a\([esc]30a.[esc]a\)/\1/p[esc]ZZ
to get a file that contains:

s/^..............................\(..............................\)/\1/p

Called as

% sed -n -f sed.cmd target_file

this will print only the second 30 characters in every row of the target file. Adding the row pattern match ('/X/,/Y/') will restrict it to only the rows you want

/From/,/To/s/^..............................\(..............................\)/\1/p

and so:

!!sed -n -f sed.cmd target file

will insert just that block of text into your file.