从函数中间跳转到当前函数声明

vin*_*ent 1 vim

有没有办法跳转到我的光标当前所在的函数的签名,然后跳回我所在的位置?

例如,当我有一个 1000 行函数时,其中前缀x + y:指的是行号,有没有办法从我的光标位置 at 跳到x + 555签名 atx + 0然后回到我所在的位置 ( x + 555):

x + 000: void theFn(int arg) {
x + ...:     ...
x + 555:     /// where my cursor starts
x + ...:     ...
x + 999: }
Run Code Online (Sandbox Code Playgroud)

是的,我非常同意你的观点,不应该有 1000 行函数。

另外,有没有一种方法可以自动跳转到函数末尾,而不需要在函数的左括号处?

Jor*_*nar 5

在这种情况下有用的动议是[[][<C-o>

正如我们可以在帮助中读到的:

                            *[[*
[[          [count] sections backward or to the previous '{' in
            the first column.  |exclusive|
            Note that |exclusive-linewise| often applies.
Run Code Online (Sandbox Code Playgroud)
                            *][*
][          [count] sections forward or to the next '}' in the
            first column.  |exclusive|
            Note that |exclusive-linewise| often applies.
Run Code Online (Sandbox Code Playgroud)
                            *CTRL-O*
CTRL-O          Go to [count] Older cursor position in jump list
            (not a motion command).
            {not available without the |+jumplist| feature}
Run Code Online (Sandbox Code Playgroud)

简而言之:

  • [[开始
  • <C-o>返回到之前的位置
  • ][走到尽头

仅当大括号位于第一列时,这些动作才会产生所需的效果,但从您的示例来看,似乎未满足此要求。在这种情况下,我们可以
在末尾读到::h section

If your '{' or '}' are not in the first column, and you would like to use "[["
and "]]" anyway, try these mappings: >
   :map [[ ?{<CR>w99[{
   :map ][ /}<CR>b99]}
   :map ]] j0[[%/{<CR>
   :map [] k$][%?}<CR>
Run Code Online (Sandbox Code Playgroud)

不幸的是,Vim 没有提供更好的解决方案,因为它不解析语法。
随着 Neovim 与Tree-sitter 的实验,它可能会改变。


如果有一个插件可以为此类运动提供更好的支持,这也不足为奇。
Tagbar适合这个角色:

  1. 切换标签栏窗口
  2. 切换到它
  3. 光标应该已经位于当前标签上
  4. 按回车键
  5. 切换窗口
  6. 您正处于函数的开头
  7. 用于<C-o>返回

我也曾经在我的配置中发现并拥有一个映射,在这种情况下也很有用:

nnoremap <Leader>gd ?\v%(%(if|while|for|switch)\_s*)@<!\([^)]*\)\_[^;(){}]*\zs\{
Run Code Online (Sandbox Code Playgroud)