How do I paste a column of text after a different column of text in Vim?

Vim

Vim Problem Overview


I have two columns full of text, and I want to get them side-by-side. For example, I have

abc
def
ghi

and

123
456
789

and I want

123 abc
456 def
789 ghi

All I can find is how to paste the same line onto the end of every line in a column. I can't figure out how to paste a multi-line block of text on the end of another column.

Vim Solutions


Solution 1 - Vim

I was wondering why "visual block mode" wasn't working for me. The key is to yank (or delete) in visual mode.

I find myself needing to copy some stuff from excel columns having variable lengths. Here's how I do it:

Names
Donald Knuth
Sebastian Thrun
Peter Norvig
Satoshi Nakamoto

Age
100
50
60
45

Let's say you want to put the second column after the first.

  1. Yank it in visual mode:
  • Move cursor to the beginning of Age
  • Press Ctrl + v to enter visual mode
  • Move cursor to 5 in 45
  • Press y to yank (or d to delete)

You have now yanked in visual mode.

  1. Paste (in normal mode)
  • Move to the end of the first line and add more spaces because it's shorter than the second line for example. If you paste a "block" without adding extra spaces, it will overwrite the "run" in Sebastian Thrun.

  • Now you're on the first line, insert a few spaces after the last character. Make sure you're not in insert mode and hit p to paste the block. (If you want to paste in insert mode, use ctrl+r ")

enter image description here

Solution 2 - Vim

Use visual block (ctrl-v) to cut the letter column. Then move to the first line of the number column. Move to the end and make one space. Then paste the letter column.

Solution 3 - Vim

If you're on a Mac and can't figure out why the paste is inserting the block in new lines, it's because you're using the system pasteboard.

No problem, just yank/paste with buffers:

  1. Ctrlv - select what you want to yank
  2. "lettery - yank into letter buffer
  3. Go to where you want to paste
  4. "letterp - paste from letter buffer

Make sure that letter is not + as that is the system pasteboard buffer and thus it will have the same effect.

Solution 4 - Vim

You have:

abc
def
ghi

123
456
789

Move your cursor onto the a. Then CTRL+V, and move your cursor to i. Press y.

Next, add a space after the 3 character, and press p.

Done.

Solution 5 - Vim

This may seem obvious, but if you are pasting from an externally copied buffer it will not work with just visual mode paste. What you can do is first do a normal paste in an empty area and vertical cut/paste using the above method.

Solution 6 - Vim

If you'd like to manually set the register type to say blockwise, use this

:call setreg(0, getreg(0), 'b')

or

:call setreg('*', getreg('*'), 'b')

Useful for pasting a column from a spreadsheet editor.

See :help setreg and vimtip for details.

Solution 7 - Vim

If your columns are different lengths and it is cumbersome to align them to use the blockwise copy and paste, you can search-and-replace while getting rows from the clipboard.

For the rows in the most upvoted answer:

Names
Donald Knuth
Sebastian Thrun
Peter Norvig
Satoshi Nakamoto

you can do :'<,'>s/$/\=getreg('"',1,1)[line("'<")-line('.')], which substitutes end-of-line with the contents of the default register indexed (linewise) by start-of-visual-selection '< minus the line number the substitution is happening on ..

This would result in NamesAge etc., so to also add a space, you can join strings in vim with .. :'<,'>s/$/\=' '.getreg('"',1,1)[line("'<")-line('.')] gives you

Names Age
Donald Knuth 45
Sebastian Thrun 60
Peter Norvig 50
Satoshi Nakamoto 100

This is of course more useful for more complex cases, like substituting keys in dictionaries of key-value pairs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLily BallardView Question on Stackoverflow
Solution 1 - VimAbdoView Answer on Stackoverflow
Solution 2 - VimclimeView Answer on Stackoverflow
Solution 3 - VimQix - MONICA WAS MISTREATEDView Answer on Stackoverflow
Solution 4 - VimMatejView Answer on Stackoverflow
Solution 5 - VimAndrei PokrovskyView Answer on Stackoverflow
Solution 6 - VimnullpView Answer on Stackoverflow
Solution 7 - VimSakari CajanusView Answer on Stackoverflow