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

systematic replacement of text

Expand Messages
  • Natércia Fernandes
    Hi, In a file with many occurrences of something like this: xxx{a variable number of words that I want to delete}{a variable (diferent) number of words that I
    Message 1 of 13 , Apr 9, 2014
    • 0 Attachment
      Hi,

      In a file with many occurrences of something like this:

      \xxx{a variable number of words that I want to delete}{a variable (diferent)
      number of words that I want to keep}

      I would like to eliminate at once in all occurrences in the file the following:
      \xxx
      the two pairs of {}
      the content of the first {} independently of its size (which can be different
      from occurrence to occurrence)

      That is, I want to keep only the content of the second pair of {} above.
      In this case, I would like to obtain:
      a variable (diferent) number of words that I want to keep

      \xxx{argument 1}{argument 2} can exist more than once in a line, but never
      will exist a \xxx in the arguments of another \xxx.

      If "argument 1" and "argument 2" were kept in all occurrences, I could solve
      my problem with something like this:
      :1,$ s/\\xxx{argument 1}{argument 2}/argument 2/g
      but my problem is that argument 1 and argument 2 are different from occurrence
      to occurrence.
      Don't know if this info is needed or not: both argument 1 and/or argument 2
      can contain the symbols \ {}.

      Can someone help me?

      Thank you very much,
      Natércia

      --
      --
      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/d/optout.
    • Claus Atzenbeck
      ... A possibility is to do ... Possibly there are solutions with less characters. Best, Claus -- -- You received this message from the vim_use maillist. Do
      Message 2 of 13 , Apr 9, 2014
      • 0 Attachment
        On Wed, 9 Apr 2014 at 11:09am +0100, Natércia Fernandes wrote:

        > \xxx{argument 1}{argument 2} can exist more than once in a line, but never
        > will exist a \xxx in the arguments of another \xxx.
        >
        > If "argument 1" and "argument 2" were kept in all occurrences, I could solve
        > my problem with something like this:
        > :1,$ s/\\xxx{argument 1}{argument 2}/argument 2/g
        > but my problem is that argument 1 and argument 2 are different from occurrence
        > to occurrence.

        A possibility is to do

        :%s/\\xxx{\_.\{-}}{\(\_.\{-}\)}/\1/g

        Possibly there are solutions with less characters.

        Best,
        Claus

        --
        --
        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/d/optout.
      • Jean-Rene David
        ... [...] ... Not only is it needed, it changes everything. ... But that won t work if the arguments can contain } . That simple fact makes the problem a lot
        Message 3 of 13 , Apr 9, 2014
        • 0 Attachment
          * Natércia Fernandes [2014.04.09 07:02]:
          > In a file with many occurrences of something like this:
          >
          > \xxx{a variable number of words that I want to delete}{a variable (diferent)
          > number of words that I want to keep}

          [...]

          > Don't know if this info is needed or not: both argument 1 and/or argument 2
          > can contain the symbols \ {}.

          Not only is it needed, it changes everything.

          I was going to suggest:

          :%s/\\xxx{\_[^}]*}{\(\_[^}]*\)}/\1/g

          But that won't work if the arguments can contain
          '}'. That simple fact makes the problem a lot
          harder.

          --
          JR

          --
          --
          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/d/optout.
        • Karthick Gururaj
          ... But will the symbols be balanced? It must be - otherwise, how can we even tell what is argument 1 ? Is it LaTeX? ... If the OP is not specifically looking
          Message 4 of 13 , Apr 9, 2014
          • 0 Attachment
            On Wed, Apr 9, 2014 at 5:28 PM, Jean-Rene David <vim_use@...> wrote:
            > * Natércia Fernandes [2014.04.09 07:02]:
            >> In a file with many occurrences of something like this:
            >>
            >> \xxx{a variable number of words that I want to delete}{a variable (diferent)
            >> number of words that I want to keep}
            >
            > [...]
            >
            >> Don't know if this info is needed or not: both argument 1 and/or argument 2
            >> can contain the symbols \ {}.
            But will the symbols be balanced? It must be - otherwise, how can we
            even tell what is 'argument 1'? Is it LaTeX?

            >
            > Not only is it needed, it changes everything.
            >
            > I was going to suggest:
            >
            > :%s/\\xxx{\_[^}]*}{\(\_[^}]*\)}/\1/g
            >
            > But that won't work if the arguments can contain
            > '}'. That simple fact makes the problem a lot
            > harder.
            If the OP is not specifically looking to use the :substitute, a macro
            might also do the work?

            Search for \\xxx, go to end of the word and do d% for deleting all
            characters until a matching brace. Repeat the macro 'N' times, where N
            is sufficiently large. Make sure wrapscan is off.

            --
            --
            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/d/optout.
          • Ivan Ivanivich
            ... sometimes it s better to use sed or awk -- -- You received this message from the vim_use maillist. Do not top-post! Type your reply below the text you
            Message 5 of 13 , Apr 9, 2014
            • 0 Attachment
              On Wednesday, April 9, 2014 2:09:05 PM UTC+4, natercia wrote:
              > Hi,
              >
              >
              >
              > In a file with many occurrences of something like this:
              >
              >
              >
              > \xxx{a variable number of words that I want to delete}{a variable (diferent)
              >
              > number of words that I want to keep}
              >
              >
              >
              > I would like to eliminate at once in all occurrences in the file the following:
              >
              > \xxx
              >
              > the two pairs of {}
              >
              > the content of the first {} independently of its size (which can be different
              >
              > from occurrence to occurrence)
              >
              >
              >
              > That is, I want to keep only the content of the second pair of {} above.
              >
              > In this case, I would like to obtain:
              >
              > a variable (diferent) number of words that I want to keep
              >
              >
              >
              > \xxx{argument 1}{argument 2} can exist more than once in a line, but never
              >
              > will exist a \xxx in the arguments of another \xxx.
              >
              >
              >
              > If "argument 1" and "argument 2" were kept in all occurrences, I could solve
              >
              > my problem with something like this:
              >
              > :1,$ s/\\xxx{argument 1}{argument 2}/argument 2/g
              >
              > but my problem is that argument 1 and argument 2 are different from occurrence
              >
              > to occurrence.
              >
              > Don't know if this info is needed or not: both argument 1 and/or argument 2
              >
              > can contain the symbols \ {}.
              >
              >
              >
              > Can someone help me?
              >
              >
              >
              > Thank you very much,
              >
              > Natércia

              sometimes it's better to use sed or awk

              --
              --
              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/d/optout.
            • LCD 47
              ... Write a macro. Make it recursive to get all matches. Let % take care of balancing { and } and handle escapes. qqq - clear out register q qq - start
              Message 6 of 13 , Apr 9, 2014
              • 0 Attachment
                On 9 April 2014, Natércia Fernandes <natercia@...> wrote:
                > Hi,
                >
                > In a file with many occurrences of something like this:
                >
                > \xxx{a variable number of words that I want to delete}{a variable (diferent)
                > number of words that I want to keep}
                >
                > I would like to eliminate at once in all occurrences in the file the following:
                > \xxx
                > the two pairs of {}
                > the content of the first {} independently of its size (which can be different
                > from occurrence to occurrence)
                >
                > That is, I want to keep only the content of the second pair of {} above.
                > In this case, I would like to obtain:
                > a variable (diferent) number of words that I want to keep
                >
                > \xxx{argument 1}{argument 2} can exist more than once in a line, but never
                > will exist a \xxx in the arguments of another \xxx.
                >
                > If "argument 1" and "argument 2" were kept in all occurrences, I could solve
                > my problem with something like this:
                > :1,$ s/\\xxx{argument 1}{argument 2}/argument 2/g
                > but my problem is that argument 1 and argument 2 are different from occurrence
                > to occurrence.
                > Don't know if this info is needed or not: both argument 1 and/or argument 2
                > can contain the symbols \ {}.
                >
                > Can someone help me?

                Write a macro. Make it recursive to get all matches. Let % take
                care of balancing { and } and handle \ escapes.

                qqq - clear out register q
                qq - start recording a macro in register q
                /\\xxx\> - search for \xxx
                ^M - press enter to finish the search
                dt{ - delete to the next {
                d% - delete to the matching } (deletes {argument 1})
                l - move right
                "x - in register x...
                y - ... yank...
                ]} - ... to the matching } (saves argument 2)
                h - move right
                d% - delete to the matching } (deletes {argument 2})
                "xP - paste from register x (pastes argument 2)
                @q - make the macro invoke itself
                q - finish macro recording.

                Now run @q and watch.

                /lcd

                --
                --
                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/d/optout.
              • LCD 47
                ... True. Except in this particular situation it s pretty hard to solve the problem with awk, and next to impossible to do it with sed. :) /lcd -- -- You
                Message 7 of 13 , Apr 9, 2014
                • 0 Attachment
                  On 9 April 2014, Ivan Ivanivich <ivriabtsov@...> wrote:
                  > sometimes it's better to use sed or awk

                  True. Except in this particular situation it's pretty hard to solve
                  the problem with awk, and next to impossible to do it with sed. :)

                  /lcd

                  --
                  --
                  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/d/optout.
                • LCD 47
                  ... A shorter version, with a less awkward way to remove the { ... } around argument 2 : qqq - clear out register q qq - start recording a macro in register
                  Message 8 of 13 , Apr 9, 2014
                  • 0 Attachment
                    On 9 April 2014, LCD 47 <lcd047@...> wrote:
                    > On 9 April 2014, Natércia Fernandes <natercia@...> wrote:
                    > > Hi,
                    > >
                    > > In a file with many occurrences of something like this:
                    > >
                    > > \xxx{a variable number of words that I want to delete}{a variable (diferent)
                    > > number of words that I want to keep}
                    > >
                    > > I would like to eliminate at once in all occurrences in the file the following:
                    > > \xxx
                    > > the two pairs of {}
                    > > the content of the first {} independently of its size (which can be different
                    > > from occurrence to occurrence)
                    > >
                    > > That is, I want to keep only the content of the second pair of {} above.
                    > > In this case, I would like to obtain:
                    > > a variable (diferent) number of words that I want to keep
                    > >
                    > > \xxx{argument 1}{argument 2} can exist more than once in a line, but never
                    > > will exist a \xxx in the arguments of another \xxx.
                    > >
                    > > If "argument 1" and "argument 2" were kept in all occurrences, I could solve
                    > > my problem with something like this:
                    > > :1,$ s/\\xxx{argument 1}{argument 2}/argument 2/g
                    > > but my problem is that argument 1 and argument 2 are different from occurrence
                    > > to occurrence.
                    > > Don't know if this info is needed or not: both argument 1 and/or argument 2
                    > > can contain the symbols \ {}.
                    > >
                    > > Can someone help me?
                    >
                    > Write a macro. Make it recursive to get all matches. Let % take
                    > care of balancing { and } and handle \ escapes.
                    >
                    > qqq - clear out register q
                    > qq - start recording a macro in register q
                    > /\\xxx\> - search for \xxx
                    > ^M - press enter to finish the search
                    > dt{ - delete to the next {
                    > d% - delete to the matching } (deletes {argument 1})
                    > l - move right
                    > "x - in register x...
                    > y - ... yank...
                    > ]} - ... to the matching } (saves argument 2)
                    > h - move right
                    > d% - delete to the matching } (deletes {argument 2})
                    > "xP - paste from register x (pastes argument 2)
                    > @q - make the macro invoke itself
                    > q - finish macro recording.
                    >
                    > Now run @q and watch.

                    A shorter version, with a less awkward way to remove the { ... }
                    around "argument 2":

                    qqq - clear out register q
                    qq - start recording a macro in register q
                    /\\xxx\> - search for \xxx
                    ^M - press enter to finish the search
                    dt{ - delete to the next {
                    d% - delete to the matching } (deletes {argument 1})
                    % - jump to the final }
                    x - delete the final }
                    `` - go back to the position before jump
                    x - delete the {
                    @q - make the macro invoke itself
                    q - finish macro recording.

                    /lcd

                    --
                    --
                    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/d/optout.
                  • Natércia Fernandes
                    Hi again LCD, I ve got an interesting situation where the suggestion below fails: This is a text line. And the next will be a completely new line.
                    Message 9 of 13 , Apr 9, 2014
                    • 0 Attachment
                      Hi again LCD,

                      I've got an interesting situation where the suggestion below fails:

                      This is a text line. And the next will be a completely new line.
                      \xxx{\%danger}{}
                      Another line of text.

                      The change isn't performed as it was supposed...
                      Instead of nothing in that middle line it leaves a {

                      The `` seems to work in a different way here.

                      Otherwise, it has passed all the tests :)

                      Natércia


                      > A shorter version, with a less awkward way to remove the { ... }
                      > around "argument 2":
                      >
                      > qqq - clear out register q
                      > qq - start recording a macro in register q
                      > /\\xxx\> - search for \xxx
                      > ^M - press enter to finish the search
                      > dt{ - delete to the next {
                      > d% - delete to the matching } (deletes {argument 1})
                      > % - jump to the final }
                      > x - delete the final }
                      > `` - go back to the position before jump
                      > x - delete the {
                      > @q - make the macro invoke itself
                      > q - finish macro recording.
                      >
                      > /lcd

                      --
                      --
                      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/d/optout.
                    • LCD 47
                      ... I can t reproduce this with this exact macro: / xxx ^Mdt{d%%x``x@q The result I get is what I d expect: This is a text line. And the next will be a
                      Message 10 of 13 , Apr 9, 2014
                      • 0 Attachment
                        On 9 April 2014, Natércia Fernandes <natercia@...> wrote:
                        > Hi again LCD,
                        >
                        > I've got an interesting situation where the suggestion below fails:
                        >
                        > This is a text line. And the next will be a completely new line.
                        > \xxx{\%danger}{}
                        > Another line of text.
                        >
                        > The change isn't performed as it was supposed... Instead of nothing
                        > in that middle line it leaves a {
                        >
                        > The `` seems to work in a different way here.
                        >
                        > Otherwise, it has passed all the tests :)

                        I can't reproduce this with this exact macro:

                        /\\xxx\>^Mdt{d%%x``x@q

                        The result I get is what I'd expect:

                        This is a text line. And the next will be a completely new line.

                        Another line of text.

                        Vim 7.4.253, editing a LaTeX file (this might be relevant).

                        /lcd

                        --
                        --
                        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/d/optout.
                      • Natércia Fernandes
                        ... Hi, that is strange. I ve tried again and that behavior persists. I m using Vim 7.4 and I m editing a LaTeX file. I m attaching a minimal file-example:
                        Message 11 of 13 , Apr 9, 2014
                        • 0 Attachment

                          On Wednesday, April 09, 2014 07:58:15 PM you wrote:

                          > I can't reproduce this with this exact macro:

                          >

                          > /\\xxx\>^Mdt{d%%x``x@q

                          >

                          > The result I get is what I'd expect:

                          >

                          > This is a text line. And the next will be a completely new line.

                          >

                          > Another line of text.

                          >

                          > Vim 7.4.253, editing a LaTeX file (this might be relevant).

                          >

                          > /lcd

                           

                          Hi,

                          that is strange. I've tried again and that behavior persists.

                          I'm using Vim 7.4 and I'm editing a LaTeX file. I'm attaching a minimal file-example:

                          before.tex having the original source

                          after.tex having what I get after I applied the macro indicated above.

                           

                          All the occurrences get transformed ok (even the last one, more complicated) but not the second one.

                           

                          I also tried to apply the commands that make the macro directly to that 2nd occurrence and it fails when trying to do the `` command. Notice that it deletes one character (as x says) but it the wrong place (at the beginning of the previous xxx that had been already executed because it was from there it had jumped before). `` picks up the wrong jump, instead of the last, it picks up the last but one.

                           

                          Before creating the macro I position the cursor for instance in the line

                          \begin{document}

                          so that that zero occurrence of xxx (where I define the command \xxx.

                           

                          Am I overlooking something?

                          Natércia

                           

                           

                          --
                          --
                          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/d/optout.
                        • LCD 47
                          ... I see. The difference comes from virtualedit: I have it set to all, you seem to have it left to the default (which is unset). As a workaround, you can
                          Message 12 of 13 , Apr 9, 2014
                          • 0 Attachment
                            On 9 April 2014, Natércia Fernandes <natercia@...> wrote:
                            > On Wednesday, April 09, 2014 07:58:15 PM you wrote:
                            > > I can't reproduce this with this exact macro:
                            > >
                            > > /\\xxx\>^Mdt{d%%x``x@q
                            > >
                            > > The result I get is what I'd expect:
                            > >
                            > > This is a text line. And the next will be a completely new line.
                            > >
                            > > Another line of text.
                            > >
                            > > Vim 7.4.253, editing a LaTeX file (this might be relevant).
                            > >
                            > > /lcd
                            >
                            > Hi,
                            > that is strange. I've tried again and that behavior persists. I'm
                            > using Vim 7.4 and I'm editing a LaTeX file. I'm attaching a minimal
                            > file-example:
                            > before.tex having the original source
                            > after.tex having what I get after I applied the macro indicated
                            > above.
                            >
                            > All the occurrences get transformed ok (even the last one, more
                            > complicated) but not the second one.
                            >
                            > I also tried to apply the commands that make the macro directly to
                            > that 2nd occurrence and it fails when trying to do the `` command.
                            > Notice that it deletes one character (as x says) but it the wrong
                            > place (at the beginning of the previous xxx that had been already
                            > executed because it was from there it had jumped before). `` picks up
                            > the wrong jump, instead of the last, it picks up the last but one.
                            >
                            > Before creating the macro I position the cursor for instance in the
                            > line \begin{document} so that that zero occurrence of xxx (where I
                            > define the command \xxx.
                            >
                            > Am I overlooking something?

                            I see. The difference comes from virtualedit: I have it set to
                            all, you seem to have it left to the default (which is unset). As a
                            workaround, you can triple the last %, so that `` jumps to the right
                            place. :) It's also probably a good idea to search for \\xxx{ rather
                            than \\xxx\>, to avoid matching the definition of \xxx. To summarize
                            it:

                            /\\xxx{^Mdt{d%%%%x``x@qq
                            ^ ^^

                            /lcd

                            --
                            --
                            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/d/optout.
                          • Natércia Fernandes
                            ... Hi again, I already tried the macro with virtualedit set to all and it works just fine. Thank you everyone who replied to my request :) I really
                            Message 13 of 13 , Apr 11, 2014
                            • 0 Attachment
                              On Wednesday, April 09, 2014 09:41:53 PM you wrote:
                              > > Am I overlooking something?
                              >
                              > I see. The difference comes from virtualedit: I have it set to
                              > all, you seem to have it left to the default (which is unset). As a
                              > workaround, you can triple the last %, so that `` jumps to the right
                              > place. It's also probably a good idea to search for \\xxx{ rather
                              > than \\xxx\>, to avoid matching the definition of \xxx. To summarize
                              > it:
                              >
                              > /\\xxx{^Mdt{d%%%%x``x@qq
                              > ^ ^^
                              >
                              > /lcd

                              Hi again,
                              I already tried the macro with virtualedit set to all and it works just fine.
                              Thank you everyone who replied to my request :)
                              I really appreciated it!
                              Natércia

                              --
                              --
                              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/d/optout.
                            Your message has been successfully submitted and would be delivered to recipients shortly.