Sunday, July 26, 2020

Re: How can I find where one word is close to another word?

On 2020-07-25, Adrian Keister <ackbeet@gmail.com> wrote:
>
>
> As most people on this forum are likely aware, searching for a word is
> straight-forward enough. If I want to find occurrences of the word 'arc', I
> can type / arc , with spaces around the three letters, to find basic
> occurrences of the word (ignoring punctuation). But suppose I want to find
> where two words are close together, say, within ten words? Here's an
> example:
>
> Now is the time for all good men to come to the aid of their country.
>
> In this test, I want to find all occurrences where the word 'aid' is within
> ten words of the word 'country'. How can I do that in Vim?

Another idea, based on \%V (see `:help %V`):

1. Search for some text, e.g.:

/country<Enter>

2. Move back the desired amount, e.g., 10 WORDS:

10B

3. Visually select the range to be searched, e.g. 20 WORDS:

v20W

4. Return to Normal mode and search within the previously selected range:

<Esc>/\%Vaid

5. To search for the next match, move forward (10W) and repeat.

Putting all that in a couple of macros, you could do:

qa/country<Enter>10Bv20W<Esc>/\%Vaid<Enter>q
qb10W@aq

Now you may type @b repeatedly to perform your search.

Hope this helps,
Life.

--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/rfjeu5%24179l%241%40ciao.gmane.io.

No comments:

Post a Comment