6

I do not want an answer about something like Imenu or tags - features that suppose you know some keywords about the method/function name to move to.

What I want is just general commands which jump forward to next or jumps backward to previous method, e.g. in ruby-mode or emacs-lisp-mode.

1 Answer 1

12

Use C-M-a (beginning-of-defun) to move backward before each "defun" (top-level sexp).

Use C-M-e (end-of-defun) to move forward after each "defun".

To move across several by repeating, just hold down Control, Alt (Meta), and a or e.

To move forward/backward N defuns, use a numeric prefix arg. E.g. C-u 1 0 C-M-e moves forward 10 defuns.

(There are several pairs of commands that end in a or e, for moving to the beginning or end of things. The most commonly used are probably C-a and C-e.)

6
  • Is there a mnemonic that makes this memorable? e for end (-of-defun) makes sense, but then a doesn't associate with beginning. They're not adjacent on the keyboard either, or even in line with each other, in order to have a forward/back or up/down association with each other. :(
    – Ben
    Commented Mar 4, 2022 at 21:26
  • 1
    @Ben: In Emacs a and e often stand for beginning and end, respectively. Not sure what mnemonic might be helpful for thinking of a. But at least it's conventional in Emacs. See also b and f (back, forward) and p and n (previous, next).
    – Drew
    Commented Mar 5, 2022 at 16:58
  • Ah thanks, I hadn't noticed that pattern yet.
    – Ben
    Commented Mar 5, 2022 at 20:48
  • @Drew @Ben a is the first character in the alphabet :) It's also used e.g. in regular expressions where \A matches at the start of the subject (with \z matching at the end but let's not go there) Commented Sep 8, 2023 at 14:35
  • The Unix command line uses a and e as the default start and end. Actually, many C- commands in Emacs are also usable in the Unix command line.
    – Ness
    Commented Oct 15 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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