Friday, January 8, 2010

Re: Is there a way to predefine search patterns?

Hi Tim!

On Fr, 08 Jan 2010, Tim Chase wrote:

> 2) you can assign to the search register, so you can store patterns
> however you like (say, in variables) and then assign+search for them as
> you need. Untested but should be right except possibly for "\" escaping
> levels:
>
> :let ip_pattern='\([0-9]\{1,3}\.\)\{3}[0-9]\{1,3}'
> :let other_pattern='...'
> :let @/=ip_pattern
> n
>
> That's more helpful for scripts, but not as useful for general use.

To expand that a little bit, here is a script, that can makes Search
templates available.

,----
| " Define search patterns:
| " Variables should start with 'Search_'
|
| let Search_ip='\d\d\d\.\d\d\d\.\d\d\d.\d\d\d'
| let Search_foo='foo'
| let Search_blah='\\\\'
|
| fu! <SID>Search(A,C,P)
| return map(filter(keys(g:), 'v:val=~"^Search_"'), 'matchstr(v:val, "Search_\\zs.*")')
| endfu
|
| com! -nargs=1 -complete=customlist,<SID>Search S :let @/=Search_<args>
`----

You can then use :S and using <Tab> can complete through all your
variables.

regards,
Christian

No comments: