Vim provides very useful motion commands to jump to next start/end of a method: ]m, ]M, [m and ]m.
These works for Java or similar structured language. (as described in :help ]m and :help 29.3)
It seems to work considering the outermost pair of curly braces as class declaration and the next level of curly braces as method declarations.
These motion commands doesn't work when there is an outer pair of curly braces around class definition, which is somewhat common on languages as C#.
I was wondering if there is some trick to make those commands (alone and prefixed with operators, e.g., y[m, V]M) work on this code:
namespace ABC.DEF
{
class A
{
protected string strID;
public string PortID { get { return strID; } set { strID = value; } }
protected MyType x;
public MyType X
{
get { return x; }
set { x = value; if ( x != null ) func1(); }
}
int func1()
{
return 1;
}
int func2(int flag)
{
if (flag == 0)
return flag;
if (flag > 3)
{
return flag;
}
return 2;
}
int func3()
{
return 3;
}
}
}
Motions (as in movements) are how you move around in Vim. They are commands that when typed move the cursor around with high speed and precision. There are many of them, and each one is best suited for different types and lengths of movement.
Mappings to move lines In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up.
:g/pattern/cmd :range g/pattern/cmd. The g command first scans all lines in range and marks those that match pattern . It then iterates over the marked lines and executes cmd . (See multi-repeat in vim's documentation).
Andrew Radev gave an answer which idbrii turned into a plugin.
I have modified that plugin here: https://github.com/RobertCWebb/vim-jumpmethod/
I fixed various issues and added improved mappings for [[ and ]] as well
Regarding the latter, I found it's more useful to stop at class definitions and properties too.
Properties in C# can contain a get and a set method, but I decided it was probably sufficient to just stop once at the definition of the property itself.
For classes, I noticed that I would jump from a method in one function to a method in another, without even realising I was in a different class. That seems dangerous to me, so it's nice to stop at the class definition too. The [[ and ]] mappings will do this.
A few weeks ago, a similar question has been asked on vim mailing list, but for C++. Here is the solution I came up with.
It relies on ctags and a few other plugins of mine: lh-dev, lh-tag, and lh-vim-lib. You can install lh-dev via vim-addon-manager, this will install lh-tag and lh-vim-lib in turn.
HTH,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With