3

In vim, how can I paste multiple lines of code after every line of a visual block?

What I have is:

 foo bar 1 2 3 

What I am trying to do is:

1 foo bar 2 foo bar 3 foo bar 

Is there a way to easily accomplish this?

3
  • you have your answers below, if it is something more complex than what you have shown then you should have a look at macros vim.wikia.com/wiki/Macros Commented Nov 27, 2013 at 16:23
  • I was hoping that there was some way to do it using visual mode but I guess not. Thanks for the help everyone! Commented Nov 27, 2013 at 16:33
  • If you do want to use visual mode for this, something here might help: vim.wikia.com/wiki/Repeat_command_on_each_line_in_visual_block Commented Nov 27, 2013 at 16:38

3 Answers 3

4

You can cut it to the default register and do a global replacement for the rest of lines, like this:

Go to first column of first line of the file:

gg0 

Cut data:

3dd 

Do a global repeat for every line of the file and paste it:

:g/^/put 

It yields:

1 foo bar 2 foo bar 3 foo bar 
Sign up to request clarification or add additional context in comments.

1 Comment

This one does it way better
2

assume that you want to copy and paste line number 1 2 3, run this command:

:g/^\S/1,3t. 

then the text foo bar would be copied to right place. then you can remove the two lines.

You can also change the 1,3 to other range.

Comments

1

Not fully automated but almost there:

  • Visually highlight desired rows to copy with Shift+V

  • Delete with d

  • Move to first line ("1") and start recording with q followed by some letter, i.e., a.

  • Paste with p and then move down to the next line ("2"). Stop recording with q.

  • Now just repeat the last recorded command with @@, or + the designated letter, i.e., @a.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.