In Vi, if I am in the middle of a function, how can I navigate back to the function declaration?


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).

| improve this answer | |

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

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.

| improve this answer | |

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.