Loading ...
Sorry, an error occurred while loading the content.

Re: search and replace question

Expand Messages
  • Scott LaBounty
    ... Cory, Take out the square brakets and add backslashes on the parens in other ... Scott LaBounty Nexa Technologies, Inc.
    Message 1 of 10 , Apr 1 10:00 AM
    • 0 Attachment
      Cory Riddell wrote:

      >I have a file that I want to do a search and replace for any lines that
      >start with two (and only two) spaces followed by a non-space character
      >and replace it with two spaces, an asterisk, a space, then the non-space
      >character. For example, I want to replace this:
      > Some Text
      > and some more text
      >with
      > * Some Text
      > * and some more text
      >
      >I tried this command
      >:%s/^ ([\S])/ * \1/g
      >
      >but it just tells me the search pattern was not found. Can anybody help
      >me find the right expression?
      >
      >Cory
      >
      >
      >
      >
      >________________________________________________________________________
      >nexatechnologies.com - Inbound email scanned for viruses by MessageLabs.
      >
      >
      >
      Cory,

      Take out the square brakets and add backslashes on the parens in other
      words:

      :%s/^ \(\S\)/ * \1/g

      Scott LaBounty
      Nexa Technologies, Inc.

      ________________________________________________________________________
      This nexatechnologies.com email has been scanned for all viruses by the MessageLabs Email Security System.
    • t. scott urban
      ... You have to escape ( and ) used for submatching (unless there is some option I m not aware of). The brackets around the non-space specifier are not
      Message 2 of 10 , Apr 1 10:01 AM
      • 0 Attachment
        On Fri, 2005-04-01 at 17:51 +0000, Cory Riddell wrote:
        > I have a file that I want to do a search and replace for any lines that
        > start with two (and only two) spaces followed by a non-space character
        > and replace it with two spaces, an asterisk, a space, then the non-space
        > character. For example, I want to replace this:
        > Some Text
        > and some more text
        > with
        > * Some Text
        > * and some more text
        >
        > I tried this command
        > :%s/^ ([\S])/ * \1/g

        You have to escape ( and ) used for submatching (unless there is some
        option I'm not aware of). The brackets around the non-space specifier
        are not needed, either. Since the expression can only match once per
        line because of the beginning of line anchor, the 'g' option is not
        needed either.

        :%s/^ \(\S\)/ * \1/

        --
        t. scott urban <scottu@...>
      • Cory Riddell
        Thanks all for the amazingly quick replies. Cory
        Message 3 of 10 , Apr 1 10:06 AM
        • 0 Attachment
          Thanks all for the amazingly quick replies.

          Cory
        • Keith Roberts
          ... -Keith
          Message 4 of 10 , Apr 4 9:59 AM
          • 0 Attachment
            >-----Original Message-----
            >From: t. scott urban [mailto:scottu@...]
            >Sent: Friday, April 01, 2005 10:01 AM
            >To: criddell@...
            >Cc: vim@...
            >Subject: Re: search and replace question
            >
            >On Fri, 2005-04-01 at 17:51 +0000, Cory Riddell wrote:
            >> I have a file that I want to do a search and replace for any lines
            >> that start with two (and only two) spaces followed by a non-space
            >> character and replace it with two spaces, an asterisk, a space, then
            >> the non-space character. For example, I want to replace this:
            >> Some Text
            >> and some more text
            >> with
            >> * Some Text
            >> * and some more text
            >>
            >> I tried this command
            >> :%s/^ ([\S])/ * \1/g
            >
            >You have to escape ( and ) used for submatching (unless there
            >is some option I'm not aware of). The brackets around the
            >non-space specifier are not needed, either. Since the
            >expression can only match once per line because of the
            >beginning of line anchor, the 'g' option is not needed either.
            >
            >:%s/^ \(\S\)/ * \1/

            :%s/^ (\S)\v/ * \1/ will also work.

            -Keith
          • jamessan@jamessan.com
            ... James -- GPG Key: 1024D/61326D40 2003-09-02 James Vega
            Message 5 of 10 , Apr 4 10:45 AM
            • 0 Attachment
              On Mon, Apr 04, 2005 at 09:59:21AM -0700, Keith Roberts wrote:
              > >-----Original Message-----
              > >From: t. scott urban [mailto:scottu@...]
              > >Sent: Friday, April 01, 2005 10:01 AM
              > >To: criddell@...
              > >Cc: vim@...
              > >Subject: Re: search and replace question
              > >
              > >On Fri, 2005-04-01 at 17:51 +0000, Cory Riddell wrote:
              > >> I have a file that I want to do a search and replace for any lines
              > >> that start with two (and only two) spaces followed by a non-space
              > >> character and replace it with two spaces, an asterisk, a space, then
              > >> the non-space character. For example, I want to replace this:
              > >> Some Text
              > >> and some more text
              > >> with
              > >> * Some Text
              > >> * and some more text
              > >>
              > >> I tried this command
              > >> :%s/^ ([\S])/ * \1/g
              > >
              > >You have to escape ( and ) used for submatching (unless there
              > >is some option I'm not aware of). The brackets around the
              > >non-space specifier are not needed, either. Since the
              > >expression can only match once per line because of the
              > >beginning of line anchor, the 'g' option is not needed either.
              > >
              > >:%s/^ \(\S\)/ * \1/
              >
              > :%s/^ (\S)\v/ * \1/ will also work.

              I think you meant:

              :%s/\v^ (\S)/ * \1/

              James
              --
              GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
            • Keith Roberts
              ... Aha! Thanks for the correction. As I never use it, I hadn t realized that v is a positional flag (like U and E) rather than one which affects the
              Message 6 of 10 , Apr 4 2:21 PM
              • 0 Attachment
                >-----Original Message-----
                >From: jamessan@... [mailto:jamessan@...]
                >Sent: Monday, April 04, 2005 9:45 AM
                >To: vim@...
                >Subject: Re: search and replace question
                >
                >On Mon, Apr 04, 2005 at 09:59:21AM -0700, Keith Roberts wrote:
                >> >-----Original Message-----
                >> >From: t. scott urban [mailto:scottu@...]
                >> >Sent: Friday, April 01, 2005 10:01 AM
                >> >To: criddell@...
                >> >Cc: vim@...
                >> >Subject: Re: search and replace question
                >> >
                >> >On Fri, 2005-04-01 at 17:51 +0000, Cory Riddell wrote:
                >> >> I have a file that I want to do a search and replace for
                >any lines
                >> >> that start with two (and only two) spaces followed by a non-space
                >> >> character and replace it with two spaces, an asterisk, a space,
                >> >> then the non-space character. For example, I want to replace this:
                >> >> Some Text
                >> >> and some more text
                >> >> with
                >> >> * Some Text
                >> >> * and some more text
                >> >>
                >> >> I tried this command
                >> >> :%s/^ ([\S])/ * \1/g
                >> >
                >> >You have to escape ( and ) used for submatching (unless
                >there is some
                >> >option I'm not aware of). The brackets around the non-space
                >> >specifier are not needed, either. Since the expression can only
                >> >match once per line because of the beginning of line
                >anchor, the 'g'
                >> >option is not needed either.
                >> >
                >> >:%s/^ \(\S\)/ * \1/
                >>
                >> :%s/^ (\S)\v/ * \1/ will also work.
                >
                >I think you meant:
                >
                >:%s/\v^ (\S)/ * \1/
                >
                >James

                Aha! Thanks for the correction. As I never use it, I hadn't realized
                that \v is a positional flag (like \U and \E) rather than one which
                affects the entire regex (like \c).

                -Keith
              • jamessan@jamessan.com
                ... I actually didn t realize v (or c) was a positional flag. I had always just seen it/used it at the head of the pattern and noticed that the expression
                Message 7 of 10 , Apr 4 8:12 PM
                • 0 Attachment
                  On Mon, Apr 04, 2005 at 02:21:40PM -0700, Keith Roberts wrote:
                  > >-----Original Message-----
                  > >From: jamessan@... [mailto:jamessan@...]
                  > >Sent: Monday, April 04, 2005 9:45 AM
                  > >To: vim@...
                  > >Subject: Re: search and replace question
                  > >
                  > >On Mon, Apr 04, 2005 at 09:59:21AM -0700, Keith Roberts wrote:
                  > >>
                  > >> :%s/^ (\S)\v/ * \1/ will also work.
                  > >
                  > >I think you meant:
                  > >
                  > >:%s/\v^ (\S)/ * \1/
                  > >
                  > >James
                  >
                  > Aha! Thanks for the correction. As I never use it, I hadn't realized
                  > that \v is a positional flag (like \U and \E) rather than one which
                  > affects the entire regex (like \c).

                  I actually didn't realize \v (or \c) was a positional flag. I had
                  always just seen it/used it at the head of the pattern and noticed that
                  the expression you had provided didn't work when I tried it. We both
                  learned something. :)

                  James
                  --
                  GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
                • jamessan@jamessan.com
                  ... And if I could compose my thoughts correctly, I d actually be able to convey what it was I learned. I meant to say that I didn t realize v or c could be
                  Message 8 of 10 , Apr 4 9:20 PM
                  • 0 Attachment
                    On Mon, Apr 04, 2005 at 11:12:16PM -0400, jamessan@... wrote:
                    > On Mon, Apr 04, 2005 at 02:21:40PM -0700, Keith Roberts wrote:
                    > > >-----Original Message-----
                    > > >From: jamessan@... [mailto:jamessan@...]
                    > > >Sent: Monday, April 04, 2005 9:45 AM
                    > > >To: vim@...
                    > > >Subject: Re: search and replace question
                    > > >
                    > > >On Mon, Apr 04, 2005 at 09:59:21AM -0700, Keith Roberts wrote:
                    > > >>
                    > > >> :%s/^ (\S)\v/ * \1/ will also work.
                    > > >
                    > > >I think you meant:
                    > > >
                    > > >:%s/\v^ (\S)/ * \1/
                    > > >
                    > > >James
                    > >
                    > > Aha! Thanks for the correction. As I never use it, I hadn't realized
                    > > that \v is a positional flag (like \U and \E) rather than one which
                    > > affects the entire regex (like \c).
                    >
                    > I actually didn't realize \v (or \c) was a positional flag. I had
                    > always just seen it/used it at the head of the pattern and noticed that
                    > the expression you had provided didn't work when I tried it. We both
                    > learned something. :)

                    And if I could compose my thoughts correctly, I'd actually be able to
                    convey what it was I learned. I meant to say that I didn't realize \v
                    or \c could be used anywhere other than the head of the pattern.

                    James
                    --
                    GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
                  Your message has been successfully submitted and would be delivered to recipients shortly.