Emacs : Jump to the next line with same indentation
Solution 1:
A little improvement on Stefan's answer:
(defun jump-to-same-indent (direction)
(interactive "P")
(let ((start-indent (current-indentation)))
(while
(and (not (bobp))
(zerop (forward-line (or direction 1)))
(or (= (current-indentation) 0)
(> (current-indentation) start-indent)))))
(back-to-indentation))
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)
Solution 2:
I don't know of any, but something like
(defun jump-to-next-same-indent ()
(interactive)
(let ((start-indent (current-indentation)))
(while
(and (not (bobp))
(zerop (forward-line 1))
(> (current-indentation) start-indent))))
(back-to-indentation))
should work, which you could bind for example to M-p with
(global-set-key [?\M-p] #'jump-to-next-same-indent)