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

Re: Search & Replace

Expand Messages
  • Benjamin R. Haskell
    ... Tim gave you one possible answer, with how to match it with a pattern. Often more useful are the tag block motions. e.g. with the cursor anywhere inside
    Message 1 of 10 , Apr 1 9:24 PM
    • 0 Attachment
      On Thu, 1 Apr 2010, Kunal Bajpai wrote:

      > I have been using Vim for about 3-4 months and am a student web
      > developer. I am still learning Vim and need help with a command. I am
      > editing some web pages using web pages many a times I have to edit web
      > pages. Here is what I want:
      >
      > ______________________________
      > <p>Mike
      > Gilbert
      > Senior</p>
      > _____________________________
      >
      > I need a command that selects the whole html code including the
      > newline character.

      Tim gave you one possible answer, with how to match it with a pattern.

      Often more useful are the 'tag block' motions.

      e.g. with the cursor anywhere inside your example HTML, you can type

      'dat' in normal mode to 'd'elete 'a' 't'ag. (includes the <p> and </p>)
      'dit' in normal mode to 'd'elete 'i'nner 't'ag. (just inside the tags)

      the 'at' and 'it' are the motions. 'd' is just one example command that
      takes a motion. Other particularly useful ones, 'y'ank, 'v'isually-select.

      :help tag-blocks
      :help {motion}

      --
      Best,
      Ben

      --
      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

      To unsubscribe, reply using "remove me" as the subject.
    • Kunal Bajpai
      To this html code I wanted to select the text between the tags and replace the original text with n character removed. There is a more complex example that I
      Message 2 of 10 , Apr 1 9:40 PM
      • 0 Attachment
        To this html code I wanted to select the text between the tags and replace the original text with \n character removed. There is a more complex example that I wanted to ask, here is the problem:

        Original
        ______________________________
        <p>Mike
        <strong>Gilbert </strong>
        Senior</p>
        _____________________________

        After search & replace:
        ______________________________
        <p>Mike <strong>Gilbert</strong> Senior</p>
        _____________________________

        I hope you really got what I was asking.

        TIA


        On Thu, Apr 1, 2010 at 8:25 PM, Tim Chase <vim@...> wrote:
        Kunal Bajpai wrote:
        I have been using Vim for about 3-4 months and am a student web
        developer. I am still learning Vim and need help with a command.
        I am editing some web pages using web pages many a times I have to
        edit web pages. Here is what I want:

        ______________________________
        <p>Mike
        Gilbert
        Senior</p>
        _____________________________

        I need a command that selects the whole html code including the
        newline character.

        While you can include "\n" in your search pattern for the newline,

         /<p>Mike\nGilbert\nSenior<\/p>

        you don't specify *what* you want to do with it once  you've selected it.  Do you want to replace all or a portion with something?  Do you want to indent it?  Capitalize it?

        With more information, it may be possible to get a solution to modify all your files in one pass.  You may also read up in the help regarding windo/argdo/bufdo/tabdo  for performing an action on a collection of files (since you mention multiple pages).

        -tim




        --
        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

        To unsubscribe, reply using "remove me" as the subject.

        --
        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
      • Karthick Gururaj
        ... See help :/ -- 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
        Message 3 of 10 , Apr 2 12:43 AM
        • 0 Attachment
          On Fri, Apr 2, 2010 at 10:10 AM, Kunal Bajpai <kunal.bajpai@...> wrote:
          To this html code I wanted to select the text between the tags and replace the original text with \n character removed. There is a more complex example that I wanted to ask, here is the problem:

          Original
          ______________________________
          <p>Mike
          <strong>Gilbert </strong>
          Senior</p>
          _____________________________

          After search & replace:
          ______________________________
          <p>Mike <strong>Gilbert</strong> Senior</p>
          _____________________________


          You can do this with:
          : /<p>/, /<\/p>/ join
          See help :/

          --
          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
        • Christian Brabandt
          Hi Kunal! ... (one line) ... anything that matches ( ) the tag and remember it ( w + ) a word (which means a string that
          Message 4 of 10 , Apr 2 5:23 AM
          • 0 Attachment
            Hi Kunal!

            On Fr, 02 Apr 2010, Kunal Bajpai wrote:

            > To this html code I wanted to select the text between the tags and replace
            > the original text with \n character removed. There is a more complex example
            > that I wanted to ask, here is the problem:
            >
            > Original
            > ______________________________
            > <p>Mike
            > <strong>Gilbert </strong>
            > Senior</p>
            > _____________________________
            >
            > After search & replace:
            > ______________________________
            > <p>Mike <strong>Gilbert</strong> Senior</p>
            > _____________________________
            >
            > I hope you really got what I was asking.

            Try this:
            :%s/\(<p>\)\(\w\+\)\_s*\(\w\+\)\_s*\(\w\+\)\(<\/p>\)/\1 \2 <strong>\3<\/strong> \4 \5/
            (one line)

            which boils down to:
            :%s/ " substitute on every line
            " anything that matches
            \(<p>\) " the <p> tag and remember it
            \(\w\+\) " a word (which means a string that
            " matches the range 0-9A-Za-z_ but no space
            " and remember it
            \_s* " a space or linebreak
            \(\w\+\) " a word and remember it (see above)
            \_s* " a space or line break
            \(\w\+\) " a word and remember it (see above)
            \(<\/p>\) " the closing tag (and remeber it)
            / " and replace the match part
            \1 " the first remembered string followed by a space
            \2 " the second remembered string followed by a space
            <strong> " the strong tag
            \3 " the third remembered string followed by
            <\/strong> " the closing tag followed by a space followed by
            \4 " the 4th remembered word followed by a space and
            \5 " the 5th remembered word
            /

            regards,
            Christian

            --
            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

            To unsubscribe, reply using "remove me" as the subject.
          • Benjamin R. Haskell
            ... Good explanation, Christian. Here s one that s more general (not tailored to the specific contents of ... I put the p in a capture group there so that it
            Message 5 of 10 , Apr 2 6:16 AM
            • 0 Attachment
              On Fri, 2 Apr 2010, Christian Brabandt wrote:

              > Hi Kunal!
              >
              > On Fr, 02 Apr 2010, Kunal Bajpai wrote:
              >
              > > To this html code I wanted to select the text between the tags and
              > > replace the original text with \n character removed. There is a more
              > > complex example that I wanted to ask, here is the problem:
              > >
              > > Original
              > > ______________________________
              > > <p>Mike
              > > <strong>Gilbert </strong>
              > > Senior</p>
              > > _____________________________
              > >
              > > After search & replace:
              > > ______________________________
              > > <p>Mike <strong>Gilbert</strong> Senior</p>
              > > _____________________________
              > >
              > > I hope you really got what I was asking.
              >
              > Try this:
              > :%s/\(<p>\)\(\w\+\)\_s*\(\w\+\)\_s*\(\w\+\)\(<\/p>\)/\1 \2 <strong>\3<\/strong> \4 \5/
              > (one line)
              >
              > which boils down to:
              > :%s/ " substitute on every line
              > " anything that matches
              > \(<p>\) " the <p> tag and remember it
              > \(\w\+\) " a word (which means a string that
              > " matches the range 0-9A-Za-z_ but no space
              > " and remember it
              > \_s* " a space or linebreak
              > \(\w\+\) " a word and remember it (see above)
              > \_s* " a space or line break
              > \(\w\+\) " a word and remember it (see above)
              > \(<\/p>\) " the closing tag (and remeber it)
              > / " and replace the match part
              > \1 " the first remembered string followed by a space
              > \2 " the second remembered string followed by a space
              > <strong> " the strong tag
              > \3 " the third remembered string followed by
              > <\/strong> " the closing tag followed by a space followed by
              > \4 " the 4th remembered word followed by a space and
              > \5 " the 5th remembered word
              > /
              >

              Good explanation, Christian.

              Here's one that's more general (not tailored to the specific contents of
              that '<p>' tag):

              :%s/<\(p\)\_[^>]*>\_.\{-}<\/\1>/\=substitute(submatch(0),"\n"," ","g")/

              I put the 'p' in a capture group there so that it could be generalized:

              e.g. change it to \w\+ to match all tags. Change it to p|div to match
              <p> and <div> tags.

              :%s/ " substitute on all lines things matching:
              <\(p\) " start tag opener plus tag name
              \_[^>]*> " optional things in the start tag, plus start tag closer
              " e.g., would match the ' class="big">' of <p class="big">
              \_.\{-} " minimal amount of anything-including-newlines until:
              <\/\1> " the close tag that matches the open tag name
              /\= " replace with the following expression
              substitute( " see :help substitute
              submatch(0), " the whole match
              "\n"," ","g" " with "\n" replaced by " " 'g'lobally
              )/

              --
              Best,
              Ben

              --
              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

              To unsubscribe, reply using "remove me" as the subject.
            • Christian Brabandt
              Hi Benjamin! ... Good hint. I thought about that, but then I couldn t insert tags around it anymore. But then again, this wasn t event asked for and I
              Message 6 of 10 , Apr 2 6:34 AM
              • 0 Attachment
                Hi Benjamin!

                On Fr, 02 Apr 2010, Benjamin R. Haskell wrote:

                > On Fri, 2 Apr 2010, Christian Brabandt wrote:
                >
                > > Hi Kunal!
                > >
                > > On Fr, 02 Apr 2010, Kunal Bajpai wrote:
                > >
                > > > To this html code I wanted to select the text between the tags and
                > > > replace the original text with \n character removed. There is a more
                > > > complex example that I wanted to ask, here is the problem:
                > > >
                > > > Original
                > > > ______________________________
                > > > <p>Mike
                > > > <strong>Gilbert </strong>
                > > > Senior</p>
                > > > _____________________________
                > > >
                > > > After search & replace:
                > > > ______________________________
                > > > <p>Mike <strong>Gilbert</strong> Senior</p>
                > > > _____________________________
                > > >
                > > > I hope you really got what I was asking.
                > >
                > Good explanation, Christian.
                >
                > Here's one that's more general (not tailored to the specific contents of
                > that '<p>' tag):
                >
                > :%s/<\(p\)\_[^>]*>\_.\{-}<\/\1>/\=substitute(submatch(0),"\n"," ","g")/

                Good hint. I thought about that, but then I couldn't insert <strong>
                tags around it anymore. But then again, this wasn't event asked for and
                I thought the OP wanted the <strong> tags around the name, since these
                tags were missing in his first mail. Oh Well.


                regards,
                Christian

                --
                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

                To unsubscribe, reply using "remove me" as the subject.
              • Ben Fritz
                ... You can also try gqat from normal mode. Potentially you could repeat this using a macro with a count (999999@q) or a recursive macro:
                Message 7 of 10 , Apr 2 7:33 AM
                • 0 Attachment
                  On Apr 1, 11:40 pm, Kunal Bajpai <kunal.baj...@...> wrote:
                  > To this html code I wanted to select the text between the tags and replace
                  > the original text with \n character removed. There is a more complex example
                  > that I wanted to ask, here is the problem:
                  >
                  > Original
                  > ______________________________
                  > <p>Mike
                  > <strong>Gilbert </strong>
                  >  Senior</p>
                  > _____________________________
                  >
                  > After search & replace:
                  > ______________________________
                  > <p>Mike <strong>Gilbert</strong> Senior</p>
                  > _____________________________
                  >
                  > I hope you really got what I was asking.
                  >

                  You can also try gqat from normal mode. Potentially you could repeat
                  this using a macro with a count (999999@q) or a recursive macro:

                  http://vim.wikia.com/wiki/Record_a_recursive_macro

                  --
                  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

                  To unsubscribe, reply using "remove me" as the subject.
                • Kunal Bajpai
                  Thank you all for the help :) -- You received this message from the vim_use maillist. Do not top-post! Type your reply below the text you are replying to.
                  Message 8 of 10 , Apr 5 6:11 AM
                  • 0 Attachment
                    Thank you all for the help :)

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