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

Re: append to non-blank lines in a selection

Expand Messages
  • Gary Johnson
    ... I don t think there is a way to qualify an operation like A on a visually-selected region like that. However, you can so qualify a ... When you type the
    Message 1 of 7 , Jun 20, 2013
      On 2013-06-20, Chris Lott wrote:
      > Given a text:
      >
      > line1 has stuff
      > so does line 2
      >
      > line 4 has more stuff
      > and line 5
      > and so on
      >
      > I would like to select the text block and append a word to each
      > non-blank line. I can use a block selection and `A` to add to all
      > selected lines, but not sure how to only apply to lines that have
      > text.

      I don't think there is a way to qualify an operation like A on a
      visually-selected region like that. However, you can so qualify a
      :substitute command. Select the text block and type

      :g/./s/$/word/

      When you type the colon, Vim will automatically insert the names of
      the marks for the beginning and end of the selected block, so you
      will see this on the command line:

      :'<,'>g/./s/$/word/

      Then hit Enter.

      The :g (:global) command followed by the pattern . specifies that
      the :s (:substitute) command is to be executed only on those lines
      containing at least one character. The :substitute command
      substitutes the end of the line ($) with "word".

      See

      :help :g
      :help :s
      :help /$

      Regards,
      Gary

      --
      --
      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.
    • Ben Fritz
      ... -- -- 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,
      Message 2 of 7 , Jun 21, 2013
        On Thursday, June 20, 2013 11:23:39 PM UTC-5, Gary Johnson wrote:
        > On 2013-06-20, Chris Lott wrote:
        >
        > > Given a text:
        >
        > >
        >
        > > line1 has stuff
        >
        > > so does line 2
        >
        > >
        >
        > > line 4 has more stuff
        >
        > > and line 5
        >
        > > and so on
        >
        > >
        >
        > > I would like to select the text block and append a word to each
        >
        > > non-blank line. I can use a block selection and `A` to add to all
        >
        > > selected lines, but not sure how to only apply to lines that have
        >
        > > text.
        >
        >
        >
        > I don't think there is a way to qualify an operation like A on a
        >
        > visually-selected region like that. However, you can so qualify a
        >
        > :substitute command. Select the text block and type
        >
        >
        >
        > :g/./s/$/word/
        >

        Since you don't like regex (although you're right, you should learn), you could alternatively do it like you've always done with A:

        :g/./normal! Aword

        --
        --
        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.
      • Chris Lott
        ... It s not that I don t like regex, it s that I ve found them hard to learn. Suggestions on good sites/books/etc to learn them would be much appreciated. c
        Message 3 of 7 , Jun 21, 2013
          On Fri, Jun 21, 2013 at 6:55 AM, Ben Fritz <fritzophrenic@...> wrote:
          > Since you don't like regex (although you're right, you should learn)

          It's not that I don't like regex, it's that I've found them hard to
          learn. Suggestions on good sites/books/etc to learn them would be much
          appreciated.

          c
          --
          Chris Lott <chris@...>

          --
          --
          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.
        • Ben Fritz
          ... When I learned, I just did a web search for regex tutorial or learn regular expressions or something like that. Although each regular expression
          Message 4 of 7 , Jun 21, 2013
            On Friday, June 21, 2013 10:40:45 AM UTC-5, Chris Lott wrote:
            > On Fri, Jun 21, 2013 at 6:55 AM, Ben Fritz <fritzophrenic@...> wrote:
            >
            > > Since you don't like regex (although you're right, you should learn)
            >
            >
            >
            > It's not that I don't like regex, it's that I've found them hard to
            >
            > learn. Suggestions on good sites/books/etc to learn them would be much
            >
            > appreciated.
            >
            >

            When I learned, I just did a web search for "regex tutorial" or "learn regular expressions" or something like that. Although each regular expression dialect is different, the concepts are usually very portable. After you learn the basics you can start looking into Vim's :help pattern to pick up on some of the more advanced features available.

            --
            --
            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.
          • Paul Isambert
            ... Perhaps you should start with some basic stuff (like “*” or “$” or character classes) and then learn new things when you need them (that’s how I
            Message 5 of 7 , Jun 21, 2013
              Chris Lott <chris@...> a écrit:
              > On Fri, Jun 21, 2013 at 6:55 AM, Ben Fritz <fritzophrenic@...> wrote:
              > > Since you don't like regex (although you're right, you should learn)
              >
              > It's not that I don't like regex, it's that I've found them hard to
              > learn. Suggestions on good sites/books/etc to learn them would be much
              > appreciated.

              Perhaps you should start with some basic stuff (like “*” or “$” or
              character classes) and then learn new things when you need them
              (that’s how I got used to regex myself). For instance, Gary and Ben’s
              solution might give unwanted results if blank lines contain space
              characters. So your next step is “Could it be possible to match a line
              that contain at least one non-space character?” and a quick look up
              “:help pattern.txt” gives you “\S”:

              :g/\S/s/$/word/

              So my advice (I’m definitely no authority on that subject, mind you)
              is to *use* regex and fill in the blanks stepwise; if you really don’t
              know anything about regex (I doubt that is true, though), then perhaps
              “:help user_27” might be a good starting point.

              (And you definitely should use regex with Vim! Otherwise it’s like a
              corded drill with the power off.)

              Best,
              Paul

              --
              --
              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.
            • Jean-Rene David
              ... Mastering Regular Expressions by Jeffrey Friedl is very good. It assumes no prior knowledge and will take you as far as you are willing to go. Very
              Message 6 of 7 , Jul 3, 2013
                * Chris Lott [2013.06.21 11:50]:
                > On Fri, Jun 21, 2013 at 6:55 AM, Ben Fritz <fritzophrenic@...> wrote:
                > > Since you don't like regex (although you're right, you should learn)
                >
                > It's not that I don't like regex, it's that I've found them hard to
                > learn. Suggestions on good sites/books/etc to learn them would be much
                > appreciated.

                "Mastering Regular Expressions" by Jeffrey Friedl
                is very good. It assumes no prior knowledge and
                will take you as far as you are willing to go.
                Very complete and well written.

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