Search code examples
visual-studio-codevimvscodevim

How to move cursor to a C++ function(method)'s name from its body in VSCode with VIM extension


I'm developing some C++ code with VSCode+VIM extension. From time to time I need to do this while reading code: say I'm inside a long function and I want to know who called it. The first step to do is to move cursor directly under function's name so that I can invoke some keystrokes to show references.

What I'm current using is to press "[" key twice which will bring me to the opening bracket of the function. Since I have to follow some coding standard, the typical scenario is like this:

ReturnType ClassName::FunctionName(
    ParamType1 param1,
    ParamType2 param2,
    ParamType3 param3)
{ // <-- Cursor here
    ......

}

Then I need to press "k" several times to move cursor under "ReturnType", depending on how many parameters are there. Next, I still have to press "w" 3 times to eventually move cursor from "ReturnType", to "FunctionName".

As you can see, this is a little painful here. I've tried easy motion approach with VSCode VIM extension, this makes my life a little easier, but I'm looking for a even better one.

Any VIM trick or VSCode extension can do this decently? Any help will be appreciated, thanks!


Solution

  • To avoid having to press k a variable number of times it's possible to make use of the fact that the ) is right on the line above, and use % to go to the matching (. The complete key sequence is [[b%b.

    However the first b will go to the ( if there's nothing between the parentheses. [[ge%b can be used instead.

    If there's something between ) and { (such as a const qualifier) [[?)<cr>%b would work (this solution is complex and perhaps only useful in a key binding?)

    [[?(<cr>b works too as long as there's no parameter that contains an open parentheses (such as in FunctionName(int (*function_pointer)(int, int)) { ... })