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

Replacing a string which is in a line and not in the beginning?

Expand Messages
  • Niels Kobschaetzki
    Hi, I have a file that looks like this: - This is a te- st - foo bar And I want to replace all occurrences of - when they are not at the beginning of the
    Message 1 of 11 , Jan 25, 2014
      Hi,

      I have a file that looks like this:
      - This is a te- st
      - foo bar

      And I want to replace all occurrences of "- " when they are not at the
      beginning of the line. So the above mentioned file should look like:
      - This is a test
      - foo bar

      I tried to find something and it seems that in "normal" regex I would
      have to do something like ^.+(- .) or ^.?<pattern> but those don't work.
      What do I have to do to get the replacements I want?

      Thanks,

      Niels
    • Karig
      Niels Kobschätzki-2 wrote ... I m fairly new to Vim myself, but I just tried this. I entered this into a new buffer -- - This is a te- st - foo bar - This is
      Message 2 of 11 , Jan 25, 2014
        Niels Kobschätzki-2 wrote
        > I have a file that looks like this:
        > - This is a te- st
        > - foo bar
        >
        > And I want to replace all occurrences of "- " when they are not at the
        > beginning of the line. So the above mentioned file should look like:
        > - This is a test
        > - foo bar
        >
        > I tried to find something and it seems that in "normal" regex I would
        > have to do something like ^.+(- .) or ^.?
        > <pattern>
        > but those don't work.
        > What do I have to do to get the replacements I want?

        I'm fairly new to Vim myself, but I just tried this.

        I entered this into a new buffer --

        - This is a te- st
        - foo bar
        - This is another - test I mean

        and ran this from the command line (using "x" as the replacement text) --

        :%s/\v^(.+)\- /\1x/g

        and I got this:

        - This is a texst
        - foo bar
        - This is another xtest I mean

        Note that I used "\v" (very magic) to make Vim use regular expressions that
        the rest of the world understands. :-)



        --
        View this message in context: http://vim.1045645.n5.nabble.com/Replacing-a-string-which-is-in-a-line-and-not-in-the-beginning-tp5720146p5720148.html
        Sent from the Vim - General mailing list archive at Nabble.com.

        --
        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • ZyX
        ... Not as easy. According to the documentation only alphanumeric characters, underscore and non-ASCII are guaranteed not to have special meaning in this mode.
        Message 3 of 11 , Jan 25, 2014
          > I'm fairly new to Vim myself, but I just tried this.
          >
          > I entered this into a new buffer --
          >
          > - This is a te- st
          > - foo bar
          > - This is another - test I mean
          >
          > and ran this from the command line (using "x" as the replacement text) --
          >
          > :%s/\v^(.+)\- /\1x/g
          >
          > and I got this:
          >
          > - This is a texst
          > - foo bar
          > - This is another xtest I mean
          >
          > Note that I used "\v" (very magic) to make Vim use regular expressions that
          > the rest of the world understands. :-)

          Not as easy. According to the documentation only alphanumeric characters, underscore and non-ASCII are guaranteed not to have special meaning in this mode. I.e. you must escape the space just as you escaped hyphenminus. Both though *currently* have no special meaning.

          --
          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Marc Weber
          ... There are always two ways to cope with this if they are not . Fastest: (This assumses - is still meant to be beginnig of the line, thus even if
          Message 4 of 11 , Jan 25, 2014
            Excerpts from Niels Kobschaetzki's message of Sat Jan 25 11:37:24 +0100 2014:
            > And I want to replace all occurrences of "- " when they are not at the
            > beginning of the line. So the above mentioned file should look like:

            There are always two ways to cope with this "if they are not". Fastest:

            (This assumses " - " is still meant to be beginnig of the line, thus
            even if there are leading spaces)
            :%s/^(\s*)- /\1DONT_REPLACE_THIS/
            :%s/- //
            # substitute DONT_REPLACE_THIS back:
            :%s/^(\s*)DONT_REPLACE_THIS/\1- /

            second way is looking up all that "no match" magic in :h regexp
            Eg: |/\@!| \@! \@! nothing, requires NO match |/zero-width| (*)

            Marc Weber

            --
            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php

            ---
            You received this message because you are subscribed to the Google Groups "vim_use" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • John Little
            ... Regards, John Little -- -- You received this message from the vim_use maillist. Do not top-post! Type your reply below the text you are replying to. For
            Message 5 of 11 , Jan 25, 2014
              I suggest using \zs":

              :%s/.\zs- //g

              Regards, John Little

              --
              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php

              ---
              You received this message because you are subscribed to the Google Groups "vim_use" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            • Niels Kobschätzki
              ... Good to know about v. That would have solved it really fast ^^ Niels
              Message 6 of 11 , Jan 26, 2014
                On 25 Jan 2014, at 22:17, ZyX wrote:

                >> I'm fairly new to Vim myself, but I just tried this.
                >>
                >> I entered this into a new buffer --
                >>
                >> - This is a te- st
                >> - foo bar
                >> - This is another - test I mean
                >>
                >> and ran this from the command line (using "x" as the replacement text) --
                >>
                >> :%s/\v^(.+)\- /\1x/g
                >>
                >> and I got this:
                >>
                >> - This is a texst
                >> - foo bar
                >> - This is another xtest I mean
                >>
                >> Note that I used "\v" (very magic) to make Vim use regular expressions that
                >> the rest of the world understands. :-)
                >
                > Not as easy. According to the documentation only alphanumeric characters, underscore and non-ASCII are guaranteed not to have special meaning in this mode. I.e. you must escape the space just as you escaped hyphenminus. Both though *currently* have no special meaning.

                Good to know about \v. That would have solved it really fast ^^

                Niels
              • Niels Kobschätzki
                ... Good to know, I have to save this somewhere. Thanks. Niels
                Message 7 of 11 , Jan 26, 2014
                  On 26 Jan 2014, at 5:59, Marc Weber wrote:

                  > Excerpts from Niels Kobschaetzki's message of Sat Jan 25 11:37:24 +0100 2014:
                  >> And I want to replace all occurrences of "- " when they are not at the
                  >> beginning of the line. So the above mentioned file should look like:
                  >
                  > There are always two ways to cope with this "if they are not". Fastest:
                  >
                  > (This assumses " - " is still meant to be beginnig of the line, thus
                  > even if there are leading spaces)
                  > :%s/^(\s*)- /\1DONT_REPLACE_THIS/
                  > :%s/- //
                  > # substitute DONT_REPLACE_THIS back:
                  > :%s/^(\s*)DONT_REPLACE_THIS/\1- /
                  >
                  > second way is looking up all that "no match" magic in :h regexp
                  > Eg: |/\@!| \@! \@! nothing, requires NO match |/zero-width| (*)

                  Good to know, I have to save this somewhere. Thanks.

                  Niels
                • Niels Kobschätzki
                  ... It works but what does the /. in there? Niels
                  Message 8 of 11 , Jan 26, 2014
                    On 26 Jan 2014, at 6:21, John Little wrote:

                    > I suggest using \zs":
                    >
                    > :%s/.\zs- //g

                    It works but what does the /. in there?

                    Niels
                  • Christian Brabandt
                    Hi Niels! ... It is already in the help Best, Christian -- Wir nähern uns immer mehr der Entdeckung Gottes durch die Wissenschaft. Ich bange um sein
                    Message 9 of 11 , Jan 26, 2014
                      Hi Niels!

                      On So, 26 Jan 2014, Niels Kobschätzki wrote:

                      > On 26 Jan 2014, at 5:59, Marc Weber wrote:
                      >
                      > > Excerpts from Niels Kobschaetzki's message of Sat Jan 25 11:37:24 +0100 2014:
                      > >> And I want to replace all occurrences of "- " when they are not at the
                      > >> beginning of the line. So the above mentioned file should look like:
                      > >
                      > > There are always two ways to cope with this "if they are not". Fastest:
                      > >
                      > > (This assumses " - " is still meant to be beginnig of the line, thus
                      > > even if there are leading spaces)
                      > > :%s/^(\s*)- /\1DONT_REPLACE_THIS/
                      > > :%s/- //
                      > > # substitute DONT_REPLACE_THIS back:
                      > > :%s/^(\s*)DONT_REPLACE_THIS/\1- /
                      > >
                      > > second way is looking up all that "no match" magic in :h regexp
                      > > Eg: |/\@!| \@! \@! nothing, requires NO match |/zero-width| (*)
                      >
                      > Good to know, I have to save this somewhere. Thanks.

                      It is already in the help

                      Best,
                      Christian
                      --
                      Wir nähern uns immer mehr der Entdeckung Gottes durch die
                      Wissenschaft. Ich bange um sein Schicksal.
                      -- Stanislaw Jerzy Lec (eig. S. J. de Tusch-Letz)

                      --
                      --
                      You received this message from the "vim_use" maillist.
                      Do not top-post! Type your reply below the text you are replying to.
                      For more information, visit http://www.vim.org/maillist.php

                      ---
                      You received this message because you are subscribed to the Google Groups "vim_use" group.
                      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
                      For more options, visit https://groups.google.com/groups/opt_out.
                    • Tony Mechelynck
                      ... % range, the whole file s :s[ubstitute] command / from . any character zs what will be
                      Message 10 of 11 , Jan 26, 2014
                        On 26/01/14 09:04, Niels Kobschätzki wrote:
                        > On 26 Jan 2014, at 6:21, John Little wrote:
                        >
                        >> I suggest using \zs":
                        >>
                        >> :%s/.\zs- //g
                        >
                        > It works but what does the /. in there?
                        >
                        > Niels
                        >

                        : start of ex-command
                        % range, the whole file
                        s :s[ubstitute] command
                        / from
                        . any character
                        \zs what will be replaced starts here
                        - (dash-space) replace this
                        // by nothing
                        g everywhere in the line

                        IOW, replace dash-space by nothing (any number of times per line, in the
                        whole file), but only if preceded by something on the same line.

                        Best regards,
                        Tony.
                        --
                        Be valiant, but not too venturous.
                        Let thy attire be comely, but not costly.
                        -- John Lyly

                        --
                        --
                        You received this message from the "vim_use" maillist.
                        Do not top-post! Type your reply below the text you are replying to.
                        For more information, visit http://www.vim.org/maillist.php

                        ---
                        You received this message because you are subscribed to the Google Groups "vim_use" group.
                        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
                        For more options, visit https://groups.google.com/groups/opt_out.
                      • Sibin Thomas
                        You could also use - ... From :help % - % 23c Matches after a specific column. On Mon, Jan 27, 2014 at 10:59 AM, Tony Mechelynck
                        Message 11 of 11 , Jan 26, 2014
                          You could also use -
                          :%s/\%>2c- //g

                          From ":help \%>" - 
                                         \%>23c Matches after a specific column.
                           


                          On Mon, Jan 27, 2014 at 10:59 AM, Tony Mechelynck <antoine.mechelynck@...> wrote:
                          On 26/01/14 09:04, Niels Kobschätzki wrote:
                          On 26 Jan 2014, at 6:21, John Little wrote:

                          I suggest using \zs":

                          :%s/.\zs- //g

                          It works but what does the /. in there?

                          Niels


                             :                 start of ex-command
                              %                range, the whole file
                               s               :s[ubstitute] command
                                /              from
                                 .             any character
                                  \zs          what will be replaced starts here
                                     -         (dash-space) replace this
                                        //     by nothing
                                          g    everywhere in the line

                          IOW, replace dash-space by nothing (any number of times per line, in the whole file), but only if preceded by something on the same line.

                          Best regards,
                          Tony.
                          --
                          Be valiant, but not too venturous.
                          Let thy attire be comely, but not costly.
                                          -- John Lyly


                          --
                          --
                          You received this message from the "vim_use" maillist.
                          Do not top-post! Type your reply below the text you are replying to.
                          For more information, visit http://www.vim.org/maillist.php

                          --- You received this message because you are subscribed to the Google Groups "vim_use" group.
                          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
                          For more options, visit https://groups.google.com/groups/opt_out.



                          --
                          Regards,
                          Sibin

                          --
                          --
                          You received this message from the "vim_use" maillist.
                          Do not top-post! Type your reply below the text you are replying to.
                          For more information, visit http://www.vim.org/maillist.php
                           
                          ---
                          You received this message because you are subscribed to the Google Groups "vim_use" group.
                          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
                          For more options, visit https://groups.google.com/groups/opt_out.
                        Your message has been successfully submitted and would be delivered to recipients shortly.