You are not logged in.

#1 2013-11-01 07:49:49

cryptkeeper
Member
From: Zurich, Switzerland
Registered: 2011-12-29
Posts: 63

[SOLVED] vim: newlines when pasting block selection

Hi there!

When pasting a block selection im Vim, the pasted lines are added to the existing lines. That definitely has it's uses, but usually I'd like to paste the lines onto blank lines. For this, I first have to generate a buch of empty lines, and only then can I paste the block. Is there a way to automatically generate new lines when pasting blocks?

Here's a small example in Fortran. I'd like to get the list of REALs by block-selecting the variables and copying them between the REAL and the INTEGER block.

REAL :: &
  real1 (2,2) , &
  real2 (5,2) , &
  real3 (1,2)

INTEGER :: &
  int1 ,&
  int2

If I just block-select it and paste it onto the line between the REAL and INTEGER blocks, I end up with this:

REAL :: &
  real1 (2,2) , &
  real2 (5,2) , &
  real3 (1,2)
real1
real2INTEGER :: &
real3  int1 ,&
  int2

What I'd like to end up with, though, is this:

REAL :: &
  real1 (2,2) , &
  real2 (5,2) , &
  real3 (1,2)
real1
real2
real3
INTEGER :: &
  int1 ,&
  int2

Without manually inserting blank lines, that is!

PS: I'm not really sure whether that's the correct subforum, but as the questions mostly addresses Programmers/Coders, I guess it should be fine here.

Last edited by cryptkeeper (2013-11-07 08:16:32)

Offline

#2 2013-11-01 08:26:53

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] vim: newlines when pasting block selection

Offline

#3 2013-11-01 10:59:56

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: [SOLVED] vim: newlines when pasting block selection

Karol's answer seems like a more direct solution, but I'd personally go with a combination of simpler commands like "3yy3jPw Ctrl-v 3j$d"

Last edited by Trilby (2013-11-01 11:02:01)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#4 2013-11-07 08:16:05

cryptkeeper
Member
From: Zurich, Switzerland
Registered: 2011-12-29
Posts: 63

Re: [SOLVED] vim: newlines when pasting block selection

karol wrote:

I haven't tried it yet, but it looks exactly what I need! What I do after pasting the selected block is often joining the lines to a single line with the variables delimited by commas or \\|| (for further use in vim regexes), and it looks like this plugin can do exactly this.

Thanks! I'll mark it SOLVED then (and hope I'll get around to actually try it soon enough).

Trilby wrote:

Karol's answer seems like a more direct solution, but I'd personally go with a combination of simpler commands like "3yy3jPw Ctrl-v 3j$d"

The problem with this is that you know the exact number of lines, i.e. 3 in this case, which is usually not the case in the code I work with as the variable lists are much longer.

But what I've become accustomed to in the meantime is memorizing the number of lines that vim shows when I yank the block and inserting that number of new lines before pasting, e.g. for a 24 line block

24o<ESC>j
24k
p

I doubt that using 24o<ESC>j is the best way to insert the 24 blank lines, but it works well enough.

Offline

#5 2013-11-07 09:49:54

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] vim: newlines when pasting block selection

cryptkeeper wrote:

I doubt that using 24o<ESC>j is the best way to insert the 24 blank lines, but it works well enough.

I used to have a mapping in my ~/.vimrc for '20o<esc>20k' and it worked well enough for me too :-)

Offline

#6 2013-11-07 11:35:38

OliK
Member
Registered: 2011-12-15
Posts: 4

Re: [SOLVED] vim: newlines when pasting block selection

:put

:h :put
:put always works linewise

Offline

#7 2013-11-07 11:39:51

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] vim: newlines when pasting block selection

It's good to know that vim a builtin linewise paste. Not that I doubted its existence or anything ;P

One day I'll have a go at vimtutor, but I have to finish perpetum mobile first.

Offline

#8 2013-11-07 12:05:22

cryptkeeper
Member
From: Zurich, Switzerland
Registered: 2011-12-29
Posts: 63

Re: [SOLVED] vim: newlines when pasting block selection

OliK wrote:
:put

:h :put
:put always works linewise

Awesome, now that's exactly what I'd been looking for! That simplifies things quite a bit.

Thanks!

Offline

#9 2013-11-07 13:12:43

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [SOLVED] vim: newlines when pasting block selection

cryptkeeper wrote:
Trilby wrote:

Karol's answer seems like a more direct solution, but I'd personally go with a combination of simpler commands like "3yy3jPw Ctrl-v 3j$d"

The problem with this is that you know the exact number of lines, i.e. 3 in this case, which is usually not the case in the code I work with as the variable lists are much longer.

But what I've become accustomed to in the meantime is memorizing the number of lines that vim shows when I yank the block and inserting that number of new lines before pasting, e.g. for a 24 line block

24o<ESC>j
24k
p

I doubt that using 24o<ESC>j is the best way to insert the 24 blank lines, but it works well enough.

Yank the whole thing in visual line mode (V), which tells you how many lines there are -- then use that number to delete what you don't want in block mode.

"V/real3<Enter>y3jPw<Ctrl-v>3j$d"
Here you don't have to know how many lines there are between the cursor and real3 until after you've done the 'y', which displays the count.

Last edited by Trent (2013-11-07 13:12:59)

Offline

#10 2013-11-07 13:19:55

cryptkeeper
Member
From: Zurich, Switzerland
Registered: 2011-12-29
Posts: 63

Re: [SOLVED] vim: newlines when pasting block selection

Trent wrote:

Yank the whole thing in visual line mode (V), which tells you how many lines there are -- then use that number to delete what you don't want in block mode.

"V/real3<Enter>y3jPw<Ctrl-v>3j$d"
Here you don't have to know how many lines there are between the cursor and real3 until after you've done the 'y', which displays the count.

That's actually what I meant by "memorizing the number of lines that vim shows when I yank the block". wink

Offline

#11 2013-11-08 00:27:12

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [SOLVED] vim: newlines when pasting block selection

Yes, I understand, but you objected to Trilby's solution because it couldn't count for you, so I wanted to show you that was still possible.

Offline

#12 2013-11-08 02:19:48

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,156

Re: [SOLVED] vim: newlines when pasting block selection

OliK wrote:
:put

:h :put
:put always works linewise

Thank you. Only now do I understand what this thread is about. (I always use :put so couldn't see how the problem arose.)


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

Board footer

Powered by FluxBB