views:

814

answers:

3

Is there a Vim shortcut for jumping to the argument list of the current function? I often find myself needing to mess with the argument list of a function, and it's kind of annoying to have to do ?def or ?function or 10k or what-have-you until I finally get to it, then /( or t( or 5e to get to the right position in the argument list, and so on. It would be great if I could just hit ,a for example and instantly get put into insert mode at the end/beginning of the argument list.

Possible approaches:

  • Folding
  • Tag support (ctags)

Also, I'm using Python, so solutions based on curly braces unfortunately won't work.

If no such shortcut exists, I'll just write one and post it here as an answer. :-)

+1  A: 

map ,a ma[{F(a

Hit ,a to go to the argument list, then `a to return to where you were when you invoked ,a. Caveat: [{ jumps back to the last unmatched { character, so if you're inside a loop or other control structure, it will take you to the beginning of that, instead.

I don't know of a way to get to the beginning of the function in a fool-proof way. If you're consistent about your tabbing, you may be able to do something like this:

map ,a ma?function :nohlf(a

where, if you don't use a single tab before you define your functions, you'd change to appropriate value.

Lucas Oman
I'm using Python, so unfortunately the { solution won't work. Sorry--I should have been more specific in the first place.The second solution is interesting, but would get caught by inline method declarations.
Max
+3  A: 

Disclaimer, I don't know Python, I assume a Python function can be identified by "function" or "def" from your question. Just change the regex in consequence.

May be something like:

   :nnoremap <buffer> [m :call search('def\|function', 'b')<cr>f(

?

NB:

  • I have used search() in order to not mess up the search history ; searchpair() may be a better choice as it will only jump to the definition of the function we are within, instead of the previous function.
  • As this is intended to work with Python only, I use <buffer> in order to not mess up the key-binding in non-Python files; this mapping is best defined in a python ftplugin.

HTH,

Luc Hermitte
This is good, but there would be issues if any inline functions were declared between the current line and the prototype. I wonder if there's a good way to guard against that... searchpair() is definitely worth looking into.
Max
@Max - well, maybe modify the regex to be '^\s*\%(def\|function\)', since the current function shouldn't have anything before it on the line, right, what with Python being all whitespacy and whatnot, right?IAANAPD (i am also not a python developer)
rampion
+1  A: 

The fool proof way of getting to the begining of a function is to use [[. So you use

map ,a ma[[kf(a

so it can take you to the function definition, search for the first occurance of "(" and then put you in the insert mode.

AFAIK `[[` only takes you to function/classes with no white spaces before them (not the case in python), but `[m` works. Also, why is there a `k` after [[? It puts the cursor a line above the function.
catchmeifyoutry