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

Visual block question

Expand Messages
  • Iain Duncan
    Hi everyone, quick question to the dev gurus. The help files mention that the way Visual Block select automatically gets extended to the end of a line may get
    Message 1 of 13 , Dec 11, 2003
    • 0 Attachment
      Hi everyone, quick question to the dev gurus. The help files mention that
      the way Visual Block select automatically gets extended to the end of a line
      may get changed in the future. Does anyone know if this will happen? If not,
      what about a patch to allow folks to choose that feature if they want?

      I'm making a grid editor for editing musical event lists. Everything about
      Vim is great for this except the extremely frustrating fact that I don't
      seem to be able to highlight a rectangular block and apply a search and
      replace or other function to everything in that block and that block only!

      Alternatively what about another flag like the /g that means to the end of
      the selected block instead of to the end of the line? At any rate I'd
      personally find this change very very helpful.

      Thanks for listening,
      Iain
    • Srinath Avadhanula
      ... This functionality can be obtained via a vim script. I beleive Dr. Chip already has made something for this purpose... Let me see... Here you go...
      Message 2 of 13 , Dec 11, 2003
      • 0 Attachment
        On Thu, 11 Dec 2003, Iain Duncan wrote:

        > I'm making a grid editor for editing musical event lists. Everything about
        > Vim is great for this except the extremely frustrating fact that I don't
        > seem to be able to highlight a rectangular block and apply a search and
        > replace or other function to everything in that block and that block only!

        This functionality can be obtained via a vim script. I beleive Dr. Chip
        already has made something for this purpose... Let me see...

        Here you go...

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

        Srinath
      • Jason Holt
        ... This isn t the right list for this, but this works fine for me: control-V to highlight the block, then :s/foo/bar/g -J
        Message 3 of 13 , Dec 11, 2003
        • 0 Attachment
          On Thu, 11 Dec 2003, Srinath Avadhanula wrote:

          > On Thu, 11 Dec 2003, Iain Duncan wrote:
          >
          > > I'm making a grid editor for editing musical event lists. Everything about
          > > Vim is great for this except the extremely frustrating fact that I don't
          > > seem to be able to highlight a rectangular block and apply a search and
          > > replace or other function to everything in that block and that block only!

          This isn't the right list for this, but this works fine for me:

          control-V to highlight the block, then :s/foo/bar/g

          -J
        • iainduncan@telus.net
          ... That doesn t work because it will automatically extend the range of the command to the end of the line, while dropping the /g will only replace the first.
          Message 4 of 13 , Dec 11, 2003
          • 0 Attachment
            > This isn't the right list for this, but this works fine for me:
            >
            > control-V to highlight the block, then :s/foo/bar/g
            >

            That doesn't work because it will automatically extend the range of the
            command to the end of the line, while dropping the /g will only replace the
            first. So if for example I want to replace only the integers in question in
            the first four columns, the normal process is insufficient. This problem seems
            also to affects popping a block into an external script as the command used
            works on the whole line. ( At least my test with the manual's example of :c
            sort did that. )

            The reason I posted to this list is because the manual specifically mentions
            this problem and says this may be changed in future versions of Vim. I can't
            figure out why that change wouldn't be a good idea, as it doesn't make sense
            to me to have Visual Block behave like non-block visual mode for search and
            replace. That sounded like a dev issue to me, sorry if should have been on the
            other list. If I were a good enough programmer, I'd make a patch for my own
            uses!

            Thanks,
            Iain
          • Pavol Juhas
            ... I think it is not that simple to implement visual block search/replace. For set ve=all you can extend the selection beyond existing text - should :s treat
            Message 5 of 13 , Dec 11, 2003
            • 0 Attachment
              On Thu, Dec 11, 2003 at 07:06:46PM -0800, iainduncan@... wrote:
              > >
              > > control-V to highlight the block, then :s/foo/bar/g
              >
              > That doesn't work because it will automatically extend the range of the
              > command to the end of the line, while dropping the /g will only replace the
              > first. So if for example I want to replace only the integers in question in
              > the first four columns, the normal process is insufficient. This
              ...
              > The reason I posted to this list is because the manual specifically mentions
              > this problem and says this may be changed in future versions of Vim. I can't
              > figure out why that change wouldn't be a good idea, as it doesn't make sense
              > to me to have Visual Block behave like non-block visual mode for search and
              > replace. That sounded like a dev issue to me, sorry if should have been on the
              > other list. If I were a good enough programmer, I'd make a patch for my own
              > uses!

              I think it is not that simple to implement visual block
              search/replace. For set ve=all you can extend the selection
              beyond existing text - should :s treat this as spaces? What about
              matches that cross the end of visual block - should they be
              matched? I suspect learning vim regular expressions would be much
              easier than writing a patch. For example to match Pattern after
              the 15th column use
              /\%>15cPattern/
              to match after 15th virtual column use
              /\%>15cPattern/
              or to match pattern that comes rigth after "foo:"
              /\(foo:\)\@<=Pattern

              HTH,

              Pavol
            • Iain Duncan
              ... I ve thought of ways to hack around it myself too, but I still can t see why visual block should set a range that doesn t actually match the visual block!
              Message 6 of 13 , Dec 11, 2003
              • 0 Attachment
                > I think it is not that simple to implement visual block
                > search/replace. For set ve=all you can extend the selection
                > beyond existing text - should :s treat this as spaces? What about
                > matches that cross the end of visual block - should they be
                > matched? I suspect learning vim regular expressions would be much
                > easier than writing a patch. For example to match Pattern after
                > the 15th column use
                > /\%>15cPattern/
                > to match after 15th virtual column use
                > /\%>15cPattern/
                > or to match pattern that comes rigth after "foo:"
                > /\(foo:\)\@<=Pattern

                I've thought of ways to hack around it myself too, but I still can't see why
                visual block should set a range that doesn't actually match the visual
                block! Does anyone think this is something that may or could be fixed? I'm
                debating whether it will be easier to just do the grid editor in Emacs,
                which I would prefer not to do because I like Vim a lot better. But grabbing
                rectangular blocks, processing them, and having them automatically replaced
                is pretty much going to be most of the work done in this thing, and the
                whole purpose is to make that as fast as possible. So for example perhaps
                there are 16 columns by 128 lines, one should be able to quickly select any
                block of that and pass it to a command, or do a replacement, or do some
                math, etc, etc. Having to repeat the process for each column will be a pain.
                Visual block is nicely integrated with mouse support and key selecting.

                As far as I can tell, the only thing working the way I would expect it to is
                yanking, so the other hack I thought of was yanking the area to a temp
                buffer, running the script on that buffer, and then putting the content from
                the temp buffer back into the main one. But I would think that making Visual
                Block act like a functional range indicator would be a handy feature for all
                kinds of other purposes too.

                Anyway, thanks everyone for all the alternate solutions, and please do let
                me know if this is something that may change in future version.

                Iain
              • Iain Duncan
                ... That looks good. Anyone know if that will also work for passing the block to system commands? I will try it out at any rate and if anyone is interested
                Message 7 of 13 , Dec 11, 2003
                • 0 Attachment
                  > This functionality can be obtained via a vim script. I beleive Dr. Chip
                  > already has made something for this purpose... Let me see...
                  >
                  > Here you go...
                  >
                  > http://vim.sourceforge.net/tips/tip.php?tip_id=63
                  >

                  That looks good. Anyone know if that will also work for passing the block to
                  system commands? I will try it out at any rate and if anyone is interested
                  report back.

                  Thanks
                  Iain
                • Iain Duncan
                  ... Beautiful, this does work with commands and makes it behave exactly as one would expect. I guess my only complaint would be that I have no idea how I would
                  Message 8 of 13 , Dec 11, 2003
                  • 0 Attachment
                    > This functionality can be obtained via a vim script. I beleive Dr. Chip
                    > already has made something for this purpose... Let me see...
                    >
                    > Here you go...
                    >
                    > http://vim.sourceforge.net/tips/tip.php?tip_id=63
                    >

                    Beautiful, this does work with commands and makes it behave exactly as one
                    would expect. I guess my only complaint would be that I have no idea how I
                    would have found this without the mailing list! I did try searching through
                    the manuals and vim.org to no avail; there are an awful lot of tips on
                    there! Perhaps if the powers that be in documentation land see this they
                    could make it easier to find this script or consider it for default
                    inclusion in an install? Yay, no need to learn emacs tonight. ; )

                    Thanks everyone
                    Iain
                  • Pavol Juhas
                    On Thu, Dec 11, 2003 at 11:08:54PM -0800, Iain Duncan wrote: ... linewise, and many scripts rely on that. To access block range on ex command line would
                    Message 9 of 13 , Dec 11, 2003
                    • 0 Attachment
                      On Thu, Dec 11, 2003 at 11:08:54PM -0800, Iain Duncan wrote:
                      ...
                      > I've thought of ways to hack around it myself too, but I still can't see why
                      > visual block should set a range that doesn't actually match the visual
                      > block! Does anyone think this is something that may or could be fixed? I'm
                      ...

                      :s is ex command. Since ex is line editor, all its commands work
                      linewise, and many scripts rely on that. To access block range on
                      ex command line would require a good deal of new range-addresing
                      in ex, possibly breaking compatibility. I guess it's not
                      difficult to add block search/replace, but it is difficult to make
                      it work right.

                      Have you already tried Dr. Chip's script as was already suggested
                      to you by Srinath? http://vim.sourceforge.net/tips/tip.php?tip_id=63

                      Pavol
                    • Miroslaw Dobrzanski-Neumann
                      ... Some time ago I have asked for pattern matching only in a block. But I got now answer. ... There seems to be no command for searching only. I ve tried ...
                      Message 10 of 13 , Dec 11, 2003
                      • 0 Attachment
                        On Fri, Dec 12, 2003 at 01:07:02AM -0500, Pavol Juhas wrote:
                        > On Thu, Dec 11, 2003 at 07:06:46PM -0800, iainduncan@... wrote:
                        > > >
                        > > > control-V to highlight the block, then :s/foo/bar/g
                        > >
                        > > That doesn't work because it will automatically extend the range of the
                        > > command to the end of the line, while dropping the /g will only replace the
                        > > first. So if for example I want to replace only the integers in question in
                        > > the first four columns, the normal process is insufficient. This
                        > ...
                        > > The reason I posted to this list is because the manual specifically mentions
                        > > this problem and says this may be changed in future versions of Vim. I can't
                        > > figure out why that change wouldn't be a good idea, as it doesn't make sense
                        > > to me to have Visual Block behave like non-block visual mode for search and
                        > > replace. That sounded like a dev issue to me, sorry if should have been on the
                        > > other list. If I were a good enough programmer, I'd make a patch for my own
                        > > uses!
                        >
                        > I think it is not that simple to implement visual block
                        > search/replace. For set ve=all you can extend the selection
                        > beyond existing text - should :s treat this as spaces? What about
                        > matches that cross the end of visual block - should they be
                        > matched? I suspect learning vim regular expressions would be much
                        > easier than writing a patch. For example to match Pattern after
                        > the 15th column use
                        > /\%>15cPattern/
                        > to match after 15th virtual column use
                        > /\%>15cPattern/
                        > or to match pattern that comes rigth after "foo:"
                        > /\(foo:\)\@<=Pattern

                        Some time ago I have asked for pattern matching only in a block. But I got now
                        answer.

                        Search and replace works fine for me:
                        :'<,'>s/a/b/g
                        There seems to be no command for searching only. I've tried
                        :'<,'>search (a)
                        but but vim tells me: "search is not an editor command" althogh documented in
                        eval.txt

                        Would it be possible to implement such command?

                        Regards
                        --
                        Mirosław Dobrzański-Neumann
                        E-mail: mne@...

                        This message is utf-8 encoded
                      • Pavol Juhas
                        ... Indeed, search is not a command, it is a function. To run function on a range of lines you need to do ... but it would probably not do what you want -
                        Message 11 of 13 , Dec 12, 2003
                        • 0 Attachment
                          On Fri, Dec 12, 2003 at 08:40:19AM +0100, Miroslaw Dobrzanski-Neumann wrote:
                          > Some time ago I have asked for pattern matching only in a block.
                          > But I got now answer.
                          >
                          > Search and replace works fine for me:
                          > :'<,'>s/a/b/g
                          > There seems to be no command for searching only. I've tried
                          > :'<,'>search (a)
                          > but but vim tells me: "search is not an editor command" althogh documented in
                          > eval.txt

                          Indeed, search is not a command, it is a function. To run
                          function on a range of lines you need to do
                          :'<,'>call search('a')
                          but it would probably not do what you want - search() function does
                          not accept range, so it would be executed for every line in '<,'>
                          and it would find the first 'a' after '>.

                          OK, here it is - save and source the following lines and try
                          :1,5call RangeSearch('a') this should match 'a' in lines 1-5.

                          function! RangeSearch( pat ) range
                          " match on the firstline or below
                          let pbeg = '\(\%' . a:firstline . 'l\|\%>' . a:firstline . 'l\)'
                          " match on the lastline or above
                          let pend = '\(\%' . a:lastline . 'l\|\%<' . a:lastline . 'l\)'
                          let @/ = pbeg . '\&' . pend . '\&' . a:pat
                          normal! n
                          endfunction

                          Application with visual block is up to you.

                          Pavol
                        • Miroslaw Dobrzanski-Neumann
                          ... Thaks, it works fine for me -- Mirosław Dobrzański-Neumann E-mail: mne@mosaic-ag.com This message is utf-8 encoded
                          Message 12 of 13 , Dec 12, 2003
                          • 0 Attachment
                            On Fri, Dec 12, 2003 at 04:02:16AM -0500, Pavol Juhas wrote:
                            > On Fri, Dec 12, 2003 at 08:40:19AM +0100, Miroslaw Dobrzanski-Neumann wrote:
                            > > Some time ago I have asked for pattern matching only in a block.
                            > > But I got now answer.
                            > >
                            > > Search and replace works fine for me:
                            > > :'<,'>s/a/b/g
                            > > There seems to be no command for searching only. I've tried
                            > > :'<,'>search (a)
                            > > but but vim tells me: "search is not an editor command" althogh documented in
                            > > eval.txt
                            >
                            > Indeed, search is not a command, it is a function. To run
                            > function on a range of lines you need to do
                            > :'<,'>call search('a')
                            > but it would probably not do what you want - search() function does
                            > not accept range, so it would be executed for every line in '<,'>
                            > and it would find the first 'a' after '>.
                            >
                            > OK, here it is - save and source the following lines and try
                            > :1,5call RangeSearch('a') this should match 'a' in lines 1-5.
                            >
                            > function! RangeSearch( pat ) range
                            > " match on the firstline or below
                            > let pbeg = '\(\%' . a:firstline . 'l\|\%>' . a:firstline . 'l\)'
                            > " match on the lastline or above
                            > let pend = '\(\%' . a:lastline . 'l\|\%<' . a:lastline . 'l\)'
                            > let @/ = pbeg . '\&' . pend . '\&' . a:pat
                            > normal! n
                            > endfunction
                            >
                            > Application with visual block is up to you.

                            Thaks, it works fine for me
                            --
                            Mirosław Dobrzański-Neumann
                            E-mail: mne@...

                            This message is utf-8 encoded
                          • Benji Fisher
                            ... The most likely way for the docs to change is if you figure out where in the docs it would be most helpful, edit your copy of the help file, and submit a
                            Message 13 of 13 , Dec 12, 2003
                            • 0 Attachment
                              On Thu, Dec 11, 2003 at 11:34:03PM -0800, Iain Duncan wrote:
                              > > This functionality can be obtained via a vim script. I beleive Dr. Chip
                              > > already has made something for this purpose... Let me see...
                              > >
                              > > Here you go...
                              > >
                              > > http://vim.sourceforge.net/tips/tip.php?tip_id=63
                              > >
                              >
                              > Beautiful, this does work with commands and makes it behave exactly as one
                              > would expect. I guess my only complaint would be that I have no idea how I
                              > would have found this without the mailing list! I did try searching through
                              > the manuals and vim.org to no avail; there are an awful lot of tips on
                              > there! Perhaps if the powers that be in documentation land see this they
                              > could make it easier to find this script or consider it for default
                              > inclusion in an install? Yay, no need to learn emacs tonight. ; )

                              The most likely way for the docs to change is if you figure out
                              where in the docs it would be most helpful, edit your copy of the help
                              file, and submit a patch to Bram. If he likes it, it will appear in the
                              next release (vim 6.3).

                              HTH --Benji Fisher
                            Your message has been successfully submitted and would be delivered to recipients shortly.