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

Re: [vim] apply to a section

Expand Messages
  • Charles E. Campbell
    ... You can select blocks of text V line-wise v character-wise (like sentences) ctrl-v visual-block (ie. select a rectangular region of text)
    Message 1 of 13 , Feb 3, 2003
      On Mon, Feb 03, 2003 at 07:03:14PM +0530, Payal Rathod wrote:
      > Emacs lets me define a region (a "block" in other editors) and then
      > apply an external command to each line in the region...
      >
      > He does not know how to do this. Any ideas on this? I may want to apply
      > sed or awk script to a particular region.
      ---------------------------------------------------------------------

      You can select blocks of text

      V line-wise
      v character-wise (like sentences)
      ctrl-v visual-block (ie. select a rectangular region of text)

      There are a number of operators you can use: see

      :he visual-operators

      For visual-blocks (the ctrl-v one), there's a tip for
      applying any operator (in particular, substitutes) to
      just the visual block: see

      http://vim.sourceforge.net/tips/tip.php?tip_id=6

      In your case I think you want to apply an external
      command to a block of lines. Use V and !externalcmd
      (where externalcmd is the external command you wish
      to run). However, if in fact you want a visual-block
      (ie. using ctrl-v), the script/map associated with
      the tip works with external commands, too.

      Regards,
      C Campbell

      --
      Charles E Campbell, Jr, PhD _ __ __
      Goddard Space Flight Center / /_/\_\_/ /
      cec@... /_/ \/_//_/
      PGP public key: http://www.erols.com/astronaut/pgp.html
    • Pritesh Mistry
      Okay, I think, with equalprg , you could set the default program/cmd to run on not only the visually selected block of text (with == ), but also in an
      Message 2 of 13 , Feb 3, 2003
        Okay, I think, with 'equalprg', you could set the default program/cmd to run
        on not only the visually selected block of text (with '=='), but also in an
        operator pending mode.

        Ie: if you are using an external code beautifier program & set
        'equalprg'=mycodebeautifier, then, if your cursor is on the beginning '{' of
        main(), you could simply do '=%' to run the codebeautifier through the
        entire main(), until the matching/closing brace. Your code will be
        beautified & replaced by this external beautifier.

        List, if u have time, any better suggestions for this -- I very much depend
        on 'equalprg'.

        HTH!
        -Pritesh

        -----Original Message-----
        From: Payal Rathod [mailto:payal@...]
        Sent: Monday, February 03, 2003 10:58 pm
        To: Pritesh Mistry
        Subject: Re: apply to a section


        * Pritesh Mistry <pmistry@...> [2003-02-03 22:37]:
        > Hi,
        >
        > :he =
        > :he 'equalprg'
        >
        > HTH!

        Sorry, nope. I didn't get anything from this. Frankly, not even a
        sentence. Can you show with a small example?

        Regards,
        -Payal

        > <quote>
        > Emacs lets me define a region (a "block" in other editors) and then
        > apply an external command to each line in the region. The output is
        > sent to another window (so I can compare the two) or else can entirely
        > replace the current block. The same thing can be done in vim ...
        > </quote>
        >

        --
        "Visit Linux Success Stories"
        www.geocities.com/rpayal99
        Guest-Book Section Updated.
      • Payal Rathod
        Hi, Thanks for all who replied. I got it almost working except have some doubts. I have digest form of list, so mailing like this. David says, Note, there are
        Message 3 of 13 , Feb 5, 2003
          Hi,
          Thanks for all who replied. I got it almost working except have some
          doubts. I have digest form of list, so mailing like this.

          David says,
          Note, there are 3 ways to get regions in Vim.

          V (lower case), select only what you want
          V (upper case), includes full lines
          Ctrl-V - select a block of code (like drawing a rectangle).

          1,2 works but not 3.

          I have a file.
          $ vi sample.dat
          rathod rathod.
          payal Rathod

          Now I select using ctrl-V only 6 characters from each row using ctrl-V
          and then
          :'<,'>!sed 's/a/A/g'

          it does not work. Changes are applied to the whole line as such.

          Gerhard says,
          Or you can define a region using marks.

          How exactly?

          Charles Campbell says,
          http://vim.sourceforge.net/tips/tip.php?tip_id=6

          I am not sure on how this tip is related to my problem directly.

          I will state my problem.
          I want to apply some sed or awk filtering on a part of file only.

          So, I thought it can be done in vi properly.

          Thanks a lot for the time and help.

          Regards.
          -Payal




          --
          "Visit Linux Success Stories"
          www.geocities.com/rpayal99
          Guest-Book Section Updated.
        • Gerhard Siegesmund
          Luckily I saw this today on vimtips: VimTip 62: Applying substitutes to a visual block Take a look at www.vim.org and search for vimtip Nr. 62. Basically pure
          Message 4 of 13 , Feb 5, 2003
            Luckily I saw this today on vimtips:

            VimTip 62: Applying substitutes to a visual block

            Take a look at www.vim.org and search for vimtip Nr. 62. Basically pure
            vim can't substitute in a CTRL-V-Block. But with a plugin it works. HTH.

            > Gerhard says,
            > Or you can define a region using marks.
            > How exactly?

            You can mark to points in your text with m (help mark) and then give
            a set of lines using ' (help ').

            E.g. go to line 7 mark with ma, then go to line 10 and do mb. Now you
            have to marks and can do a substitute for example like:

            :'a,'b s/now/then/g

            --
            cu
            --== Jerri ==--
            Homepage: http://www.jerri.de/ ICQ: 54160208
          • Gary Johnson
            ... That s correct. You would think that the command would apply to just the selected region but it doesn t. However, there is a script available to fix
            Message 5 of 13 , Feb 5, 2003
              On 2003-02-05, Payal Rathod <payal@...> wrote:
              > Hi,
              > Thanks for all who replied. I got it almost working except have some
              > doubts. I have digest form of list, so mailing like this.
              >
              > David says,
              > Note, there are 3 ways to get regions in Vim.
              >
              > V (lower case), select only what you want
              > V (upper case), includes full lines
              > Ctrl-V - select a block of code (like drawing a rectangle).
              >
              > 1,2 works but not 3.
              >
              > I have a file.
              > $ vi sample.dat
              > rathod rathod.
              > payal Rathod
              >
              > Now I select using ctrl-V only 6 characters from each row using ctrl-V
              > and then
              > :'<,'>!sed 's/a/A/g'
              >
              > it does not work. Changes are applied to the whole line as such.

              That's correct. You would think that the command would apply to
              just the selected region but it doesn't. However, there is a script
              available to fix that: vis.vim. I think it's available and
              vim.sf.net and maybe at Charles Campbell's page.

              > Gerhard says,
              > Or you can define a region using marks.
              >
              > How exactly?

              1. Set a mark at the top of the region by putting the cursor on
              that line and typing

              ma

              2. Set mark at the bottom of the region by putting the cursor on
              that line and typing

              mb

              3. Execute an external command on that region by typing

              :'a,'b!sed 's/a/A/g'

              The letters 'a' and 'b' and the sed command are all just examples.
              See also

              :help mark-motions

              > Charles Campbell says,
              > http://vim.sourceforge.net/tips/tip.php?tip_id=6
              >
              > I am not sure on how this tip is related to my problem directly.

              What he's saying is that if the range of lines you wish to filter is
              surrounded by some pair of characters recognized by the % command
              (see ":help %", ":help matchpairs", and the matchit.vim plugin at
              vim.sf.net), you can filter them by positioning the cursor on one of
              the characters and typing

              !%sed 's/a/A/g'

              > I will state my problem.
              > I want to apply some sed or awk filtering on a part of file only.
              >
              > So, I thought it can be done in vi properly.

              It can. I do it all the time. It's just a matter of learning how.

              Regards,
              Gary

              --
              Gary Johnson | Agilent Technologies
              garyjohn@... | Spokane, Washington, USA
            • Charles E. Campbell
              ... That should be http://vim.sourceforge.net/tips/tip.php?tip_id=63, Applying substitutes to a visual block . The script actually applies to any ex command.
              Message 6 of 13 , Feb 5, 2003
                On Wed, Feb 05, 2003 at 03:01:09PM +0530, Payal Rathod wrote:
                > Now I select using ctrl-V only 6 characters from each row using ctrl-V
                > and then
                > :'<,'>!sed 's/a/A/g'
                >
                > it does not work. Changes are applied to the whole line as such.
                > ...snip...
                >
                > Charles Campbell says,
                > http://vim.sourceforge.net/tips/tip.php?tip_id=6
                >
                > I am not sure on how this tip is related to my problem directly.
                ---------------------------------------------------------------------

                That should be http://vim.sourceforge.net/tips/tip.php?tip_id=63,
                "Applying substitutes to a visual block". The script actually
                applies to any ex command.

                Example:
                Original:
                one two three
                four five six
                seven eight nine
                ten eleven twelve

                Select center column using ctrl-v,
                then apply sort: B !sort
                one eight three
                four eleven six
                seven five nine
                ten two twelve

                Note that the outer two columns are unaffected but the
                center column has been sorted.

                Regards,
                Chip Campbell

                --
                Charles E Campbell, Jr, PhD _ __ __
                Goddard Space Flight Center / /_/\_\_/ /
                cec@... /_/ \/_//_/
                PGP public key: http://www.erols.com/astronaut/pgp.html
              • Payal Rathod
                Hi, Thanks for the mails. I tried vis.vim script, selected a block of text and tried to do sort on it and got an error, Error detected while processing
                Message 7 of 13 , Feb 5, 2003
                  Hi,
                  Thanks for the mails. I tried vis.vim script, selected a block of text
                  and tried to do sort on it and got an error,

                  Error detected while processing function <SNR>21_VisBlockCmd:
                  line 54:
                  Not an editor command: .,$sort

                  But the part was sorted.


                  I tried to select one block with ctrl-v and then applied,
                  :B sed 's/rathod/RATHOD/g' and got,
                  E116: Invalid arguments for function s:VisBlockCmd

                  same thing happened with using ! before sed.
                  How does this work?

                  Also, I don't know which key I pressed but I got,
                  :'<,'> automatically. Is there a shortcut key for this?

                  Thanks a lot and bye.
                  With regards,
                  -Payal

                  --
                  "Visit Linux Success Stories"
                  www.geocities.com/rpayal99
                  Guest-Book Section Updated.
                • Gerhard Siegesmund
                  ... This is how visual selection works. Basically it creates two marks at the beginning and end of your selection. This are named with . If you then
                  Message 8 of 13 , Feb 5, 2003
                    > Also, I don't know which key I pressed but I got,
                    > :'<,'> automatically. Is there a shortcut key for this?

                    This is how visual selection works. Basically it creates two marks at
                    the beginning and end of your selection. This are named with < and >. If
                    you then press : it gives you this two marks for anything you want to
                    do.

                    --
                    cu
                    --== Jerri ==--
                    Homepage: http://www.jerri.de/ ICQ: 54160208
                  • Charles E. Campbell
                    ... make ctrl-v visual selection, then you press ... The sort is an external program; to do filtering of text through an external program one needs to use the
                    Message 9 of 13 , Feb 6, 2003
                      On Thu, Feb 06, 2003 at 12:40:49PM +0530, Payal Rathod wrote:
                      > I tried vis.vim script, selected a block of text
                      > and tried to do sort on it and got an error,
                      >
                      > Error detected while processing function <SNR>21_VisBlockCmd:
                      > line 54:
                      > Not an editor command: .,$sort
                      >
                      > But the part was sorted.
                      ---------------------------------------------------------------------

                      make ctrl-v visual selection, then you press

                      :B !sort

                      The sort is an external program; to do filtering of text
                      through an external program one needs to use the ! ex
                      command.

                      You'll see

                      :'<,'>B !sort

                      Regards,
                      Chip Campbell

                      --
                      Charles E Campbell, Jr, PhD _ __ __
                      Goddard Space Flight Center / /_/\_\_/ /
                      cec@... /_/ \/_//_/
                      PGP public key: http://www.erols.com/astronaut/pgp.html
                    Your message has been successfully submitted and would be delivered to recipients shortly.