Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add missing spaces after commas

Tags:

regex

vim

vi

I try to change

rest.get = function(url,onsuccess,onerror) {
      rest.askServer("GET", url, null,200,onsuccess,onerror);
};

into

rest.get = function(url, onsuccess, onerror) {
      rest.askServer("GET", url, null, 200, onsuccess, onerror);
};

I thought this command would work:

:%s/,(\S)/, \1/g

But it doesn't.

Why ? What command should I use ?

like image 374
Denys Séguret Avatar asked Jan 17 '26 23:01

Denys Séguret


2 Answers

You can use capturing group:

%s/,\(\S\)/, \1/g

\(\S\) is being used to capture the next non-space character after a comma.

OR you can avoid the capturing using positive lookahead:

:%s/,\(\S\)\@=/, /g

Or to avoid the escaping using very magic:

:%s/\v,(\S)\@=/, /g
like image 184
anubhava Avatar answered Jan 20 '26 23:01

anubhava


Use :%s/,\(\S\)/, \1/g.

You should escape parenthesis as noted in vim documentation. Consider this wiki entry: Search and replace in vim.

like image 22
newbie Avatar answered Jan 20 '26 23:01

newbie



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!