emacs -- C/C++ 跳转到包含块的开头

mga*_*lgs 12 emacs c c++

在 emacs 中编辑 C 或 C++ 文件时,我希望能够转到包含代码块的开头。我期待找到一个 c-beginning-of-block 函数,但唉,不存在这样的函数(据我所知)。例如,我可能正在编辑以下丑陋的 C 代码:

void myFunction()
{
  if (something) { //<--- I want to jump to this brace!
    // do lots of stuff
    if (stuff) {
      // stuff
    }
    // more stuff
    // ...

    // I want to put my cursor somewhere on this line <---
    // (anywhere just outside the following if) and call c-beginning-of-block
    // and jump to the brace marked above (skipping "sibling" statements)
    if (pizza_is_good) {
      // do something
      // wait, where am I?
    }
    // way more stuff
    // ...
    if (i_love_pizza) {
      // eat pizza
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果这还不是 emacs 的一部分,我会感到非常惊讶,我只是在任何地方都找不到它......

fortran 模式有 fortran-beginning-of-block

promela-mode 有 promela-find-start-of-containing-block

小智 15

尝试backward-up-list,默认绑定到C-M-u.

  • CMn(前向列表)跳转到块的末尾 (3认同)
  • 有四个相关命令:CMn(下一个)和 CMp(上一个),它们使您在整个括号块上前后移动;CMu(向上)和 CMd(向下),使您在括号嵌套中向上或向下一级。(参见[docs](https://www.gnu.org/software/emacs/manual/html_node/emacs/Moving-by-P​​arens.html))因此要转到当前块的末尾,您需要 CMu CMn (2认同)