Search code examples
regexvimvi

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 ?


Solution

  • 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