difficulty with simple substitution

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

difficulty with simple substitution

jkilbour
myfile:
WHOFRE|3717
WHOGER|3402

I want to replace the vertical bars with "--". I try

:%s/\|/--/
and get:
--WHOFRE|3717
--WHOGER|3402

:s/\|/--/g
and get:
--W--H--O--F--R--E--|--3--7--1--7
--W--H--O--G--E--R--|--3--4--0--2

I'll settle with just removing the bars, but nothing happens:
:%s/\|//
WHOFRE|3717
WHOGER|3402

What am I missing?



Reply | Threaded
Open this post in threaded view
|

Re: difficulty with simple substitution

Gareth Oakes-2
Depending on your 'magic' settings the pipe should NOT be escaped.  Vim
regexs can be a little funny like that.

So for your first example regex:

:%s/\|/--/

Try this instead:

:%s/|/--/

Cheers,
Gareth

[hidden email] wrote:

> myfile:
> WHOFRE|3717
> WHOGER|3402
>
> I want to replace the vertical bars with "--". I try
>
> :%s/\|/--/
> and get:
> --WHOFRE|3717
> --WHOGER|3402
>
> :s/\|/--/g
> and get:
> --W--H--O--F--R--E--|--3--7--1--7
> --W--H--O--G--E--R--|--3--4--0--2
>
> I'll settle with just removing the bars, but nothing happens:
> :%s/\|//
> WHOFRE|3717
> WHOGER|3402
>
> What am I missing?
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: difficulty with simple substitution

Tim Chase-2
In reply to this post by jkilbour
> I want to replace the vertical bars with "--". I try
>
> :%s/\|/--/
[cut]
> What am I missing?

Depending on your "magic" settings, the escaped-bar is a
conditional, allowing the stuff either to the left or to the
right of it to match.  Simply remove the back-slash, as in

        :%s/|/--

and you should be good to go.

        :help /\|
        :help magic

Hope this helps,

-tim