Searching for any one of a set or words ?

34 views
Skip to first unread message

Philip Rhoades

unread,
Sep 20, 2014, 9:09:14 AM9/20/14
to vim_use
People,

If I have words in a file - either on the same or separate lines - is it
possible, within vim (I could probably do what I want with a shell
script and grep) to search for _any_ of the words in the set?

eg I have a file with words:

quick fox lazy dog

and I have a text file open in vim with the lines

the
quick
brown
fox
jumps
over
the
lazy
dog

- I want my search to stop on "quick" then "fox" then "lazy" then "dog"
when the search is repeated . . seems too hard to me . .

Regards,

Phil.

--
Philip Rhoades

GPO Box 3411
Sydney NSW 2001
Australia
E-mail: ph...@pricom.com.au

Tim Chase

unread,
Sep 20, 2014, 9:44:17 AM9/20/14
to vim...@googlegroups.com, ph...@pricom.com.au
On 2014-09-20 23:09, Philip Rhoades wrote:
> If I have words in a file - either on the same or separate lines -
> is it possible, within vim (I could probably do what I want with a
> shell script and grep) to search for _any_ of the words in the set?
>
> eg I have a file with words:
>
> quick fox lazy dog
>
> and I have a text file open in vim with the lines
>
> the
> quick
> brown
> fox
> jumps
> over
> the
> lazy
> dog
>
> - I want my search to stop on "quick" then "fox" then "lazy" then
> "dog" when the search is repeated . . seems too hard to me . .

You can mung your file to make it into an expression and then use
that to search:

:new " create an empty buffer
:r words.txt " read your wordlist into it
:%s/\_s\+/\\|/g " convert all whitespace including newlines
" to "\|", the "or" conjunction in a regexp
:y a " yank that into the "a" register
:q
:let @/ = '\<\%(' . @a, '\.[*') . '\)\>'

The "let" line takes the resulting combined terms, wraps it in the
enforcement that it have word boundaries so you don't find "bulldogs"
but only "dog", and assigns it to the search register. If you don't
care about word boundaries, you can just do

:let @/ = @a

Once you've populated the search register, you can quit the scratch
window:

:q!

and edit the text you want to search

:e my_document.txt

and use n/N to search forward & backward for matches.

-tim






Nicolas Dermine

unread,
Sep 20, 2014, 9:45:11 AM9/20/14
to vim...@googlegroups.com
hi Philip,

On Sat, Sep 20, 2014 at 3:09 PM, Philip Rhoades <ph...@pricom.com.au> wrote:
People,

If I have words in a file - either on the same or separate lines - is it possible, within vim (I could probably do what I want with a shell script and grep) to search for _any_ of the words in the set?

eg I have a file with words:

  quick fox lazy dog

and I have a text file open in vim with the lines

the
quick
brown
fox
jumps
over
the
lazy
dog

- I want my search to stop on "quick" then "fox" then "lazy" then "dog" when the search is repeated . . seems too hard to me . .

you could search with a regular expression, for example :

/\v(quick|fox|lazy|dog)

would work with your example (but does not handle word boundaries),

nico





 

Regards,

Phil.

--
Philip Rhoades

GPO Box 3411
Sydney NSW      2001
Australia
E-mail:  ph...@pricom.com.au


--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Philip Rhoades

unread,
Sep 20, 2014, 10:48:38 AM9/20/14
to vim...@googlegroups.com
Tim, Nico,
Wow! - thanks for the super fast response! - I will try out this stuff
now.

Philip Rhoades

unread,
Sep 20, 2014, 11:11:01 AM9/20/14
to vim...@googlegroups.com
Tim,


On 2014-09-20 23:42, Tim Chase wrote:
The simple version works fine but there is an error in the complex let
statement somewhere - I can see the search register and the "a" register
- can you explain how the stuff around the "a" register works or point
me to somewhere?

Thanks,

Tim Chase

unread,
Sep 20, 2014, 4:27:06 PM9/20/14
to vim...@googlegroups.com
On 2014-09-21 01:10, Philip Rhoades wrote:
> > :new " create an empty buffer
> > :r words.txt " read your wordlist into it
> > :%s/\_s\+/\\|/g " convert all whitespace including newlines
> > " to "\|", the "or" conjunction in a regexp
> > :y a " yank that into the "a" register
> > :q
> > :let @/ = '\<\%(' . @a, '\.[*') . '\)\>'
>
> The simple version works fine but there is an error in the complex
> let statement somewhere - I can see the search register and the "a"
> register
> - can you explain how the stuff around the "a" register works or
> point me to somewhere?

The goal is to have a search expression that would look something like

\<\%(quick\|fox\|lazy\|dog\)\>

The \< and \> enforce word start/end boundaries, and the \%(...\)
should create a group around the various alternatives that are
separated by "\|". If it helps, instead of joining them, you can
type the "/" to search, type the "\<\%(" and then use control+R
followed by "a" to paste in the contents you yanked into the "a"
register, then do any cleanup, finally add on the "\)\>" at the end.

-tim


Philip Rhoades

unread,
Sep 21, 2014, 12:10:14 PM9/21/14
to vim...@googlegroups.com, v...@tim.thechases.com
Tim,


On 2014-09-21 06:24, Tim Chase wrote:
> On 2014-09-21 01:10, Philip Rhoades wrote:
>
>> > :new " create an empty buffer > :r words.txt " read your wordlist into it > :%s/\_s\+/\\|/g " convert all whitespace including newlines > " to "\|", the "or" conjunction in a regexp > :y a " yank that into the "a" register > :q > :let @/ = '\<\%(' . @a, '\.[*') . '\)\>' The simple version works fine but there is an error in the complex let statement somewhere - I can see the search register and the "a" register - can you explain how the stuff around the "a" register works or point me to somewhere?
> ... [show rest of quote]
>
> The goal is to have a search expression that would look something like
>
> \<\%(quick\|fox\|lazy\|dog\)\>
>
> The \< and \> enforce word start/end boundaries,


Right.


> and the \%(...\)
> should create a group around the various alternatives that are
> separated by "\|". If it helps, instead of joining them, you can type
> the "/" to search, type the "\<\%(" and then use control+R followed by
> "a" to paste in the contents you yanked into the "a" register, then do
> any cleanup, finally add on the "\)\>" at the end.


OK, I can get that manual method to work, but I still want to know what
was wrong with the original complex let statement:

:let @/ = '\<\%(' . @a, '\.[*') . '\)\>'

- this works:

:let @/ = '\<\%(' . @a . '\)\>'

- what was the extra:

", '\.[*')"

supposed to do?

Thanks,

Tim Chase

unread,
Sep 21, 2014, 1:52:56 PM9/21/14
to ph...@pricom.com.au, vim...@googlegroups.com
On 2014-09-22 02:10, Philip Rhoades wrote:
> OK, I can get that manual method to work, but I still want to know
> what was wrong with the original complex let statement:
>
> :let @/ = '\<\%(' . @a, '\.[*') . '\)\>'
>
> - this works:
>
> :let @/ = '\<\%(' . @a . '\)\>'
>
> - what was the extra:
>
> ", '\.[*')"
>
> supposed to do?

Doh! I see...I'd originally used the escape() function but realized
I was escaping the "\" characters that I still needed. So I removed
the function opening but accidentally failed to remove the closing
second-half of the call. So sorry for the confusion.

-tim




Reply all
Reply to author
Forward
0 new messages