In Vim, how to cut in block mode but paste in normal mode, inserting as new lines?

StackOverflow https://stackoverflow.com/questions/21751820

  •  11-10-2022
  •  | 
  •  

Pregunta

Every now and then I run into this type of editing problem in Vim.

I have text formatting in a table that I want to turn into a list.

Before:

AAA BBB
AAA BBB
AAA BBB

CCC DDD
CCC DDD
CCC DDD

After:

AAA
AAA
AAA

BBB
BBB
BBB

CCC
CCC
CCC

DDD
DDD
DDD

Of course not as trivial as this example. The blocks can have hairier contents, larger and inconsistent numbers of lines, etc.

The way I do it now seems to be a bit of a hack:

  1. Select a block from the right column in block mode: CTRL-q in the Windows version to select the top-left corner.
    d to cut the block.
  2. Note how many lines were in the block, then manually add that many blank lines.
  3. Go to the top-left of the new blank area I created. SHIFT+p to paste the block into this area.

Step is the rough part.

Doing a normal non-block cut or copy will always paste into "new" lines. A kind of "insert" or "append" operation. Doing a block cut or copy will normally paste in a kind of "overwrite" mode.

Is there a better way to copy or cut in "block mode" but paste in "insert / append mode"?

¿Fue útil?

Solución

The ex command :put will always paste a register line wise.

Cut the block of text with visual block mode like you have before then execute :put instead of p.

If you want to "cast" pastes in more ways then use @Ingo Karkat's plugin.

For more information see:

:h pu

Otros consejos

you can block wise select the BBB part as how you did, and after cut by d for example, you run this command:

call setreg('"',@",'V')

then you can paste to target line, it will turn your block-wise "yank" into line-wise.

You can create a mapping if you do need to do this often.

My UnconditionalPaste plugin has a glp (go linewise paste) command that will paste the register contents as new lines, regardless of how they were yanked (e.g. in blockwise mode).

It also has several other helpful commands that affect the way the register is pasted.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top