2

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 ?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Please note that this question isn't about doing it outside of vim. – Denys Séguret Oct 28 '15 at 14:30
  • `:%s/,\(\S\)/, \1/g` should work. – newbie Oct 28 '15 at 14:31
  • 2
    Because there are a zillion different dialects for regular expressions, and Vim's is only one of them. See `:help pattern-searches`, and in particular `:help /magic` and subsequent sections. Read them patiently. – Sato Katsura Oct 28 '15 at 15:22
  • @SatoKatsura +1 for zillion dealects for regexes. Related: http://stackoverflow.com/questions/3604617/why-does-vim-have-its-own-regex-syntax. – newbie Oct 28 '15 at 15:55

4 Answers4

8

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
anubhava
  • 761,203
  • 64
  • 569
  • 643
4

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

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

newbie
  • 1,230
  • 1
  • 12
  • 21
1

Alternative solution:

:%s/,\zs\ze\S/ /g
Sato Katsura
  • 3,066
  • 15
  • 21
0

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

Mosqueteiro
  • 124
  • 1
  • 4
  • 1
    Do note that this regex is not strictly vim-compatible: it would have to be re-written to use vim’s positive lookahead syntax for use in vim. I’m sure it works in vscode just fine though. – D. Ben Knoble Jan 16 '20 at 03:40