How to jump to the beginning of the current function body in Vim?

前端 未结 7 1552
抹茶落季
抹茶落季 2021-02-02 05:14

As title, if I\'m in the middle of function body and the function body is very long, how can I jump back to the beginning of the function body .

7条回答
  •  礼貌的吻别
    2021-02-02 05:58

    I spent hours to make this pattern: /^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{, it works good for me.

    EDIT: a better pattern(version 2): /\(\(if\|for\|while\|switch\|catch\)\_s*\)\@64

    see the effect here:

    you can map some convenient bindings in your .vimrc, such as:

    " jump to the previous function
    nnoremap  [f :call search('^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{', "bw")
    " jump to the next function
    nnoremap  ]f :call search('^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{', "w")
    

    EDIT: a better pattern(version 2):

    " jump to the previous function
    nnoremap  [f :call
    \ search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64
    " jump to the next function
    nnoremap  ]f :call
    \ search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64
    

提交回复
热议问题