0

I got a block of text in vim with 3 rows with diffrent length. I get another block of data with also 3 rows from an external application (like word or notepad) and I would like to append-paste it on all these 3 rows. I found some posts on this matter https://superuser.com/questions/300362/vim-how-to-paste-a-block-of-text-at-the-end-of-multiple-lines and cut and paste multiple lines in vim (also Paste multiple times) but which only seem to work when the content was originally yanked from vim. clipboard=unnamed is set.

Here is an example what I would like to achieve (stolen from first post ;-D):

//Comment1
//Comment2
//Comment3

datablock from external application

foo = 1;
bar = 2;
baz = 3;

original data

foo = 1; //Comment1
bar = 2; //Comment2
baz = 3; //Comment3

result

Community
  • 1
  • 1
Sensei
  • 598
  • 1
  • 4
  • 13

3 Answers3

2

In Vim, yanked text can be of three kinds: "characterwise", "linewise" or "blockwise". While it is possible with Vimscript to change the kind of the content of a register it certainly is not very practical (:help setreg()).

The simplest would be to:

  • paste those comments somewhere above or below your original data
  • hit <C-v> to enter visual-block mode and select that block of comments
  • hit d to delete it
  • move the cursor on the first line of your original data
  • hit p to paste "blockwise"
romainl
  • 186,200
  • 21
  • 280
  • 313
  • This also works great. Kudos to you for not using a plugin. Just requires an additional two steps. Also +1 for reminding me of the register formats. – Sensei May 30 '13 at 14:19
1

You can use my UnconditionalPaste plugin for that. It provides a gbp mapping that forces the paste to be blockwise, regardless of the mode the register was yanked. (It also has other related mappings for characterwise and linewise pastes, and more!)

With it, position the cursor at the end of the first line, and use "+gbp.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thank you for another great plugin. Worked on second try. Important note: `clipboard=unnamed` can not be set. Otherwise it won't work! – Sensei May 30 '13 at 14:15
  • What would I have to do to make it work when the lines have different lengths and the first one is not the longest? And FYI, pasting from MS Word does not work. I guess the line endings are messed up or something. – Sensei May 30 '13 at 16:03
  • I don't have any problems with `clipboard=unnamed`; with that, I don't even have to use the `"+` prefix! – Ingo Karkat May 30 '13 at 19:35
  • For different line lengths, `:set virtualedit=all` allows you to position the cursor at the right position in the first line. – Ingo Karkat May 30 '13 at 19:35
0

This is something that I would do (<CR> = Enter Key, <ctrl-v> control+v):

e: external_application.c<CR>
<ctrl-v>G$"py
e original_data.c<CR>
gg$"pp
cforbish
  • 8,567
  • 3
  • 28
  • 32