Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In vim, how do I substitute a regex pattern with whitespace?

Tags:

vim

In my specific case I have

(function(){

and I want to replace that with

<whitespace char>xxx<whitespace char>

so it would go from

$('#myId').click(function(){do_something()})

to

$('#myId').click xxx do_something()})

I've tried

:s%/(function(){/ xxx /g  

but it throws an error throws an error 'E486: Pattern not found' even though I can find just /(function(){/. Nor does trying to add a \s to the replacement text work.

(sorry about the weird formatting of this question. doesn't really flow well)

like image 491
c3rin Avatar asked Nov 19 '25 08:11

c3rin


2 Answers

transposed the s and %

:s%/(function(){/ xxx /g  

should become

:%s/(function(){/ xxx /g  

I tried it and it works for me.

the former returns E486: Pattern not found: /(function(){/ xxx /g

like image 145
matchew Avatar answered Nov 21 '25 21:11

matchew


Your only problem is that it should be :%s not :s%.

:% specifies the whole file as the range for the following Ex command.

:s% means use '%' as the regex separator character (instead of /). So in your query, you aren't finding "/(function" in the current line.

like image 21
bheeshmar Avatar answered Nov 21 '25 22:11

bheeshmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!