17

Sometimes I'm inside a long function where the start of the function is not visible. While I have recently learned about (which-function-mode) which tells me in the modeline the name of the function, I'd like to be able to quickly jump to the start of the function and then back as easily as possible.

So far I've found two solutions.

  1. Using a mark

    C-SPC Mark current point

    C-M-a Move backward to start of function

    C-x C-x Exchange mark with current point (jumps you back)

    C-g Cancel marked region

  2. Using a register

    C-x r SPC p Save current point in register 'p'

    C-M-a Move backward to start of function

    C-x r j p Jump to point saved in register 'p'

Is there a better way? I'm not opposed to adding some elisp to my .emacs file.

1 Answer 1

20

This works for me:

  • C-M-a to move to the beginning of the function; this sets mark automatically
  • C-u C-SPC to pop the mark and return to the previous position
3
  • 2
    Nice! I had no idea that it set the mark for me, and I didn't know about the prefix modifier for set-mark-command
    – Harvey
    Oct 24, 2014 at 13:24
  • 11
    Once you get used to using pop-to-mark-command (which is what is called by C-u C-SPC), you will wonder how you lived without it. You can find your way back where you came from by following a little trail of breadcrumbs that you didn't even know you were leaving.
    – nispio
    Oct 24, 2014 at 14:30
  • Indeed. It's typical for Emacs commands to push to the mark ring if they are about to move you some arbitrary distance from where you were, so users should remember this approach in general.
    – phils
    Sep 13, 2016 at 3:06

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.