Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6184201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:29:45+00:00 2026-05-24T01:29:45+00:00

Vim provides very useful motion commands to jump to next start/end of a method:

  • 0

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;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:29:46+00:00Added an answer on May 24, 2026 at 1:29 am

    I don’t think the ]m family of mappings can be customized. In such cases, the usual practice is to override it with custom logic. I came up with some vimscript that should do what you describe. Basically, it jumps through curly braces and looks at the relevant line to decide what to do. In this case, it just ignores “class” and “namespace” declarations.

    nnoremap <buffer> ]m :<c-u>call <SID>JumpMethod('{', 'W',  'n')<cr>
    nnoremap <buffer> [m :<c-u>call <SID>JumpMethod('{', 'Wb', 'n')<cr>
    nnoremap <buffer> ]M :<c-u>call <SID>JumpMethod('}', 'W',  'n')<cr>
    nnoremap <buffer> [M :<c-u>call <SID>JumpMethod('}', 'Wb', 'n')<cr>
    
    xnoremap <buffer> ]m :<c-u>call <SID>JumpMethod('{', 'W',  'v')<cr>
    xnoremap <buffer> [m :<c-u>call <SID>JumpMethod('{', 'Wb', 'v')<cr>
    xnoremap <buffer> ]M :<c-u>call <SID>JumpMethod('}', 'W',  'v')<cr>
    xnoremap <buffer> [M :<c-u>call <SID>JumpMethod('}', 'Wb', 'v')<cr>
    
    onoremap <buffer> ]m :<c-u>call <SID>JumpMethod('{', 'W',  'o')<cr>
    onoremap <buffer> [m :<c-u>call <SID>JumpMethod('{', 'Wb', 'o')<cr>
    onoremap <buffer> ]M :<c-u>call <SID>JumpMethod('}', 'W',  'o')<cr>
    onoremap <buffer> [M :<c-u>call <SID>JumpMethod('}', 'Wb', 'o')<cr>
    
    function! s:JumpMethod(char, flags, mode)
      let original_cursor = getpos('.')
    
      if a:mode == 'v'
        normal! gv
      elseif a:mode == 'o'
        normal! v
      endif
    
      while search(a:char, a:flags) > 0
        if a:char == '}'
          " jump to the opening one to analyze the definition
          normal! %
        endif
    
        let current_line = line('.')
    
        if getline(current_line) =~ '^\s*{'
          " it's alone on the line, check the above one
          let method_line = current_line - 1
        else
          let method_line = current_line
        endif
    
        let method_line_body = getline(method_line)
    
        if method_line_body =~ '\k\+\s*(.*)' && method_line_body !~ '\<\(for\|foreach\|if\|while\|switch\|using\|catch\|get\|set\)\>'
          " it's probably a function call
    
          if a:char == '}'
            " we need to go back to the closing bracket
            normal! %
          endif
    
          echo
          return
        else
          if a:char == '}'
            " we still need to go back to the closing bracket
            normal! %
          endif
        endif
      endwhile
    
      " if we're here, the search has failed, restore cursor position
      echo
      call setpos('.', original_cursor)
    endfunction
    

    Bear in mind that I don’t really know a lot of C#, so it might not work properly in all cases, but if you give me examples that break, I might be able to figure something out.

    To try it, you should put it somewhere under “ftplugin” in your vimfiles directory, as “cs.vim”. Any other filename that starts with “cs” and ends in “.vim” is good too, if you already have a “cs.vim” file there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tim Pope's rails.vim provides a command :A (and a set of related commands) which
Vim's find and replace is very powerful and very useful however sometimes I don't
Vim is indenting method definitions an extra level after an attribute. This is driving
vim has very powerful command tools. I can substitute 'Set's for all 'Array's: :%s/Array/Set/g
Vim is very accommodating when it comes to tab Vs. space preferences. As I
I would like to make vim my C++ editor. I have very little experience
Vim has templates and idioms programming and other similar stuff. For example, the c.vim
I'm starting a Java programming class at UCSD next week and I'm trying to
Vim is very productive editor and I enjoy using it everyday, but I've found
I have started to do some programming using VIM. I have very mixed feelings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.