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

RE: Most Valuable Tricks

Expand Messages
  • Zdenek Sekera
    ... I totally agree with Mathew Winn original posting, it s regular expressions for me, too, that really make a difference. I have the book you mentioned, use
    Message 1 of 27 , Sep 30, 2005
    • 0 Attachment
      > -----Original Message-----
      > From: John Love-Jensen [mailto:eljay@...]
      > Sent: 30 September 2005 13:33
      > To: MSX to Vim
      > Subject: Re: Most Valuable Tricks
      >
      > Hi everyone,
      >
      > > It's astonishing how many apparently impossible tasks
      > become trivial with
      > regular expression matching or replacing.
      >
      > Agreed!
      >
      > Here's a book I highly recommend:
      >
      > Mastering Regular Expressions
      > by Jeffery E. F. Friedl
      > http://www.amazon.com/exec/obidos/tg/detail/-/0596002890
      >
      > It really is *THE* definitive book on mastering regular expressions.

      I totally agree with Mathew Winn original posting, it's regular
      expressions for me, too, that really make a difference.
      I have the book you mentioned, use it every time I realize
      I again forgot something, but you'll agree that vim has even
      more of RE possibilities.

      Wouldn't it be time that someone real guru would sacrifice
      himself and write some guide/cookbook/or "how to master
      vim Regexp's" ? I am sometimes really amazed when reading
      posts on this forum what some people are capable of doing
      with RE's. Takes me some time to decipher that.

      ---Zdenek
    • Stephen R Laniel
      ... Yes, Friedl s book has this. And I agree that it s really irritating trying to keep track of which regex uses what. Me, I think Perl s is the smartest: all
      Message 2 of 27 , Sep 30, 2005
      • 0 Attachment
        On Fri, Sep 30, 2005 at 08:09:22AM -0500, Tim Chase wrote:
        > Having not read this text in particular, does it have (or do you
        > know of) a nifty chart comparing regexp operators in various
        > utilities?

        Yes, Friedl's book has this. And I agree that it's really
        irritating trying to keep track of which regex uses what.
        Me, I think Perl's is the smartest: all metacharacters are
        unescaped. That's a nice straightahead rule, rather than
        remembering that (for instance) the '.' metacharacter is
        unescaped and '{' is escaped. Silliness. PCREs for all.

        --
        Stephen R. Laniel
        steve@...
        +(617) 308-5571
        http://laniels.org/
        PGP key: http://laniels.org/slaniel.key
      • Tim Chase
        ... Having not read this text in particular, does it have (or do you know of) a nifty chart comparing regexp operators in various utilities? It drives me nuts
        Message 3 of 27 , Sep 30, 2005
        • 0 Attachment
          > Mastering Regular Expressions by Jeffery E. F. Friedl
          > http://www.amazon.com/exec/obidos/tg/detail/-/0596002890
          >
          > It really is *THE* definitive book on mastering regular
          > expressions.

          Having not read this text in particular, does it have (or do you
          know of) a nifty chart comparing regexp operators in various
          utilities?

          It drives me nuts switching between Vim, Sed, Ed, Grep (and with
          Sed/Grep, which quotes I use in bash trigger differing needs for
          escaping), Posix regexps, Perl/pcre, Java's regexps, Python's
          regexps, PHP's regexps, etc. It would be handy to have some big
          table where I could look up the Vim way (with which I'm most
          versed) and then see how to translate that into the other ones.
          Things like

          -Are groupings done with "\(...\)" or just unescaped parens?

          -Do \< and \> work, or does the program want \b

          -Does non-greedy ("\{-}") matching work in this program?

          -Does the N-M matching need to be escaped? ("\{2,3}" vs. "{2,3}"
          vs "\{2,3\}")

          These are just some of those little gotchas that I can pull off
          the top of my head. However, it would be handy to be able to
          look it up somewhere to convince myself that I'm *not* crazy (or
          at least any more than usual ;) and that a given regexp *should*
          work, just in another application.

          -tim
        • Matthew Winn
          ... And just to _really_ throw a spanner in the works, Perl 6 has a new regular expression syntax. Regular expression syntaxes multiply like coathangers in a
          Message 4 of 27 , Sep 30, 2005
          • 0 Attachment
            On Fri, Sep 30, 2005 at 09:08:23AM -0400, Stephen R Laniel wrote:
            > On Fri, Sep 30, 2005 at 08:09:22AM -0500, Tim Chase wrote:
            > > Having not read this text in particular, does it have (or do you
            > > know of) a nifty chart comparing regexp operators in various
            > > utilities?
            >
            > Yes, Friedl's book has this. And I agree that it's really
            > irritating trying to keep track of which regex uses what.

            And just to _really_ throw a spanner in the works, Perl 6 has a new
            regular expression syntax. Regular expression syntaxes multiply
            like coathangers in a cupboard.

            > Me, I think Perl's is the smartest: all metacharacters are
            > unescaped. That's a nice straightahead rule, rather than
            > remembering that (for instance) the '.' metacharacter is
            > unescaped and '{' is escaped. Silliness. PCREs for all.

            I can see why there's a difference, though. In Perl you're creating an
            expression that will be written once but used many times, so the simple
            policy of
            | backslashed no backslash
            --------------|----------------------------
            letter | special literal
            punctuation | literal special

            makes for an easy syntax to remember. In Vi, on the other hand, most
            expressions are typed once and used once so it's more important to make
            the most common uses quick to type. Leaning toothpick syndrome isn't a
            problem if you write your expression once and then leave it lurking in a
            program forever, but you wouldn't want it in an interactive environment.

            The main difficulty I have with regular expressions is all the different
            styles of grep out there. I don't just mean the fact that I have to
            choose between egrep, fgrep and grep, but also that different versions
            of Unix have different greps, each with a different set of characters
            requiring backslashes.

            --
            Matthew Winn (matthew@...)
          • Stephen R Laniel
            ... Isn t the most common use of { the metacharacter use, though? Likewise for . , and [ , and I m pretty sure all of the others. So if the metacharacter
            Message 5 of 27 , Sep 30, 2005
            • 0 Attachment
              On Fri, Sep 30, 2005 at 02:55:43PM +0100, Matthew Winn wrote:
              > makes for an easy syntax to remember. In Vi, on the other hand, most
              > expressions are typed once and used once so it's more important to make
              > the most common uses quick to type.

              Isn't the most common use of '{' the metacharacter use,
              though? Likewise for '.', and '[', and I'm pretty sure all
              of the others. So if the metacharacter use is the most
              common, shouldn't metacharacters all be unescaped? I think
              that's the reasoning behind Perl's escaping policies. (To
              the extent that Perl can be said to have policies [1].)

              I also think Perl's ability to choose your own quote- and
              substitution-delimiter makes it easier to type. E.g., if you
              could only use 's/pattern/sub/options', and you wanted to do
              some HTML substitution in Perl, you'd have

              s/<oldTag>(.*?)<\/oldTag>/<newTag>$1<\/newTag>/g

              which is messy, whereas Perl's

              s{<oldTag>(.*?)</oldTag}{<newTag>$1</newTag>}g

              is clean.

              [1] - http://www.wall.org/~larry/pm.html

              --
              Stephen R. Laniel
              steve@...
              +(617) 308-5571
              http://laniels.org/
              PGP key: http://laniels.org/slaniel.key
            • Chris Allen
              ... I think the consensus has been that what you ve said is true for Perl but not for Vim. When I code I certainly do a lot of grepping for literal characters
              Message 6 of 27 , Sep 30, 2005
              • 0 Attachment
                On 30/09/05, Stephen R Laniel <steve@...> wrote:
                > Isn't the most common use of '{' the metacharacter use,
                > though? Likewise for '.', and '[', and I'm pretty sure all
                > of the others. So if the metacharacter use is the most
                > common, shouldn't metacharacters all be unescaped? I think
                > that's the reasoning behind Perl's escaping policies. (To
                > the extent that Perl can be said to have policies [1].)

                I think the consensus has been that what you've said is true for Perl
                but not for Vim. When I code I certainly do a lot of grepping for
                literal characters like () (functional calls everywhere), {} (Perl
                hashes) and [] (arrays everywhere). I generally do *much* less
                complicated greppage in Vim than in Perl and it could be argued that
                matching what I am searching for by default is the correct behaviour.
                Of course, as a Perl programmer I know regexps and think magic
                properties should be identical everywhere. Others may not agree, so
                perhaps this is a feature for them.

                Thanks,
                Chris Allen
              • Gareth Oakes
                ... My view on the whole situation is that it would be much quicker for everyone to have one scheme or the other exclusively. It takes more time to figure out
                Message 7 of 27 , Sep 30, 2005
                • 0 Attachment
                  > I think the consensus has been that what you've said is true for Perl
                  > but not for Vim. When I code I certainly do a lot of grepping for
                  > literal characters like () (functional calls everywhere), {} (Perl
                  > hashes) and [] (arrays everywhere). I generally do *much* less
                  > complicated greppage in Vim than in Perl and it could be argued that
                  > matching what I am searching for by default is the correct behaviour.
                  > Of course, as a Perl programmer I know regexps and think magic
                  > properties should be identical everywhere. Others may not agree, so
                  > perhaps this is a feature for them.

                  My view on the whole situation is that it would be much quicker for
                  everyone to have one scheme or the other exclusively. It takes more
                  time to figure out which characters need escaping or not than to
                  actually just type the backslashes.

                  -G
                • Mikolaj Machowski
                  ... Vim also has this possibility, just choice is limited to non-pair elements like +. m.
                  Message 8 of 27 , Sep 30, 2005
                  • 0 Attachment
                    Dnia piątek, 30 września 2005 16:02, Stephen R Laniel napisał:
                    > I also think Perl's ability to choose your own quote- and
                    > substitution-delimiter makes it easier to type. E.g., if you
                    > could only use 's/pattern/sub/options', and you wanted to do
                    > some HTML substitution in Perl, you'd have
                    >
                    > s/<oldTag>(.*?)<\/oldTag>/<newTag>$1<\/newTag>/g
                    >
                    > which is messy, whereas Perl's
                    >
                    > s{<oldTag>(.*?)</oldTag}{<newTag>$1</newTag>}g

                    Vim also has this possibility, just choice is limited to non-pair
                    elements like +.

                    m.
                  • Dave Roberts
                    ... The global command (:h :g) My favorite example of this is the creation of a DOS batch file to run a program and show the return code. Under DOS, you check
                    Message 9 of 27 , Sep 30, 2005
                    • 0 Attachment
                      Frederico Franzosi wrote:

                      >Hi, I've been a reader of the list for about 2 months and I've been
                      >collecting lots of knowledge about vim. I think I'm still a begginer
                      >facing people in the list. Anyway, my editing abilities grown up so
                      >much with Vim that I decided to give a (introductory) course of it in
                      >a Free Software event ( http://www.inf.ufes.br/eri2005/?q=node/4 ) in
                      >my town. So, I've been collecting lots of material at Vim.org
                      >Tips/Scrips, vim online-help (wich I think it's the best help system
                      >ever created), other sites in the internet and here. Even though, I've
                      >decided to ask you for some motivational tricks you can do with Vim,
                      >like when the editor really saved life (or at least hours of work).
                      >
                      > Hope I get some good answer here.
                      > Thanks anyway
                      > Frederico Franzosi
                      >
                      >
                      >

                      The global command (:h :g)

                      My favorite example of this is the creation of a DOS batch file to run a
                      program and show the return code.

                      Under DOS, you check the return code in a batch file using "ERRORLEVEL
                      n" which is TRUE if the errorlevel is n OR HIGHER. Since the return code
                      can be anything from 0 to 255 you actually have to test every value from
                      255 down to 1.

                      The following will put the proper key-sequence into register 'a' to
                      create this batch file:

                      exe 'let @a="iecho off\<CR>set RC=0\<CR>%1 %2 %3 %4 %5 %6 %7 %8
                      %9\<CR>\<CR>if errorlevel 255 set RC=255\<CR>if not %RC% == 0 goto
                      done\<ESC>2k3Y254P:let i=255|g/255/s//\\=i/g|let
                      i=i-1\<CR>Go\<CR>:done\<CR>echo RC = %RC%\<CR>set RC=\<CR>\<ESC>"'

                      Create an empty batch file (eg. vim retcode.bat) and cut and paste the
                      above (make sure it's one line) into the command line
                      (<colon><ctrl-r>*<cr>) to put the key sequence into register 'a' then
                      just run the key-sequence with @a to create a useful 700 line batch file
                      which is used by passing the program and its parameters to it.

                      The relevant part (to replace all the 255's with decreasing numbers) is:
                      :let i=255 | g/255/s//\=i/g | let i=i-1

                      - Dave
                    • A. J. Mechelynck
                      ... To me, the most valuable tricks are those which allow finding more useful ways of using Vim: - completion with menu set nocompatible ... set wildmenu -
                      Message 10 of 27 , Oct 1, 2005
                      • 0 Attachment
                        Frederico Franzosi wrote:
                        > Hi, I've been a reader of the list for about 2 months and I've been
                        > collecting lots of knowledge about vim. I think I'm still a begginer
                        > facing people in the list. Anyway, my editing abilities grown up so
                        > much with Vim that I decided to give a (introductory) course of it in
                        > a Free Software event ( http://www.inf.ufes.br/eri2005/?q=node/4 ) in
                        > my town. So, I've been collecting lots of material at Vim.org
                        > Tips/Scrips, vim online-help (wich I think it's the best help system
                        > ever created), other sites in the internet and here. Even though, I've
                        > decided to ask you for some motivational tricks you can do with Vim,
                        > like when the editor really saved life (or at least hours of work).
                        >
                        > Hope I get some good answer here.
                        > Thanks anyway
                        > Frederico Franzosi
                        >
                        >
                        >
                        >
                        To me, the most valuable tricks are those which allow finding more
                        useful ways of using Vim:

                        - completion with menu
                        set nocompatible
                        ...
                        set wildmenu

                        - completion on the :help command
                        :help blahblah<Tab>
                        (where blahblah is a pattern)

                        - the :helpgrep command
                        :helpgrep blahblah
                        (where blahblah is a pattern)
                        for instance
                        :helpgrep \<blahblah\>
                        (where blahblah is a word)
                        followed by "quickfix" commands such as :cnext

                        - the Search function at vim-online.
                        http://vim.sourceforge.net/
                        -> Search


                        Best regards,
                        Tony.
                      • A. J. Mechelynck
                        ... [...] Religious Integrist! ;-) Flaming will get us nowhere. Myself, I don t particularly care for any scripting languages other than Vim script and shell
                        Message 11 of 27 , Oct 1, 2005
                        • 0 Attachment
                          Neil Watson wrote:
                          > On Thu, Sep 29, 2005 at 09:36:28AM -0500, Tim Chase wrote:
                          >> For those in the crowd (such as myself) that don't particularly care
                          >> for Perl,
                          >
                          > Blasphemer!
                          [...]

                          Religious Integrist! ;-)

                          Flaming will get us nowhere.

                          Myself, I "don't particularly care" for any scripting languages other
                          than Vim script and shell script, yet I include all possible languages
                          in the Vim executables that I compile and publish, for the benefit of
                          anyone who may want them.


                          Best regards,
                          Tony.
                        • A. J. Mechelynck
                          Matthew Winn wrote: [...] ... So hurrah for the new (v7) command :vimgrep, which does away with the need for an external grep command to handle file searching
                          Message 12 of 27 , Oct 1, 2005
                          • 0 Attachment
                            Matthew Winn wrote:
                            [...]
                            > The main difficulty I have with regular expressions is all the different
                            > styles of grep out there. I don't just mean the fact that I have to
                            > choose between egrep, fgrep and grep, but also that different versions
                            > of Unix have different greps, each with a different set of characters
                            > requiring backslashes.


                            So hurrah for the new (v7) command :vimgrep, which does away with the
                            need for an external grep command to handle file searching in Vim.

                            Best regards,
                            Tony.
                          • Frederico Franzosi
                            I d like to thank you all for the valuable answers... You guys are the best!!! Happy vimming
                            Message 13 of 27 , Oct 3, 2005
                            • 0 Attachment
                              I'd like to thank you all for the valuable answers...
                              You guys are the best!!!

                              Happy vimming

                              On 10/1/05, A. J. Mechelynck <antoine.mechelynck@...> wrote:
                              > Matthew Winn wrote:
                              > [...]
                              > > The main difficulty I have with regular expressions is all the different
                              > > styles of grep out there. I don't just mean the fact that I have to
                              > > choose between egrep, fgrep and grep, but also that different versions
                              > > of Unix have different greps, each with a different set of characters
                              > > requiring backslashes.
                              >
                              >
                              > So hurrah for the new (v7) command :vimgrep, which does away with the
                              > need for an external grep command to handle file searching in Vim.
                              >
                              > Best regards,
                              > Tony.
                              >
                              >
                            • David Fishburn
                              ... One of the features I use the most is use Visual Block mode to comment and uncomment code. one two three ... Place your cursor on the o of one. CTRL-v
                              Message 14 of 27 , Oct 7, 2005
                              • 0 Attachment
                                > -----Original Message-----
                                > From: Frederico Franzosi [mailto:ffranzosi@...]
                                > Sent: Thursday, September 29, 2005 8:19 AM
                                > To: vim@...
                                > Subject: Most Valuable Tricks
                                >
                                > Hi, I've been a reader of the list for about 2 months and
                                > I've been collecting lots of knowledge about vim. I think I'm
                                > still a begginer facing people in the list. Anyway, my
                                > editing abilities grown up so much with Vim that I decided to
                                > give a (introductory) course of it in a Free Software event (
                                > http://www.inf.ufes.br/eri2005/?q=node/4 ) in my town. So,
                                > I've been collecting lots of material at Vim.org Tips/Scrips,
                                > vim online-help (wich I think it's the best help system ever
                                > created), other sites in the internet and here. Even though,
                                > I've decided to ask you for some motivational tricks you can
                                > do with Vim, like when the editor really saved life (or at
                                > least hours of work).

                                One of the features I use the most is use Visual Block mode to comment and
                                uncomment code.

                                one
                                two
                                three

                                To comment the above lines:
                                ------------------
                                Place your cursor on the "o" of one.
                                CTRL-v
                                Down arrow (j) until the "t" in three is visually selected.
                                I// (capital I, followed by your comment character).
                                <Esc>

                                To uncomment the above lines:
                                ------------------
                                Place your cursor on the first "/" of the first line.
                                CTRL-v
                                Down arrow (j) until the "/" in three is visually selected.
                                Right arrow (l) until both "//"s are visually selected.
                                x


                                Voila.

                                I use this constantly.

                                Dave
                              • calmar
                                ... Just as a side note, there is also EnhancedCommentify.vim , what allows e.g marking the area and do something like a c or x for commenting. Cheers marco
                                Message 15 of 27 , Oct 9, 2005
                                • 0 Attachment
                                  On Fri, Oct 07, 2005 at 10:22:04AM -0400, David Fishburn wrote:
                                  >
                                  > One of the features I use the most is use Visual Block mode to comment and
                                  > uncomment code.
                                  >
                                  > one
                                  > two
                                  > three
                                  >
                                  > To comment the above lines:
                                  > ------------------
                                  > Place your cursor on the "o" of one.
                                  > CTRL-v
                                  > Down arrow (j) until the "t" in three is visually selected.
                                  > I// (capital I, followed by your comment character).
                                  > <Esc>
                                  >
                                  > To uncomment the above lines:
                                  > ------------------
                                  > Place your cursor on the first "/" of the first line.
                                  > CTRL-v
                                  > Down arrow (j) until the "/" in three is visually selected.
                                  > Right arrow (l) until both "//"s are visually selected.
                                  > x
                                  >

                                  Just as a side note, there is also 'EnhancedCommentify.vim',
                                  what allows e.g marking the area and do something like a \c or \x for
                                  commenting.

                                  Cheers
                                  marco

                                  --
                                  calmar
                                  (o_ It rocks: LINUX + Command-Line-Interface
                                  //\
                                  V_/_ http://www.calmar.ws
                                Your message has been successfully submitted and would be delivered to recipients shortly.