3

I would like to navigate quickly between classes in Python code. I'm looking for a command to go to the next / previous class so I can bind that to a key sequence. This question is not about graphical navigation tools like treemacs.

It might be interesting to hear about things like hydras too, but that's also not the focus of this question.

3 Answers 3

4

What I found so far and use to navigate between classes in python-mode:

First C-M-u which runs the command python-nav-backward-up-list to navigate up to class definition. After that C-M-a (beginning-of-defun) and C-M-e (end-of-defun) to navigate to previous and next class definition respectively.

1

Based on @muffinmad's answer:

(defun my/python-navigate-up-to-class-statement ()
  (interactive)
  (let ((pos nil))
    (while (not (equal pos (point)))
      (setf pos (point))
      (python-nav-backward-up-list))))

(defun my/python-navigate-to-next-python-class ()
  (interactive)
  (my/python-navigate-up-to-class-statement)
  (end-of-defun)
  (end-of-defun)
  (my/python-navigate-up-to-class-statement))

(defun my/python-navigate-to-previous-python-class ()
  (interactive)
  (my/python-navigate-up-to-class-statement)
  (beginning-of-defun))
1

With Elpy

C-<up> and C-<down> jump from class to class. Or more accurately: "These commands are used to navigate between lines with same indentation as the current line." Use C-a or Home first, if you're currently on an indented line.

Note: M-a and M-e jump from block to block.


C-c C-o "Search the buffer for a list of definitions of classes and functions."

1
  • Thanks. If you know the commands that are bound to those keys for you (e.g. C-h k C-<up> RET) that often helps people who use a different config (e.g. doom, spacemacs) Sep 18, 2022 at 12:44

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.