Emacs python translate the start or end of a for loop
In emacs, when editing in python, how do I move the cursor from the beginning of the for loop to the end?
In other languages that use curly braces, by typing in C-M-n
or C-M-p
from the position of the curled bracket, the cursor moves to the end or beginning of the loop. Is there a similar keyboard shortcut for python code?
Example: Jumping from the beginning of a loop foo to the end or loops of strokes or xyz:
for foo in foolist:
for bar in barlist:
if foo > 100:
print "foo large"
else:
if bar > 100:
print "bar large"
else:
print "bar small"
for xyz in xyzlist:
print "xyz" . xyz
Editorial staff:
I tried with this code below and started to finish, but end to beginning doesn't work, it jumps from end else
to end of the corresponding if
:
(define-key python-mode-map "\C-\M-n" 'python-nav-end-of-block)
(define-key python-mode-map "\C-\M-p" 'python-nav-beginning-of-block)
I think what you are looking for is:
python-nav-beginning-of-block
python-nav-end-of-block
You can override the binding using:
(define-key python-mode-map ...)
Yes, you can use forward-sexp
and backward-sexp
. It is associated with C-M-f
and C-M-b
.
If you like evil mode , you can use the evil-matchit mode, which expands %
to sexps ( if
, for
etc.).
With the current line python-mode.el
py-beginning-of-top-level
respectively.
py-end-of-top-level
As you can see, the start of blocking will also stop at "if".
By the way, the beginning of NAME must be delivered for all known loops