Reputation: 2095
I'm new to vim and wanted to know how I can list all functions inside the current file within vim.
Upvotes: 14
Views: 17444
Reputation: 196751
How can I list anything in the current file in vim?
The most basic command for listing all lines matching a pattern is :help :global
:
:g/foo " if you have line numbers enabled
:g/foo/# " if you don't
So, to list functions (declared with the keyword function
) you would do something like :g/^func
:
and then :78<CR>
to jump to qf#GetList()
.
Upvotes: 21
Reputation: 199
A little late to the party, stumbled across this looking for something else... But to get a populated location list that you can "scroll" up and down on, as OP, asked in a comment to an answer:
function! Matches(pat)
let buffer=bufnr("") "current buffer number
let b:lines=[]
execute ":%g/" . a:pat . "/let b:lines+=[{'bufnr':" . 'buffer' . ", 'lnum':" . "line('.')" . ", 'text': escape(getline('.'),'\"')}]"
call setloclist(0, [], ' ', {'items': b:lines})
lopen
endfunction
call Matches('^\(\s*\)\=function!\=.*(.*)\( abort\)\=$')
So the Matches function takes a regex and searches for it, then populates a location list and opens it.
So...
call Matches(@/)
would then just populate a list for the last search.
The regex example above takes into account:
function name()
function! name()
function name() abort
function! name() abort
I've probably missed some combinations but give it ago.
Upvotes: 2
Reputation: 21
Try this: I mapped to Ctrl-f (list functions) but you can map to any key you like Press Ctrl-f will list the functions. Then issue ":"line_no
Upvotes: 2
Reputation: 172648
Remember that Vim is a (powerful) text editor, not an IDE. So, the basics start with simple text searches for a pattern that matches all function definitions (depending on your programming language), e.g. done with :global/^def/print
. If the syntax performs folding of functions (many syntaxes do, or can be configured to), you can close all folds to get this overview.
Then, Vim integrates with :help tags
. There are plugins that analyze the current file and display all functions, properties, etc. in a sidebar; this is already quite similar to what IDEs offer:
Upvotes: 0