Denys Séguret
Denys Séguret

Reputation: 382474

Add missing spaces after commas

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 ?

Upvotes: 2

Views: 2425

Answers (4)

Mosqueteiro
Mosqueteiro

Reputation: 134

I had this same problem and :%s/,(\S)/, \1/g solution wasn't working for me (I was using vscode with vim key mappings). I used a positive lookahead instead to isolate the , to substitute for ,<space> using

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

regexr.com was super helpful for this

Upvotes: 0

Sato Katsura
Sato Katsura

Reputation: 3086

Alternative solution:

:%s/,\zs\ze\S/ /g

Upvotes: 1

newbie
newbie

Reputation: 1260

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

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

Upvotes: 4

anubhava
anubhava

Reputation: 786289

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

Upvotes: 8

Related Questions