Honestly I can't read regex for now.

I have quick-fix list and I have a hunch that this can be done with regex but I don't know how.

I want this:

@ViewChild('text 1')

to convert to this:

@ViewChild(MatTable, {static: false})
@ViewChild('text 1', {static: false})
@ViewChild('text2', {static: false})

Is it possible to do it with substitute and regex? If it is how should I do it?


So you'll need to apply a substitution command :s :h :s.

First you need to define the range: either % if you want that applied to all the lines in the buffer or '<,'> to apply to the lines which are visually selected. Assuming this is the first case your command will start with :%s/

Now the pattern: here the simplest solution is to say "I want to replace every closing parenthesis with some text". So :%s/)/ no need for a regex you only match a regular ) character.

Then we add the new text , {static: false}) note that as we replace the original ) we need to add it to the replacement text. So :%s/)/, {static: false})/.

Note that you could also have changed you replacement text so that you reuse the matched text: :%s/)/, {static: false}&/.

You probably also want to read :h pattern

EDIT As Ben pointed out in the comments, you actually want to apply the substitution on lines referenced in the quickfix list. To do so you will need to use :h :cdo to apply the command on every referenced lines and you will need to remove the range information. So the final command should be:

:cdo s/)/, {static: false})/
| improve this answer | |
  • 2
    Ohh, it never came on my mind to use the ) as the starting point. BTW thanks for the solution. – zer09 Jun 3 at 9:36
  • 1
    You're completely right Ben, I forgot this information when I wrote my answer, I'll update. – statox Jun 3 at 11:42
  • @D.BenKnoble yeah, I also learned about cdo yesterday – zer09 Jun 4 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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