Very often, I am stuck searching for the end of loops and methods and classes and end up sifting through all the code which is both time-consuming and frustrating. I would rather skip to go to the next line with same indentation.

Is there any shortcut in emacs to do so?


A little improvement on Stefan's answer:

(defun jump-to-same-indent (direction)
  (interactive "P")
  (let ((start-indent (current-indentation)))
      (and (not (bobp))
           (zerop (forward-line (or direction 1)))
           (or (= (current-indentation) 0)
           (> (current-indentation) start-indent)))))

This function takes a prefix argument (e.g., +1/-1) that designates the number of lines to move over when searching for a line with the same indentation. It also skips empty lines. Finally one can bind both forward and backward searches using keybindings similar to M-{ and M-} for paragraphs:

(global-set-key [?\C-{] #'(lambda () (interactive) (jump-to-same-indent -1)))
(global-set-key [?\C-}] 'jump-to-same-indent)

I don't know of any, but something like

(defun jump-to-next-same-indent ()
  (let ((start-indent (current-indentation)))
      (and (not (bobp))
           (zerop (forward-line 1))
           (> (current-indentation) start-indent))))

should work, which you could bind for example to M-p with

(global-set-key [?\M-p] #'jump-to-next-same-indent)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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