Loom
Loom

Reputation: 9986

How to replace current string line with another line in vim?

I'd like to replace current string line with another (for example the another line is placed in 5 lines above current line). I can do it with a pair of commands

dd
:-5t-1

Is there the shorter way to obtain same goal?

Upvotes: 1

Views: 141

Answers (3)

Ingo Karkat
Ingo Karkat

Reputation: 172510

If you don't mind a plugin, my LineJuggler plugin offers a ]r command (and many more):

]r Fetch the line [count] visible lines above the current line and replace the current line with it.

With it, your example would be the short and easy 5]r


In addition, the companion LineJugglerCommands plugin now offers a similar :Replace Ex command. Again, your example would be

:Replace -5

Upvotes: 1

romainl
romainl

Reputation: 196456

dd
:-5t-1

is already pretty short if you ask me. But you can squeeze everything into a one-liner:

:d|-5t-1

and remove the 1 because it's implied by -:

:d|-5t-

Barring making a custom command or mapping I don't see how you could make it shorter.

Upvotes: 3

Kent
Kent

Reputation: 195029

:-5y<CR>Vp

is it shorter?

if you need do that really often, add this into your vimrc:

command! -range R d|<line1>,<line2>t-

then you can just do :-5R replace current line with -5 line

or 2,4R to cp line 2-4 (3 lines) to current line, and replace current line.

Upvotes: 2

Related Questions