0

In python, indentation level defines (?) a new block.

for a in range(10):
  print(a)
  for c in range(10):
     print(c)
  .......

# I'd like to code here but hard to know if lines are long..

when this goes long, it is hard to know where I should start coding.

Is there a tool in emacs that can aid this problem?

  • So your for loop is over 100 lines long? You can split the window either vertically or horizontally. If your loop body is really that long I'd consider writing some functions to make the source code more manageable. – eflanigan00 Jul 27 '17 at 17:22
1

In all emacs python modes that I have used, pressing TAB (bound to indent-for-tab-command) will "intelligently" indent to the current block level. Repeatedly pressing TAB will then out-dent one level at a time, in case you want to close a block. If you select a region of text first, then TAB should indent the whole region to the current block level (assuming that transient-mark-mode is activated), but this doesn't work if there are intervening blank lines. Better to use C-c > and C-c < (bound to python-indent-shift-{right,left}) if you need to indent/outdent whole regions of code.

But eflanigan00's comment is correct: it is good programming style to keep blocks of code short, especially if they are deeply indented. If you find you need to scroll back and forth a lot in order to follow a program's structure, then it's probably time to refactor into shorter functions.

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.