Vim - Visual Block mode pasting before/after columns

Say you have tons of lines of text that look like this:

123abc
456def
789ghi
...

Say you want to insert some other really long text between the numbers and letters on each line, ending up with:

123SomeReallyLongTextInsertedHereabc
456SomeReallyLongTextInsertedHeredef
789SomeReallyLongTextInsertedHereghi

You can use Visual Block mode (CTRL+V) to highlight some columns, then insert text (using I or A) before or after each column all at once, which is amazingly useful because you only have to type the new text once. But say you don't want to type the new text even once; you want to copy/paste it into each line instead, because the text you want to insert is really long, or you're lazy.

My first instinct would've been to go into Visual Block mode, highlight some columns, then hit p or P. But that fails. It only pastes (and obliterates) the first line of text and leaves the other columns alone. If you look at :h blockwise-operators, you can see that the number of available operators is somewhat limited; insert, append, replace, change, indent. No paste.

One way to do this is to go into Visual Block mode, highlight some columns, use I to go into insert mode, then use ^R" (that's CTRL-R ") to paste register contents. Then hit ESC. (Replace " with the name of the register that contains the text you want to paste.) CTRL-R in Insert mode inserts the contents of a register. See also :h i_CTRL-R.

Another way would be to use a macro (which vim calls "complex-repeat"). In normal mode, something like qq^2lpjq would record a macro that does what we want. Then you put the cursor on the first line and do 3@q in normal mode to run the macro three times, or 100@q to run it 100 times, depending how many lines you have.

I'm sure there are other ways too.

March 24, 2008 @ 4:48 AM PDT
Cateogory: Programming
Tags: Vim

8 Comments

Ivar Refsdal
Quoth Ivar Refsdal on March 27, 2008 @ 2:10 AM PDT

Nice CTRL+R feature. Thanks. :)

Just to add:

You can do :set nowrapscan and then do something like 999@q without having to worry about endless repeats on the last line. (Given of course you want to apply the macro on all lines.)

(Emacs also has rectangles: http://jamesthornton.com/emacs/node/emacs_68.html They work OK too.)

Brian
Quoth Brian on March 27, 2008 @ 8:25 AM PDT

Oh nice. I'm adding that to my .vimrc.

Jay
Quoth Jay on February 10, 2009 @ 4:04 AM PST

How can I copy and paste a column of different text (in each row) adjacent to another column? I have tried different means. Have not been successful so far.

anonyawesom
Quoth anonyawesom on March 25, 2009 @ 7:37 PM PDT

There is another way to paste when you do a CTRL-v I

after you yank (copy) what you want to paste into the columns, do this...

CTRL-v I CTRL-o CTRL-o P

By hitting CTRL-o in insert mode, you temporarily put it in normal mode, which allows you to hit p to paste. Normally you only need one CTRL-o, but I noticed when after you do the CTRL-v it doesn't activate it unless you CTRL-o twice.

To know when it is ready. The status at the bottom won't say --INSERT-- it will say --(insert)--.

Vaibhav
Quoth Vaibhav on September 30, 2009 @ 5:31 AM PDT

Thank you. That was helpful been searching for it.

I had to copy the string to '+' register using "+y and then paste it using ^R+ followed by ESC.

David
Quoth David on February 19, 2010 @ 7:00 AM PST

"How can I copy and paste a column of different text (in each row) adjacent to another column? I have tried different means. Have not been successful so far."

select the first column and yank it into a register ( move "ay) select the second column (next to it) and paste ( move "ap). this will overwrite the second column with the contents :)

Mark Story
Quoth Mark Story on April 24, 2011 @ 7:15 AM PDT

Thanks for the hints, I've been trying to figure out how to accomplish the CTRL-R trick for a while now :)

Joe
Quoth Joe on August 01, 2012 @ 11:11 PM PDT

it's convenient to map CTRL+R" (or *) to CTRL-V in insert mode

e.g.: my map looks like this: inoremap <C-v> <C-r>*

Speak your Mind

You can use Markdown in your comment.
Email/URL are optional. Email is only used for Gravatar.

Preview