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

Search for several words in each line with vim?

Expand Messages
  • rewar
    Hi, I have looked around for a way to do this but just cant seem to find it. Basically, I have very large documents where I want to often search for a few
    Message 1 of 16 , Jun 2, 2010
    • 0 Attachment
      Hi,

      I have looked around for a way to do this but just cant seem to find
      it. Basically, I have very large documents where I want to often
      search for a few words in a line, but they wont be all together.

      For example, I want to be able to find the line below in a large text
      document by simply searching "cat dog"
      "cats are smaller than dogs"

      All editors I come across only allows you to search for an exact match
      of that field "cat dog" in any line.

      --
      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
    • rewar
      Hi, I have looked around for a way to do this but just cant seem to find it. Basically, I have very large documents where I want to often search for a few
      Message 2 of 16 , Jun 2, 2010
      • 0 Attachment
        Hi,

        I have looked around for a way to do this but just cant seem to find
        it. Basically, I have very large documents where I want to often
        search for a few words in a line, but they wont be all together.

        For example, I want to be able to find the line below in a large text
        document by simply searching "cat dog"
        "cats are smaller than dogs"

        All editors I come across only allows you to search for an exact match
        of that field "cat dog" in any line.

        --
        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
      • Christian Brabandt
        ... Try this: /^ (.*cats.* ) & (.*dogs.* )$ This finds any line which contains cats and dogs in any order. ... There are probably several editors, that support
        Message 3 of 16 , Jun 2, 2010
        • 0 Attachment
          On Wed, June 2, 2010 4:29 pm, rewar wrote:
          > Hi,
          >
          > I have looked around for a way to do this but just cant seem to find
          > it. Basically, I have very large documents where I want to often
          > search for a few words in a line, but they wont be all together.
          >
          > For example, I want to be able to find the line below in a large text
          > document by simply searching "cat dog"
          > "cats are smaller than dogs"
          >

          Try this:
          /^\(.*cats.*\)\&\(.*dogs.*\)$

          This finds any line which contains cats and dogs in any order.

          > All editors I come across only allows you to search for an exact match
          > of that field "cat dog" in any line.

          There are probably several editors, that support regular expressions.
          though probably not all of them have a that powerful RE-engine as Vim.

          regards,
          Christian

          --
          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
        • rewar
          thanks for the prompt reply! i really appreciate it. it worked perfectly, however it didnt require the $ sign? also, how would u do it so that its not case
          Message 4 of 16 , Jun 2, 2010
          • 0 Attachment
            thanks for the prompt reply! i really appreciate it. it worked
            perfectly, however it didnt require the "$" sign?

            also, how would u do it so that its not case sensitive? so it will
            return a line with CaTs and DOGS

            further, how do you become better at using such an editor as vim? I
            only know some basic stuff for c programming and when i need to do
            something like search/replace, i just google it. is there a good guide
            to start with? I mean that statement u wrote just then was quite
            complex for me to even understand lol

            On Jun 3, 1:00 am, "Christian Brabandt" <cbli...@...> wrote:
            > On Wed, June 2, 2010 4:29 pm, rewar wrote:
            > > Hi,
            >
            > > I have looked around for a way to do this but just cant seem to find
            > > it. Basically, I have very large documents where I want to often
            > > search for a few words in a line, but they wont be all together.
            >
            > > For example, I want to be able to find the line below in a large text
            > > document by simply searching "cat dog"
            > > "cats are smaller than dogs"
            >
            > Try this:
            > /^\(.*cats.*\)\&\(.*dogs.*\)$
            >
            > This finds any line which contains cats and dogs in any order.
            >
            > > All editors I come across only allows you to search for an exact match
            > > of that field "cat dog" in any line.
            >
            > There are probably several editors, that support regular expressions.
            > though probably not all of them have a that powerful RE-engine as Vim.
            >
            > regards,
            > Christian

            --
            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
          • Greg Klein
            ... Yes, and I think that this pattern could be simplified to /.*cats &.*dogs See similar example given by :help / & Greg -- You received this message from the
            Message 5 of 16 , Jun 2, 2010
            • 0 Attachment
              >
              > On Wed, June 2, 2010 4:29 pm, rewar wrote:
              > > Hi,
              > >
              > > I have looked around for a way to do this but just cant
              > seem to find
              > > it. Basically, I have very large documents where I want to often
              > > search for a few words in a line, but they wont be all together.
              > >
              > > For example, I want to be able to find the line below in a
              > large text
              > > document by simply searching "cat dog"
              > > "cats are smaller than dogs"
              > >
              >
              > Try this:
              > /^\(.*cats.*\)\&\(.*dogs.*\)$
              >
              > This finds any line which contains cats and dogs in any order.
              >
              > regards,
              > Christian
              >

              Yes, and I think that this pattern could be simplified to
              /.*cats\&.*dogs
              See similar example given by :help /\&

              Greg

              --
              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
            • Christian Brabandt
              Hi rewar! ... Yes that is true. $ means line end. Since there was a .* beofre it, it will automatically match until the end of the line. ... Add c in the
              Message 6 of 16 , Jun 2, 2010
              • 0 Attachment
                Hi rewar!

                On Mi, 02 Jun 2010, rewar wrote:

                > thanks for the prompt reply! i really appreciate it. it worked
                > perfectly, however it didnt require the "$" sign?

                Yes that is true. $ means line end. Since there was a .* beofre it, it
                will automatically match until the end of the line.

                > also, how would u do it so that its not case sensitive? so it will
                > return a line with CaTs and DOGS

                Add \c in the pattern. You should really lern to use regular
                expressions. Take a look at :h pattern.txt and :h usr_27.txt
                > further, how do you become better at using such an editor as vim? I
                > only know some basic stuff for c programming and when i need to do
                > something like search/replace, i just google it. is there a good guide
                > to start with? I mean that statement u wrote just then was quite
                > complex for me to even understand lol

                Read the help. When finished, read it again ;)

                This is really good and basically explains everything. Of course you can
                also hang up here and learn from others. That was how I did it ;)

                So take a look at :h help.txt and :h usr_toc.txt

                Oh and the wiki (http://vim.wikia.com) is probably also worth a look.

                [Fullquote snipped]
                please don't top quote.

                regards,
                Christian
                --
                Der donnernde Gott, der zürnende Gott, der rächende Gott. Was für ein
                Choleriker.
                -- Heinrich Wiesner

                --
                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
              • Christian Brabandt
                Hi Greg! ... True. But my highlighting looks prettier ;) regards, Christian -- Seien wir mal ehrlich, Das Leben ist immer lebensgefährlich. -- Erich Kästner
                Message 7 of 16 , Jun 2, 2010
                • 0 Attachment
                  Hi Greg!

                  On Mi, 02 Jun 2010, Greg Klein wrote:

                  > >
                  > > Try this:
                  > > /^\(.*cats.*\)\&\(.*dogs.*\)$
                  > >
                  > > This finds any line which contains cats and dogs in any order.
                  >
                  > Yes, and I think that this pattern could be simplified to
                  > /.*cats\&.*dogs
                  > See similar example given by :help /\&

                  True. But my highlighting looks prettier ;)

                  regards,
                  Christian
                  --
                  Seien wir mal ehrlich,
                  Das Leben ist immer lebensgefährlich.
                  -- Erich Kästner

                  --
                  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
                • caruso_g
                  You can take a look also at http://www.swaroopch.com/notes/Vim or http://vimdoc.sourceforge.net/ http://vimdoc.sourceforge.net/htmldoc/usr_toc.html
                  Message 8 of 16 , Jun 2, 2010
                  • 0 Attachment
                    You can take a look also at
                    http://www.swaroopch.com/notes/Vim
                    or
                    http://vimdoc.sourceforge.net/
                    http://vimdoc.sourceforge.net/htmldoc/usr_toc.html
                    http://www.oualline.com/vim-cook.html

                    lastly Google always works well: http://rayninfo.co.uk/vimtips.html

                    I think Vim is one of the most documented editor in the world… :)

                    @Cristian
                    Is there a way not to escape RE but escape just keywords like /w /d
                    and so on?

                    --
                    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
                  • Tony Mechelynck
                    On 02/06/10 18:14, rewar wrote: [...] ... [...] With Vim, don t google: use the help instead. It s the only program I know whose help contains *everything*.
                    Message 9 of 16 , Jun 2, 2010
                    • 0 Attachment
                      On 02/06/10 18:14, rewar wrote:
                      [...]
                      > further, how do you become better at using such an editor as vim? I
                      > only know some basic stuff for c programming and when i need to do
                      > something like search/replace, i just google it. is there a good guide
                      > to start with? I mean that statement u wrote just then was quite
                      > complex for me to even understand lol
                      [...]

                      With Vim, don't google: use the help instead. It's the only program I
                      know whose help contains *everything*.

                      The :help command accepts one optional argument: the topic about which
                      you need help. That argument can be auto-completed: type part of it,
                      then <Tab> instead of <Enter>, and Vim will complete the first (or only)
                      matching tag. (By "first", I mean "what Vim thinks is the best".)

                      If you have 'wildmenu' set (which is not the default), hitting <Tab> to
                      complete an ex-command (not just :help) will give you a menu of possible
                      completions on the status line. Select with <Left> and <Right>, and, for
                      filenames and menus (but not helptags) navigate the tree with <Down> and
                      <Up>; then accept with <Enter> or abort with <Esc>.

                      There are various lists of pointers to help subjects: the first helpfile
                      (which you get by hitting F1) has a list of all the others; ":help
                      index" lists the commands for Insert, Normal, Visual and Command-line
                      modes; ":help function-list" has a list of functions by subject (with a
                      hotlink to the alphabetical list), etc.

                      But when all hope of finding your needle in that huge haystack seems
                      lost, there is one extremely useful command: :helpgrep. It accepts one
                      Vim-style regular expression (without the bounding slashes) and will
                      search the whole contents on the help for anything matching that
                      pattern. Results are given as a quickfix list (see :help quickfix.txt),
                      navigate them with :cnext, :cprev, :cnfile, :cpfile, :cfirst, :clast
                      (which may of course be assigned to single keys, e.g.

                      :map <F2> :cnext<CR>
                      :map <S-F2> ;cprev<CR>

                      or else, :copen will open (and, afterwards, :cclose will close) a window
                      showing one line per match with the filename, linenumber, and the
                      relevant line of text.


                      Best regards,
                      Tony.
                      --
                      Wombat's Laws of Computer Selection:
                      (1) If it doesn't run Unix, forget it.
                      (2) Any computer design over 10 years old is obsolete.
                      (3) Anything made by IBM is junk. (See number 2)
                      (4) The minimum acceptable CPU power for a single user is a
                      VAX/780 with a floating point accelerator.
                      (5) Any computer with a mouse is worthless.
                      -- Rich Kulawiec

                      --
                      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
                    • Ben Fritz
                      ... Yes. Start your regex with v (see :help / v). So, this: /^ (.*cats.* ) & (.*dogs.* )$ Becomes this: / v^(.*cats.*)&(.*dogs.*)$ -- You received this
                      Message 10 of 16 , Jun 2, 2010
                      • 0 Attachment
                        On Jun 2, 2:59 pm, caruso_g <peppecar...@...> wrote:
                        > Is there a way not to escape RE but escape just keywords like /w /d
                        > and so on?

                        Yes. Start your regex with \v (see :help /\v).

                        So, this:

                        /^\(.*cats.*\)\&\(.*dogs.*\)$

                        Becomes this:

                        /\v^(.*cats.*)&(.*dogs.*)$

                        --
                        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
                      • Ben Fritz
                        ... I should mention, and this is a somewhat recent development, that we have a collection of getting started tips:
                        Message 11 of 16 , Jun 2, 2010
                        • 0 Attachment
                          On Jun 2, 11:53 am, Christian Brabandt <cbli...@...> wrote:
                          >
                          > Oh and the wiki (http://vim.wikia.com) is probably also worth a look.
                          >

                          I should mention, and this is a somewhat recent development, that we
                          have a collection of "getting started" tips:

                          http://vim.wikia.com/wiki/Category:Getting_started

                          --
                          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
                        • Andrei Popescu
                          ... Wow, that s a great tip! Regards, Andrei -- http://nuvreauspam.ro/2010/05/4-neticheta-pe-mail/
                          Message 12 of 16 , Jun 2, 2010
                          • 0 Attachment
                            On Mi, 02 iun 10, 22:28:38, Tony Mechelynck wrote:
                            >
                            > But when all hope of finding your needle in that huge haystack seems
                            > lost, there is one extremely useful command: :helpgrep. It accepts
                            > one Vim-style regular expression (without the bounding slashes) and
                            > will search the whole contents on the help for anything matching
                            > that pattern. Results are given as a quickfix list (see :help
                            > quickfix.txt), navigate them with :cnext, :cprev, :cnfile, :cpfile,
                            > :cfirst, :clast (which may of course be assigned to single keys,
                            > e.g.
                            >
                            > :map <F2> :cnext<CR>
                            > :map <S-F2> ;cprev<CR>
                            >
                            > or else, :copen will open (and, afterwards, :cclose will close) a
                            > window showing one line per match with the filename, linenumber, and
                            > the relevant line of text.

                            Wow, that's a great tip!

                            Regards,
                            Andrei
                            --
                            http://nuvreauspam.ro/2010/05/4-neticheta-pe-mail/
                          • Tony Mechelynck
                            ... before cprev it should be a : not ; character (of course). ... ...and I forgot to say: hit Enter on any line of the quickfix window to see the
                            Message 13 of 16 , Jun 4, 2010
                            • 0 Attachment
                              On 03/06/10 08:19, Andrei Popescu wrote:
                              > On Mi, 02 iun 10, 22:28:38, Tony Mechelynck wrote:
                              >>
                              >> But when all hope of finding your needle in that huge haystack seems
                              >> lost, there is one extremely useful command: :helpgrep. It accepts
                              >> one Vim-style regular expression (without the bounding slashes) and
                              >> will search the whole contents on the help for anything matching
                              >> that pattern. Results are given as a quickfix list (see :help
                              >> quickfix.txt), navigate them with :cnext, :cprev, :cnfile, :cpfile,
                              >> :cfirst, :clast (which may of course be assigned to single keys,
                              >> e.g.
                              >>
                              >> :map<F2> :cnext<CR>
                              >> :map<S-F2> ;cprev<CR>

                              before cprev it should be a : not ; character (of course).

                              >>
                              >> or else, :copen will open (and, afterwards, :cclose will close) a
                              >> window showing one line per match with the filename, linenumber, and
                              >> the relevant line of text.
                              >
                              > Wow, that's a great tip!
                              >
                              > Regards,
                              > Andrei

                              ...and I forgot to say: hit Enter on any line of the quickfix window to
                              see the corresponding line in context (as part of the file it came from).


                              Best regards,
                              Tony.
                              --
                              ARTHUR: I command you as King of the Britons to stand aside!
                              BLACK KNIGHT: I move for no man.
                              The Quest for the Holy Grail (Monty
                              Python)

                              --
                              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
                            • Phillip Bruce
                              Can anyone provide me the link to unsubscribe to this list? Thanks, Phillip On Fri, Jun 4, 2010 at 5:47 PM, Tony Mechelynck
                              Message 14 of 16 , Jun 4, 2010
                              • 0 Attachment
                                Can anyone provide me the link to unsubscribe to this list?

                                Thanks,

                                Phillip

                                On Fri, Jun 4, 2010 at 5:47 PM, Tony Mechelynck <antoine.mechelynck@...> wrote:
                                On 03/06/10 08:19, Andrei Popescu wrote:
                                On Mi, 02 iun 10, 22:28:38, Tony Mechelynck wrote:

                                But when all hope of finding your needle in that huge haystack seems
                                lost, there is one extremely useful command: :helpgrep. It accepts
                                one Vim-style regular expression (without the bounding slashes) and
                                will search the whole contents on the help for anything matching
                                that pattern. Results are given as a quickfix list (see :help
                                quickfix.txt), navigate them with :cnext, :cprev, :cnfile, :cpfile,
                                :cfirst, :clast (which may of course be assigned to single keys,
                                e.g.

                                       :map<F2>  :cnext<CR>
                                       :map<S-F2>  ;cprev<CR>

                                before cprev it should be a : not ; character (of course).


                                or else, :copen will open (and, afterwards, :cclose will close) a
                                window showing one line per match with the filename, linenumber, and
                                the relevant line of text.

                                Wow, that's a great tip!

                                Regards,
                                Andrei

                                ...and I forgot to say: hit Enter on any line of the quickfix window to see the corresponding line in context (as part of the file it came from).


                                Best regards,
                                Tony.
                                --
                                ARTHUR:        I command you as King of the Britons to stand aside!
                                BLACK KNIGHT:  I move for no man.
                                                                 The Quest for the Holy Grail (Monty Python)


                                --
                                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



                                --
                                Phillip Bruce

                                --
                                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
                              • Tony Mechelynck
                                ... [...] At the bottom of every list post (and, I think, three times [due to quoting] at the bottom of this one) there is a link to the mailing lists page,
                                Message 15 of 16 , Jun 4, 2010
                                • 0 Attachment
                                  On 05/06/10 03:15, Phillip Bruce wrote:
                                  > Can anyone provide me the link to unsubscribe to this list?
                                  >
                                  > Thanks,
                                  >
                                  > Phillip
                                  [...]

                                  At the bottom of every list post (and, I think, three times [due to
                                  quoting] at the bottom of this one) there is a link to the mailing lists
                                  page, where you can read how to subscribe, unsubscribe, post, etc.

                                  > --
                                  > 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
                                  >
                                  >
                                  >
                                  >
                                  > --
                                  > Phillip Bruce
                                  >
                                  > --
                                  > 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

                                  Best regards,
                                  Tony.
                                  --
                                  Puns are little "plays on words" that a certain breed of person loves
                                  to spring on you and then look at you in a certain self-satisfied way
                                  to indicate that he thinks that you must think that he is by far the
                                  cleverest person on Earth now that Benjamin Franklin is dead, when in
                                  fact what you are thinking is that if this person ever ends up in a
                                  lifeboat, the other passengers will hurl him overboard by the end of
                                  the first day even if they have plenty of food and water.
                                  -- Dave Barry, "Why Humor is Funny"

                                  --
                                  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
                                • Charles E Campbell Jr
                                  ... I see you have your answer already, but here s an answer for a related question: how to find lines that have cats and dogs , and other Boolean logic
                                  Message 16 of 16 , Jul 7, 2010
                                  • 0 Attachment
                                    rewar wrote:
                                    > Hi,
                                    >
                                    > I have looked around for a way to do this but just cant seem to find
                                    > it. Basically, I have very large documents where I want to often
                                    > search for a few words in a line, but they wont be all together.
                                    >
                                    > For example, I want to be able to find the line below in a large text
                                    > document by simply searching "cat dog"
                                    > "cats are smaller than dogs"
                                    >
                                    > All editors I come across only allows you to search for an exact match
                                    > of that field "cat dog" in any line.
                                    >

                                    I see you have your answer already, but here's an answer for a related
                                    question: how to find lines that have "cats" and "dogs", and other
                                    Boolean logic combinations:

                                    http://mysite.verizon.net/astronaut/vim/index.html#LOGIPAT

                                    With it:

                                    your original Q: :LP "cats" | "dogs"

                                    modified Q: :LP "cats" & "dogs"

                                    and lines with cats but no dogs: :LP "cats" & !"dogs"

                                    Regards,
                                    Chip Campbell

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