0

I would like to replace a multiline block to one line

for example :

    if (... ){
     //comment0
     line1
     line2
   }
   some code
   if (... ){
     //comment0
     line1
     line2
   }

into:

if_function()
some code
if_function()

is there a fast way to do this? I tried to yank the block into register

 shift+V
 4j
 "fy

and then replace, but I don't know how to reference the register tried:

%s/@f/if_function/g

and

%s/\@f/if_function/g

and

%s/"f/if_function/g

and

%s/\"f/if_function/g

but they all doesn't work

0

2 Answers 2

1

found how to paste register ctrl+r

so I enter to visual mode, and marked the block

shift+ v
4j

and used the replace command:

:%s/^Rf/if_function/g
1

How I would probably approach this:

  1. Navigate: possibly something like /if then $; the goal is to be on the curly brace for the % key. Or you could just get inside the curlies for a{
  2. Change: cV% or cVa{ and type if_function()
  3. Change populates a register. If I had more of the same edit, I would first navigate again (maybe n?) and then . to repeat. When I’m done, paste the original content somewhere with p if I were, for example, extracting a function. (Actually, I might have yanked it, pasted it, and created the function before starting all the changes, but since c does fill a register we can do things in this order, which almost seems like what you’re looking for.)
1
  • thanks for the answer but I wanted to do it in one command, and I found it in my answer
    – Dor marcus
    Commented Feb 28, 2021 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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