delete/move/yank multiple sections vim

49 views Asked by At

I'm aware that you can delete say lines 34-40 with :34,40d but is there a way to delete say lines 34-40 AND lines 43-45

something like :34,40(?)43,45d where (?) represents the symbol necessary to tell vim AND this second set of rows?

Thanks

2

There are 2 answers

2
romainl On

There's no usable built-in way to do exactly what you want.

But this is simple enough IMO:

:34,40d|43-45d

There's another way to look at it. If those lines share a pattern, you could do:

:g/pattern/d
0
Ingo Karkat On

The shortest way is just duplicating the :d[elete] command and concatenating all commands in one command-line. For mutable commands (like :delete), you need to start from the end, so that the line numbers are still valid:

:43,45d|34,40d

With plugin

My PatternsOnText plugin provides (among many others) a :RangeDo command that simplifies the handling, as it adapts the ranges for mutations and ensures that each line is processed only once:

:RangeDo 34,40 43,45 d

Yanks

With or without plugin, each single range still results in a separate command invocation. To accumulate yanks in a single register, you have to use the uppercase version that appends (on all but the first invocation):

:34,40yank a | 43,45yank A
:let @a = '' | RangeDo 34,40 43,45 yank A