In Vi, if I am in the middle of a function, how can I navigate back to the function declaration?
2 Answers
I'd typically use {, which gets me to the beginning of the paragraph (which is often the beginning of the function too) in one keystroke, or sometimes I might have to use { two or more times, depending on how many blank lines I have in the function.
If I'm using C and ctags, then :tag functionname will get me to the function declaration from anywhere (Vim Book, p.79).
- 621
- 5
- 12
At the risk of stating the obvious, this may depend on the language and the coding style. If the code is in C or C++ and the programmer followed the convention of beginning functions as
int
sum(int num1, int num2)
{
(or even with the return type on the same line as the function name), and then indenting all internal blocks, you could do a ?^{ search. Alternatively (and this may be the best answer), [[ seems to be a shortcut for ?^{. (]] seems to be a shortcut for /^{ –– no, not /^}.)
Even if the convention is
int sum(int num1, int num2) {
(consistently) you could search for ?^[a-zA-Z].*{$. For that matter, ?^[a-zA-Z] might be good enough. Other languages, other coding conventions might require different answers.
- 21,717