I am using beginning-of-buffer to the jump beginning of a buffer.

Is it possible to achive same behavior but to the first line under the import or from` section in the Python mode.

In this example I want to jump to 17th line. There could be multiple modules under from ... import ( and there could be also a new-line in between from ... and import... lines.

1:  #!/usr/bin/env python3
3:  import os
4:  import sys
5:  import textwrap
6:  import time
7:  from broker.utils.tools import log
9:  import zc.lockfile
10: from ipdb import launch_ipdb_on_exception
12: from broker.utils import (
13:     CacheType,
14:     StorageID,
15: )
17: # <== jump here
19: def _main():
20:     from my_module.utils.tools import work
22:     work()
  • What have you tried so far?
    – Dan
    Commented Oct 26, 2021 at 15:51
  • I have not tried anything due to my lack of knowledge in lisp coding.
    – alper
    Commented Oct 26, 2021 at 16:10

1 Answer 1


I am not aware of some existing function that does this. But there are many simple ways to achieve this; two commands you could use are:

EDIT in response to your first comment

So I guess after finding the last "import", you could just continue to look for the first empty line.

SECOND EDIT in response to your second comment

To exclude 'searching for/from' "import"s in 'the body' of the file, we can just limit the range for the search to "import", e.g. as follows:

(defun my-alternative-jump-to-end-of-imports ()
  (goto-char (point-min))
  (let (end)
      (forward-line 3)
      (setq end (point)))
    (while (search-forward "import" end t)
        (forward-line 3)
        (setq end (point))))
    (while (and (not (eobp))
                (not (member (thing-at-point 'line t) '("" "\n"))))
    (if (bolp)
      (insert "\n\n"))))

You can play a little with the number of lines for searching forward. It is not perfect, but I guess it works fine for 99% of the cases.


I would suggest that you take some time to read a few chapters of An Introduction to Programming in Emacs Lisp (or read it right inside of Emacs) and study the above functions; it is not so much work, and Emacs becomes so much more fun to use.

  • Thank you, I will study the referenced book. If there is new-line in between the from and import lines it jumps into first new-line location. Would it be possible to pass over those new-lines as well? I updated my question based on that. Seems like complexity of the question increased :(
    – alper
    Commented Oct 27, 2021 at 10:35
  • I've edited the answer in response to your comment Commented Oct 27, 2021 at 12:05
  • Ah if there is something like from my_module.imports import connect at the bottom of page inside a module(def main(): ) it jumps below that instead of the top modules (I have updated my question according to that as well). I will find way to ignore that , other than it works great
    – alper
    Commented Oct 27, 2021 at 12:30
  • Okay... I've updated the answer Commented Oct 27, 2021 at 14:21
  • 1
    Thank you, I appreciated your patient help
    – alper
    Commented Oct 27, 2021 at 14:45

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.