I asked this question, about how to find where one word is close to another word, and Quasimodo gave the very fine answer:
/\vaid(\W+\w+){,3}\W+country
Now I want to parametrize this command into something easy-to-remember, such as:
:PS first 10 derivative
which would find instances of the word "first" occurring within 10 words of the word "derivative". User B Layer came up with the code
com! -nargs=+ PS let @/ = printf('\v%s(\W+\w+){,%d}\W+%s', <f-args>)
I have a gut feeling that this is very close to what's needed, but this doesn't work. When I try to execute this, nothing happens, even when I know from executing
/\vfirst(\W+\w+){,10}\W+derivative
that there should be hits. I also tried defining
com! -nargs=+ PS let @/ = printf('/\v%s(\W+\w+){,%d}\W+%s', <f-args>)
but no joy. Any ideas what's wrong with this command definition?
One thought: it looks like we're defining a macro; can you use arguments with macros like that?
Another thought: am I invoking the command incorrectly? Should I just define the macro and invoke it in a macro-like way?
I don't know what I don't know, so any help is appreciated.
[EDIT] I just found out that B Layer's command allows me to define the macro as I go. Then, to do the actual searching, I invoke the macro in the normal way: @a
, if I've done
com! -nargs=+ PS let @a = printf('/\v%s(\W+\w+){,%d}\W+%s', <f-args>)
This works, but I'd prefer a one-step process, if possible. Just invoking
:PS first 10 derivative
actually does the search, instead of defining a macro that I invoke in a separate step.
Many thanks!