4

I have two files written in php, both with a bunch of functions. I need to compare these function names with the other file, so I'm trying to get a list of all lines that begin with function. Preferably I would like to have these lines copy to a register. Is there an easy way to do this?

9

This can be done pretty easily with the global command. I would try this:

:let @a=''
:g/^function/y A

First we must call :let @a='' to make sure that the 'a' register is empty.

Then, the global command is used to apply a command to every line matching a certain regex. In this case, the regex is ^function, and the command is y A (the ex command, not the normal mode command). From :h :y

                            *:y* *:yank* *E850*
:[range]y[ank] [x]  Yank [range] lines [into register x]. Yanking to the
            "* or "+ registers is possible only when the
            |+clipboard| feature is included.

Yanking into uppercase A as opposed to lowercase a means that the yanked text is appended to the register.

3

You could also use vimgrep. It wouldn't put them in a register but you'd get this list pretty easily.

:vimgrep /^function/ ./*.php
:cw

:cw Opens the quick fix so you can see the result of your search.

See :help :vimgrep and :help quickfix for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.