Replace all on a line

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

Replace all on a line

Jerin Joy
Hi.

The replace all option 'g' doesn't seem to be working in my case. It
replaces only the first occurrence on a line.

for eg. in the following line I wanted to remove the extra whitespaces
between input [] and the name of the variable:
task init (input [1:0]   u, input [3:0]    hash, input [3:0]    hash_index1)

Using:
:%s/\][\ ]\+/\]\ /g

replaces only for the first variable - input [1:0] u

Jerin
--
http://jerinj.blogspot.com/
--
Reply | Threaded
Open this post in threaded view
|

Re: Replace all on a line

Jürgen Krämer

Hi,

Jerin Joy wrote:

>
> The replace all option 'g' doesn't seem to be working in my case. It
> replaces only the first occurrence on a line.
>
> for eg. in the following line I wanted to remove the extra whitespaces
> between input [] and the name of the variable:
> task init (input [1:0]   u, input [3:0]    hash, input [3:0]    hash_index1)
>
> Using:
> :%s/\][\ ]\+/\]\ /g
>
> replaces only for the first variable - input [1:0] u

was this really the command you typed inside VIM? I tested it and it
worked. But you can simplify it to

  :%s/] \+/] /g

which is a bit easier to read.

Regards,
Jürgen

--
Jürgen Krämer                              Softwareentwicklung
HABEL GmbH & Co. KG                        mailto:[hidden email]
Hinteres Öschle 2                          Tel: +49 / 74 61 / 93 53 - 15
78604 Rietheim-Weilheim                    Fax: +49 / 74 61 / 93 53 - 99
Reply | Threaded
Open this post in threaded view
|

Re: Replace all on a line

Matthew Winn
In reply to this post by Jerin Joy
On Mon, Jun 26, 2006 at 04:48:25PM +0530, Jerin Joy wrote:

> The replace all option 'g' doesn't seem to be working in my case. It
> replaces only the first occurrence on a line.
>
> for eg. in the following line I wanted to remove the extra whitespaces
> between input [] and the name of the variable:
> task init (input [1:0]   u, input [3:0]    hash, input [3:0]    hash_index1)
>
> Using:
> :%s/\][\ ]\+/\]\ /g
>
> replaces only for the first variable - input [1:0] u

It works for me.  Are you sure the characters in the gaps are all spaces
and there are no tabs in there?

Also, you can simplify your expression.  You don't need to backslash
the characters in the replacement, and the [\ ] can be replaced with
a literal space:

    :%s/] \+/] /g

If there are tabs in the line you can use \s instead of space in the
pattern:

    :%s/]\s\+/] /g

--
Matthew Winn ([hidden email])
Reply | Threaded
Open this post in threaded view
|

RE: Replace all on a line

vishnu-3
In reply to this post by Jerin Joy
Hi Jerin,

Your regular expression is working fine for me (Vim 7.0).

You can simplify your regular exp. As follows:
:%s/]\s\+/] /g

\s -> whitespaces (includes tab character also)

~Vishnu

-----Original Message-----
From: Jerin Joy [mailto:[hidden email]]
Sent: Monday, June 26, 2006 4:48 PM
To: [hidden email]
Subject: Replace all on a line

Hi.

The replace all option 'g' doesn't seem to be working in my case. It
replaces only the first occurrence on a line.

for eg. in the following line I wanted to remove the extra whitespaces
between input [] and the name of the variable:
task init (input [1:0]   u, input [3:0]    hash, input [3:0]
hash_index1)

Using:
:%s/\][\ ]\+/\]\ /g

replaces only for the first variable - input [1:0] u

Jerin
--
http://jerinj.blogspot.com/
--

Reply | Threaded
Open this post in threaded view
|

Re: Replace all on a line

Jerin Joy
In reply to this post by Matthew Winn
Hi,

I tried the command with the \s instead of '\ ' and it doesn't work

the command :%s/]\s\+/] /g changed
task init (input [1:0]   u, input [3:0]    hash, input [3:0]    hash_index1)

to
task init (input [1:0] u, input [3:0]    hash, input [3:0]    hash_index1)

Could something be hardcoded in my settings? I'm running vim 6.4.6 the
default version with ubuntu 6.06.

Jerin

On 6/26/06, Matthew Winn <[hidden email]> wrote:

> On Mon, Jun 26, 2006 at 04:48:25PM +0530, Jerin Joy wrote:
> > The replace all option 'g' doesn't seem to be working in my case. It
> > replaces only the first occurrence on a line.
> >
> > for eg. in the following line I wanted to remove the extra whitespaces
> > between input [] and the name of the variable:
> > task init (input [1:0]   u, input [3:0]    hash, input [3:0]    hash_index1)
> >
> > Using:
> > :%s/\][\ ]\+/\]\ /g
> >
> > replaces only for the first variable - input [1:0] u
>
> It works for me.  Are you sure the characters in the gaps are all spaces
> and there are no tabs in there?
>
> Also, you can simplify your expression.  You don't need to backslash
> the characters in the replacement, and the [\ ] can be replaced with
> a literal space:
>
>     :%s/] \+/] /g
>
> If there are tabs in the line you can use \s instead of space in the
> pattern:
>
>     :%s/]\s\+/] /g
>
> --
> Matthew Winn ([hidden email])
>


--
http://jerinj.blogspot.com/
--
Reply | Threaded
Open this post in threaded view
|

Re: Replace all on a line

Jürgen Krämer

Hi,

Jerin Joy wrote:

>
> I tried the command with the \s instead of '\ ' and it doesn't work
>
> the command :%s/]\s\+/] /g changed
> task init (input [1:0]   u, input [3:0]    hash, input [3:0]    hash_index1)
>
> to
> task init (input [1:0] u, input [3:0]    hash, input [3:0]    hash_index1)
>
> Could something be hardcoded in my settings? I'm running vim 6.4.6 the
> default version with ubuntu 6.06.

maybe the 'gdefault' option is on. What does

  :verbose set gdefault?

output?

Regards,
Jürgen

--
Jürgen Krämer                              Softwareentwicklung
HABEL GmbH & Co. KG                        mailto:[hidden email]
Hinteres Öschle 2                          Tel: +49 / 74 61 / 93 53 - 15
78604 Rietheim-Weilheim                    Fax: +49 / 74 61 / 93 53 - 99
Reply | Threaded
Open this post in threaded view
|

Re: Replace all on a line

Jerin Joy
> maybe the 'gdefault' option is on. What does
>
>   :verbose set gdefault?
> output?
it doesn't output anything. Cursor just comes back to the file.

Jerin

>
> Regards,
> Jürgen
>
> --
> Jürgen Krämer                              Softwareentwicklung
> HABEL GmbH & Co. KG                        mailto:[hidden email]
> Hinteres Öschle 2                          Tel: +49 / 74 61 / 93 53 - 15
> 78604 Rietheim-Weilheim                    Fax: +49 / 74 61 / 93 53 - 99
>


--
http://jerinj.blogspot.com/
--
Reply | Threaded
Open this post in threaded view
|

Re: Replace all on a line

Jürgen Krämer

Hi,

Jerin Joy wrote:
> > maybe the 'gdefault' option is on. What does
> >
> >   :verbose set gdefault?
> > output?
>
> it doesn't output anything. Cursor just comes back to the file.

did you enter the question mark after "gdefault"? This is necessary to
query the state of a boolean option.

Mit freundlichen Grüßen,
Jürgen Krämer

--
Jürgen Krämer                              Softwareentwicklung
HABEL GmbH & Co. KG                        mailto:[hidden email]
Hinteres Öschle 2                          Tel: +49 / 74 61 / 93 53 - 15
78604 Rietheim-Weilheim                    Fax: +49 / 74 61 / 93 53 - 99
Reply | Threaded
Open this post in threaded view
|

Re: Replace all on a line

Jerin Joy
Sorry, missed the ?.
gdefault was the problem, it was set in my settings.vim file.

thanks!
Jerin

On 6/26/06, Jürgen Krämer <[hidden email]> wrote:

>
> Hi,
>
> Jerin Joy wrote:
> > > maybe the 'gdefault' option is on. What does
> > >
> > >   :verbose set gdefault?
> > > output?
> >
> > it doesn't output anything. Cursor just comes back to the file.
>
> did you enter the question mark after "gdefault"? This is necessary to
> query the state of a boolean option.
>
> Mit freundlichen Grüßen,
> Jürgen Krämer
>
> --
> Jürgen Krämer                              Softwareentwicklung
> HABEL GmbH & Co. KG                        mailto:[hidden email]
> Hinteres Öschle 2                          Tel: +49 / 74 61 / 93 53 - 15
> 78604 Rietheim-Weilheim                    Fax: +49 / 74 61 / 93 53 - 99
>


--
http://jerinj.blogspot.com/
--