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

Re: select text between brackets or quotes

Expand Messages
  • Tony Mechelynck
    ... What you need is object selection: see ... The latter two only work within one line; the rest can span line breaks. For instance, vi visually selects the
    Message 1 of 12 , Sep 1, 2007
    • 0 Attachment
      gabor wrote:
      > hi,
      >
      > what is the simplest way to select the text between two brackets
      > (parentheses, quotes) so that the selection does not include the
      > brackets?
      >
      > basically i want to replace the content of the brackets while the
      > cursor is located anywher inside. i can use the command %v% to make a
      > selection including the brackets, however i'd like to exclude them.
      >
      > another question is how to do the same for quoted strings?
      >
      > cheers,
      > gabor

      What you need is object selection:

      see
      :help i(
      :help i[
      :help i{
      :help i<
      :help i"
      :help i'

      The latter two only work within one line; the rest can span line breaks.

      For instance, vi" visually selects the inner double-quoted string, excluding
      the quotes.


      Best regards,
      Tony.
      --
      Good day to avoid cops. Crawl to school.

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • gabor
      cool! this is exactly what i was looking for. thanks to all of you for the elaborate answers. good to see that vim has such a helpful community. gabor ...
      Message 2 of 12 , Sep 1, 2007
      • 0 Attachment
        cool! this is exactly what i was looking for.

        thanks to all of you for the elaborate answers.
        good to see that vim has such a helpful community.

        gabor


        On Sep 1, 3:12 pm, Tim Chase <v...@...> wrote:
        > > what is the simplest way to select the text between two brackets
        > > (parentheses, quotes) so that the selection does not include the
        > > brackets?
        >
        > Sounds like you want to use text objects:
        >
        > :help text-objects
        >
        > which allow you to do things like
        >
        > vi[
        >
        > to "go Visual with the Inner contents of the '['". This works
        > anywhere within the "[...]" region (including on the brackets
        > themselves).
        >
        > Since vim7, there are now "quote" text objects too (yay! a
        > feature I requested and got :)
        >
        > vi"
        >
        > and
        >
        > vi'
        >
        > (though the quote versions aren't multi-line like the [/(/{
        > versions are, which occasionally bites me when writing python
        > code and using multi-line """...""" strings, but I've learned
        > just not to expect that to work yet)
        >
        > Since text-objects are pseudo-motions, they can be applied
        > directly to many objects, so if you want to yank the contents of
        > the "[...]", you can "yi["; if you want to fold the current
        > multi-line "{...}" block, you can "zfi{", and if you want to
        > change the contents of the current string, you can use
        >
        > ci"
        >
        > There are also "outer" variants using "a" instead of "i" which
        > include the outer characters rather than excluding them. This
        > may also end up including leading whitespace.
        >
        > Hope this helps,
        >
        > -tim


        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Dave Land
        ... This discussion also reminded me of a very handy mapping that some here may enjoy... I do lots of HTML (actually, JSP) editing in my work, and so
        Message 3 of 12 , Sep 1, 2007
        • 0 Attachment
          On Sep 1, 2007, at 7:48 AM, gabor wrote:

          > cool! this is exactly what i was looking for.
          >
          > thanks to all of you for the elaborate answers.
          > good to see that vim has such a helpful community.

          This discussion also reminded me of a very handy mapping that some
          here may enjoy...

          I do lots of HTML (actually, JSP) editing in my work, and so frequently
          have need to select the "innerHTML" of an pair of tags. I have installed
          matchit.vim(1) and set the following in my .vimrc:

          let b:match_words='<\([\:0-9A-Za-z_-]\+\)[^>]*>:<\/\1[^>]*>,<%:%>'

          That match_words string works nearly all of the time for HTML, XML, and
          so forth, but occasionally fails if the capitalization of the tags does
          not match -- it'll match forward, but not backwards.

          Then, I set the following mapping (I use a Mac, so I chose option-A
          because it is similar to command-A, which is "select all"):

          map å Vk%k

          I put the cursor on the first line of the "innerHTML" I want to select,
          then use the mapping, which goes up to the opening tag, uses % to find
          the appropriate closing tag, then goes up again to deselect the closing
          tag. Voilá -- I've selected the contents of a pair of tags.

          Dave

          (1) http://www.vim.org/scripts/script.php?script_id=39

          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • Tony Mechelynck
          ... The above mapping (using V = linewise-visual) seems to imply that each HTML tag is always alone on its line. What about some text with bold,
          Message 4 of 12 , Sep 1, 2007
          • 0 Attachment
            Dave Land wrote:
            > On Sep 1, 2007, at 7:48 AM, gabor wrote:
            >
            >> cool! this is exactly what i was looking for.
            >>
            >> thanks to all of you for the elaborate answers.
            >> good to see that vim has such a helpful community.
            >
            > This discussion also reminded me of a very handy mapping that some
            > here may enjoy...
            >
            > I do lots of HTML (actually, JSP) editing in my work, and so frequently
            > have need to select the "innerHTML" of an pair of tags. I have installed
            > matchit.vim(1) and set the following in my .vimrc:
            >
            > let b:match_words='<\([\:0-9A-Za-z_-]\+\)[^>]*>:<\/\1[^>]*>,<%:%>'
            >
            > That match_words string works nearly all of the time for HTML, XML, and
            > so forth, but occasionally fails if the capitalization of the tags does
            > not match -- it'll match forward, but not backwards.
            >
            > Then, I set the following mapping (I use a Mac, so I chose option-A
            > because it is similar to command-A, which is "select all"):
            >
            > map å Vk%k
            >
            > I put the cursor on the first line of the "innerHTML" I want to select,
            > then use the mapping, which goes up to the opening tag, uses % to find
            > the appropriate closing tag, then goes up again to deselect the closing
            > tag. Voilá -- I've selected the contents of a pair of tags.
            >
            > Dave
            >
            > (1) http://www.vim.org/scripts/script.php?script_id=39


            The above mapping (using V = linewise-visual) seems to imply that each HTML
            tag is always alone on its line. What about

            some text with <b>bold,</b> <i>italic or <b>both</b></i> on <u>the same line</u>?

            I guess you would have to adjust characterwise... let's see... what about

            :map <F5> vF>h%F<lt>?.<CR>of>/.<CR>
            ?


            Best regards,
            Tony.
            --
            "I have to convince you, or at least snow you ..."
            -- Prof. Romas Aleliunas, CS 435

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Jürgen Krämer
            Hi, ... I m not sure if I have understood the interaction between b:match_words, matchit.txt, and your mapping correctly but wouldn t vit be a much simplet
            Message 5 of 12 , Sep 2, 2007
            • 0 Attachment
              Hi,

              Dave Land wrote:
              >
              > This discussion also reminded me of a very handy mapping that some
              > here may enjoy...
              >
              > I do lots of HTML (actually, JSP) editing in my work, and so frequently
              > have need to select the "innerHTML" of an pair of tags. I have installed
              > matchit.vim(1) and set the following in my .vimrc:
              >
              > let b:match_words='<\([\:0-9A-Za-z_-]\+\)[^>]*>:<\/\1[^>]*>,<%:%>'
              >
              > That match_words string works nearly all of the time for HTML, XML, and
              > so forth, but occasionally fails if the capitalization of the tags does
              > not match -- it'll match forward, but not backwards.
              >
              > Then, I set the following mapping (I use a Mac, so I chose option-A
              > because it is similar to command-A, which is "select all"):
              >
              > map å Vk%k
              >
              > I put the cursor on the first line of the "innerHTML" I want to select,
              > then use the mapping, which goes up to the opening tag, uses % to find
              > the appropriate closing tag, then goes up again to deselect the closing
              > tag. Voilá -- I've selected the contents of a pair of tags.

              I'm not sure if I have understood the interaction between b:match_words,
              matchit.txt, and your mapping correctly but wouldn't

              vit

              be a much simplet approach to visually select the text between a tag and
              its matching end tag? There's also

              vat

              to select a text and its surrounding tags.

              Regards,
              Jürgen

              --
              Sometimes I think the surest sign that intelligent life exists elsewhere
              in the universe is that none of it has tried to contact us. (Calvin)

              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Tony Mechelynck
              ... Well, it would! Thanks for pointing out a text object I didn t know about. Best regards, Tony. -- We gave you an atomic bomb, what do you want, mermaids?
              Message 6 of 12 , Sep 2, 2007
              • 0 Attachment
                Jürgen Krämer wrote:
                >
                > Hi,
                >
                > Dave Land wrote:
                >> This discussion also reminded me of a very handy mapping that some
                >> here may enjoy...
                >>
                >> I do lots of HTML (actually, JSP) editing in my work, and so frequently
                >> have need to select the "innerHTML" of an pair of tags. I have installed
                >> matchit.vim(1) and set the following in my .vimrc:
                >>
                >> let b:match_words='<\([\:0-9A-Za-z_-]\+\)[^>]*>:<\/\1[^>]*>,<%:%>'
                >>
                >> That match_words string works nearly all of the time for HTML, XML, and
                >> so forth, but occasionally fails if the capitalization of the tags does
                >> not match -- it'll match forward, but not backwards.
                >>
                >> Then, I set the following mapping (I use a Mac, so I chose option-A
                >> because it is similar to command-A, which is "select all"):
                >>
                >> map å Vk%k
                >>
                >> I put the cursor on the first line of the "innerHTML" I want to select,
                >> then use the mapping, which goes up to the opening tag, uses % to find
                >> the appropriate closing tag, then goes up again to deselect the closing
                >> tag. Voilá -- I've selected the contents of a pair of tags.
                >
                > I'm not sure if I have understood the interaction between b:match_words,
                > matchit.txt, and your mapping correctly but wouldn't
                >
                > vit
                >
                > be a much simplet approach to visually select the text between a tag and
                > its matching end tag? There's also
                >
                > vat
                >
                > to select a text and its surrounding tags.
                >
                > Regards,
                > Jürgen
                >

                Well, it would! Thanks for pointing out a text object I didn't know about.

                Best regards,
                Tony.
                --
                We gave you an atomic bomb, what do you want, mermaids?
                -- I. I. Rabi to the Atomic Energy Commission

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              • Dave Land
                ... It certainly does! I had no idea. I m glad to know about that text object! Unfortunately, Vit and Vat don t work as I would have expected: upon typing the
                Message 7 of 12 , Sep 2, 2007
                • 0 Attachment
                  On Sep 2, 2007, at 3:13 AM, Jürgen Krämer wrote:

                  > I'm not sure if I have understood the interaction between
                  > b:match_words,
                  > matchit.txt, and your mapping correctly but wouldn't
                  >
                  > vit
                  >
                  > be a much simplet approach to visually select the text between a
                  > tag and
                  > its matching end tag? There's also
                  >
                  > vat
                  >
                  > to select a text and its surrounding tags.

                  It certainly does! I had no idea.

                  I'm glad to know about that text object!

                  Unfortunately, Vit and Vat don't work as I would have expected: upon
                  typing the final 't', the selection changes from line-oriented ('V')
                  to character-oriented (as though I'd typed 'v' to start).

                  But I will be using vit and vat to select text between tags when I
                  don't need full-line selections.

                  Dave


                  --~--~---------~--~----~------------~-------~--~----~
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                  -~----------~----~----~----~------~----~------~--~---
                • Tony Mechelynck
                  ... After an operator (but not v V Ctrl-V) and before the motion or object, you can use v V or Ctrl-V to force te operator to act characterwise, blockwise or
                  Message 8 of 12 , Sep 2, 2007
                  • 0 Attachment
                    Dave Land wrote:
                    > On Sep 2, 2007, at 3:13 AM, Jürgen Krämer wrote:
                    >
                    >> I'm not sure if I have understood the interaction between
                    >> b:match_words,
                    >> matchit.txt, and your mapping correctly but wouldn't
                    >>
                    >> vit
                    >>
                    >> be a much simplet approach to visually select the text between a
                    >> tag and
                    >> its matching end tag? There's also
                    >>
                    >> vat
                    >>
                    >> to select a text and its surrounding tags.
                    >
                    > It certainly does! I had no idea.
                    >
                    > I'm glad to know about that text object!
                    >
                    > Unfortunately, Vit and Vat don't work as I would have expected: upon
                    > typing the final 't', the selection changes from line-oriented ('V')
                    > to character-oriented (as though I'd typed 'v' to start).
                    >
                    > But I will be using vit and vat to select text between tags when I
                    > don't need full-line selections.
                    >
                    > Dave

                    After an operator (but not v V Ctrl-V) and before the motion or object, you
                    can use v V or Ctrl-V to force te operator to act characterwise, blockwise or
                    linewise, respectively: e.g.:

                    dVit

                    delete inside the tag, and extend the delete to act on whole lines.


                    Best regards,
                    Tony.
                    --
                    The reader this message encounters not failing to understand is
                    cursed.

                    --~--~---------~--~----~------------~-------~--~----~
                    You received this message from the "vim_use" maillist.
                    For more information, visit http://www.vim.org/maillist.php
                    -~----------~----~----~----~------~----~------~--~---
                  • Jürgen Krämer
                    Hi, ... while in any visual selection mode you can use , , or to change to a different one. So just use VitV or vitV to select complete lines.
                    Message 9 of 12 , Sep 2, 2007
                    • 0 Attachment
                      Hi,

                      Dave Land wrote:
                      >
                      > I'm glad to know about that text object!
                      >
                      > Unfortunately, Vit and Vat don't work as I would have expected: upon
                      > typing the final 't', the selection changes from line-oriented ('V')
                      > to character-oriented (as though I'd typed 'v' to start).

                      while in any visual selection mode you can use <V>, <v>, or <C-V> to
                      change to a different one. So just use

                      VitV

                      or

                      vitV

                      to select complete lines.

                      Regards,
                      Jürgen

                      --
                      Sometimes I think the surest sign that intelligent life exists elsewhere
                      in the universe is that none of it has tried to contact us. (Calvin)

                      --~--~---------~--~----~------------~-------~--~----~
                      You received this message from the "vim_use" maillist.
                      For more information, visit http://www.vim.org/maillist.php
                      -~----------~----~----~----~------~----~------~--~---
                    Your message has been successfully submitted and would be delivered to recipients shortly.