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

Jump to current function declaration from middle of function

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

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

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

而且,是的,我完全同意你不应该有 1000 行函数。

另外,有没有办法在函数的左括号处自动跳转到函数的末尾?

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

我们可以在帮助中看到:

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

简而言之:

  • [[ 开始了
  • <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>

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

的实验而改变

如果有一个插件可以更好地支持这种运动,也就不足为奇了。
Tagbar适合这个角色:

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

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

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