Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

While editing C or C++ files in emacs, I'd like to be able to go to the beginning of the containing block of code. I was expecting to find a c-beginning-of-block function, but alas, no such function exists (to my knowledge). For example, I might be editing the following ugly C code:

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

I would be very surprised if this isn't already part of emacs, I just can't find it anywhere...

fortran-mode has fortran-beginning-of-block

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

share|improve this question
up vote 8 down vote accepted

Try backward-up-list, bound by default to C-M-u.

share|improve this answer
That's it! Thanks! – mgalgs Mar 23 '11 at 0:10
and C-M-n (forward-list) jumps to the end of block – Francois Jun 25 '12 at 16:13
To keep jumping within the block from beginning to end, this does not solve the purpose because when I try to move to end of block C-M-n takes me to end of ) of a function call. – dknight Feb 24 '15 at 6:07
There are four related commands: C-M-n (next) and C-M-p (previous), which take you backwards and forwards over whole parenthesis blocks, and C-M-u (up) and C-M-d (down) which take you one level up or down the nesting of parentheses. (see docs) So to go to the end of the current block, you need C-M-u C-M-n – rbennett485 May 19 at 13:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.