Saturday, September 20, 2014

Re: Searching for any one of a set or words ?

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






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

No comments: