Friday, January 22, 2010

Re: search from inside a function

meino.cramer@gmx.de wrote:
> Inside a funtion definition I want to execute a search
> with a fixed search pattern including regexp portions.
>
> When I want to execute a command from inside a function
> definition I would do a
> :<command>
> and the ':' would switch to command mode.
>
> But
> /<string>
> inside a function definition looks more like the start
> of pattern as such and not like the instruction to
> search something to me...

It ("/") is a valid Ex command:

function! FindFoo()
/foo
endfunction

which you can read briefly about at

:help :/

and with no real help-anchor:

:help :bar
or
:help cmdline.txt

where you'll have to search for this bit of help:

Because of Vi compatibility the following
strange commands are supported:
:| print current line (like ":p")
:3| print line 3 (like ":3p")
:3 goto line 3

most importantly the "goto line 3" applies to any
range-specifier, not just a fixed line-number. I'm not sure how
I stumbled across this, except perhaps remembering how it worked
in "ed" and just trying it.

Anyways, that means you can validly do things like

function! Find2ndLineAfterFoo()
/foo/+2
endfunction

At least that's my preferred way. One could also reach for
something like

call search(...)

but my preference is just my old-fogey nature showing through :)

Hope this helps,

-tim


--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: