12

It took me very long time to realize that putting (pasting) something with p or P which visual-block selects with ctrl+v and yanked (copied) with y, preserves the manner before, so that, when the copied text was being in the middle of lines, it will then be copied among middle of other text whenever possible. On the contrary, select-line with V does not paste the copied line among other text, but starting on another line.

For instance, suppose I have by now:

12/12: Avocado. Apple.
12/13: Bean.    Banana.
12/14: Cherry.  Citron.
12/15:
12/16:
12/17:

When I want to cut Avocado and Bean and Cherry, by selecting (selection represented as _)

12/12: ________ Apple.
12/13: ________ Banana.
12/14: ________ Citron.
12/15:
12/16:
12/17:

and pressing y, and move cursor to the following line (cursor represented as _),

12/12: Avocado. Apple.
12/13: Bean.    Banana.
12/14: Cherry.  Citron.
_2/15:
12/16:
12/17:

and press P. It results:

12/12: Avocado. Apple.
12/13: Bean.    Banana.
12/14: Cherry.  Citron.
Avocado.12/15:
Bean.   12/16:
Cherry. 12/17:

But what I wished was:

12/12: Avocado. Apple.
12/13: Bean.    Banana.
12/14: Cherry.  Citron.
Avocado.
Bean.   
Cherry. 
12/15:
12/16:
12/17:

This confused me every time, until I realized that because they were copied in the middle of line, they were thus pasted in the middle of line, mimicking the way they were.

It seems the best way to paste something starting on another line, is moving cursor to the beginning with 0, and down 3 lines, to the new place; and entering insert mode with i, and pressing <return> 3 times, and move cursor back 6 lines, and right, back to the original place; and copying, and again move to the new place, and paste, as I did above.

Can I save a few strokes? Or what do you usually do?

(Edit: What's said above was how I understood it the moment I asked this. See those answers for correct explanation : )

2 Answers 2

14

When you copy some text, it goes into a register. The text inside a register has a type: characterwise, linewise or blockwise.

This type determines in which way the text will be put.

In your example, you copied some text from visual block mode. So, the text had the type blockwise and was stored in the unnamed register ".
Because of this type, when you will paste it back inside your buffer, it will be pasted as a block.


If you want to paste it as a sequence of lines, instead of a block, there are 3 ways.

  1. From insert mode, you can insert it by hitting C-r ".
    It should insert the contents of the unnamed register as if its type was linewise.

  1. From the command line, you can use the :put command.
    :put should paste the text inside a register as if its type was linewise, no matter its original type. Note that you can pass the name of a register as an argument, as well as a line address to specifiy below which line you want the register to be pasted.

For example:

:42put a

... should put the contents of the register a, below the 42th line.

Without a register name, Vim will assume you want to paste the unnamed register (or the + register if you tweaked 'clipboard').
And without an address, Vim will assume you want to paste it below the current line.


  1. From the command line, you can change the type of the unnamed register with the setreg() function:

    :call setreg('"', '', 'al')

Here, setreg() should append an empty string to the unnamed register (thanks to the a flag passed as a third argument) and change its type to linewise (thanks to the l flag).

Then, from normal mode, you should be able to paste the register, with the p normal command, as if its type was linewise.


It's not directly linked to your issue, but when you copy some text, it also goes into the 0 register. And if you prepended the value unnamedplus to the option 'clipboard', it also goes into the + register.

You can check what's the type of a register, using the getregtype() function:

:echo getregtype('"')

If it returns a lowercase v, it means the type is characterwise.
If it returns an uppercase V, it means the type is linewise.
If it returns ^V{some number}, it means the type is blockwise, and the width of the block is {some number}.

3

I needed to "cast" register contents into a certain (characterwise / linewise / blockwise) mode so often, I wrote the UnconditionalPaste plugin for it. It provides gcp, glp, etc. alternatives to the built-in paste commands that force a certain mode (and by now several more variations on this theme, like pasting with joined by commas or queried characters).

With it, you can just use glp / glP to paste after / before the current line (as new lines), regardless of how you've yanked the text.

4
  • I envy you that you have time to write such thing QQ But I am satisfied with :put. (Also upvoted you) Commented Dec 16, 2016 at 14:07
  • @Aminopterin: I have answered that so often, I actually have a snipMate snippet for the first paragraph. So I just need to customize a bit. Thanks for appreciating my answer, and the upvote! Commented Dec 16, 2016 at 15:25
  • Oh I said I envy you have time to write a plugin single-handedly, like this, as if done lightheartedly.... only last half a year did I start to write some c++ project as hobby project to force myself to learning programming, and did I started using vim ~~ Commented Dec 17, 2016 at 9:23
  • 2
    @Aminopterin Ah well, the plugin started as a small snippet copied from Vimtips, and slowly grew over time. As it proved so useful to me, I gradually enhanced it greatly. Many small steps are more powerful than a large single effort! Commented Dec 17, 2016 at 9:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.