how to add only one space character before and after a ,

1 view
Skip to first unread message

Jeri Raye

unread,
Jul 5, 2009, 10:15:54 AM7/5/09
to v...@vim.org
Hi,

If a line contains a , this , should have only one space character and
also only one space character
after it. How do you do that?

So the following:
sX,sY
sX ,sY
sX, sY
sX , sY

Must all be come (the last one should be trimmed to one space before
and after the ,)
sX , sY

Rgds,
Jeri

Zsolt Udvari

unread,
Jul 5, 2009, 10:21:45 AM7/5/09
to vim...@googlegroups.com

Imho with a simple :1,$ s@ *, *@, @g

Tim Chase

unread,
Jul 5, 2009, 2:09:18 PM7/5/09
to vim...@googlegroups.com
> Imho with a simple :1,$ s@ *, *@, @g'

Based on the OP's example, that's missing a space before the
replacement comma:

:%s/ *, */ , /g

should do the trick. If tabs are to be considered spaces too,
you can use

:%s/\s*,\s*/ , /g

instead.

-tim


Jay Heyl

unread,
Jul 5, 2009, 3:44:38 PM7/5/09
to vim...@googlegroups.com
This is trickier than it at first appears. Pattern matching is greedy by default, matching as many characters as possible. So normal wildcard matching could end up including commas when that's not what you want. (If there is more than one comma on a line it will probably skip all but the last.)

There may be a more clever way to do it using some of the rather specialized wildcard sequences in a single substitution. The straightforward way is to do it in two passes. First be sure all the commas have spaces around them by doing this:

:%s/,/ , /g

Then get rid of the excess spaces around the commas with:

%s/\s\+,\s\+/ , /g

That did the trick on a trial using your sample input. I'm pretty sure it should also work in more complicated situations.

  -- Jay

Tony Mechelynck

unread,
Jul 8, 2009, 12:47:33 PM7/8/09
to vim...@googlegroups.com, v...@vim.org

Are you sure?

It is possible, but the usual typographical (and programming) convention
is to have no space before a comma, and one space after it except at the
end of a line or before another comma, closing quote etc.

To do what you ask (untested)

:%s/\s*,\s*/ , /g

This ought to replace any comma preceded and followed by zero or more
spaces and tabs by space-comma-space. I think that consecutive commas
will get two intervening spaces instead of one though.


Best regards,
Tony.
--
Anything worth doing is worth overdoing

James Kanze

unread,
Jul 9, 2009, 7:39:38 AM7/9/09
to vim_use
On Jul 8, 6:47 pm, Tony Mechelynck
<antoine.mechely...@gmail.com> wrote:
> On 05/07/09 16:15, Jeri Raye wrote:

> > If a line contains a , this , should have only one space
> > character and also only one space character after it. How do
> > you do that?

> > So the following:
> > sX,sY
> > sX ,sY
> > sX, sY
> > sX , sY

> > Must all be come (the last one should be trimmed to one
> > space before and after the ,)
> > sX , sY

> Are you sure?

> It is possible, but the usual typographical (and programming)
> convention is to have no space before a comma, and one space
> after it except at the end of a line or before another comma,
> closing quote etc.

I once played with the convention of using spaces both before
and after. In the end, though, it looked to wierd, even to me.
(The rationale was simple, the comma isn't any more "attached"
to what precedes it than it is to what follows, so the spacing
should be the same.)

> To do what you ask (untested)

> :%s/\s*,\s*/ , /g

> This ought to replace any comma preceded and followed by zero
> or more spaces and tabs by space-comma-space. I think that
> consecutive commas will get two intervening spaces instead of
> one though.

It will. This can be fixed with a second step:
:%s/,\s*,/,,/g
(or whatever is actually desired).

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jeri Raye

unread,
Jul 9, 2009, 2:25:02 PM7/9/09
to v...@vim.org
Tony Mechelynck wrote on 8-7-2009 18:47:
>> If a line contains a , this , should have only one space character and
>> also only one space character
>> after it. How do you do that?

[deleted]

> Are you sure?
>
> It is possible, but the usual typographical (and programming) convention
> is to have no space before a comma, and one space after it except at the
> end of a line or before another comma, closing quote etc.

[deleted]

Hi Tony,

I agree to the usual convention.
Only for this matter I need to tweak the code style to another kind of
style. And then I need to have the , between two spaces.
My vim-script relies on that.

Rgds,
Jeri

Reply all
Reply to author
Forward
0 new messages