Emacs bulk indent for Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When Python indentation drifts in Emacs, the usual fix is to re-indent a selected region or the whole buffer using Emacs indentation commands. The important point is that Python indentation is syntax-sensitive, so Emacs can only help reliably when the buffer is already structurally valid enough to parse.
Indent a Region
The standard Emacs command for bulk indentation is indent-region. In interactive use, the common workflow is:
- Mark the region
- Run
indent-region
The default keybinding is:
So for a selected Python block, C-M-\ usually re-indents it according to the active Python mode rules.
You can also run it by name:
Re-indent the Entire Buffer
For the whole file, select the entire buffer first:
Then run:
In practice, the full shortcut sequence is:
That is the fastest built-in answer to "bulk indent this Python file."
Shift Indentation Right or Left
Sometimes you do not want Emacs to recompute indentation logic. You only want to shift a block left or right. In that case, mark the region and use the Python mode shift commands if your setup exposes them, or indent rigidly through built-in region commands.
This is useful when you pasted a block at the wrong nesting level but the internal structure of the block is already correct.
If you prefer command names over key chords, M-x indent-rigidly is a useful built-in tool for shifting a region by a fixed number of columns.
Why Syntax Problems Break Indentation
If the file has unbalanced parentheses, a missing colon after if, or other syntax damage, Emacs may indent in surprising ways. That is not usually an indentation-engine bug. It is often Emacs trying to infer structure from invalid Python.
So if bulk indentation looks wrong, check for:
- missing colons
- unmatched brackets
- unfinished triple-quoted strings
Once the syntax is fixed, the indentation command becomes much more reliable.
A Practical Workflow
For a messy pasted block, a good workflow is:
- Fix obvious syntax issues
- Mark the block or whole buffer
- Run
indent-region - Re-check the result visually
That is usually faster and safer than manually re-spacing dozens of lines.
Another practical habit is to run the Python formatter or linter after re-indenting. Bulk indentation makes structure readable again, but a formatter can still catch lines that are syntactically valid yet stylistically inconsistent. That is often the fastest way to confirm the cleanup worked.
For teams that already use black, ruff, or another formatter in CI, Emacs bulk indentation is best treated as the first cleanup step rather than the final formatting step.
That is usually the smoother workflow in real Python projects.
Overall.
Common Pitfalls
- '
indent-regiondepends on the active major mode, so make sure the buffer is in Python mode.' - Bad Python syntax often causes confusing indentation results.
- Re-indenting the whole buffer is convenient, but it can hide the real issue if the parser is already confused.
- Use whole-buffer indentation for cleanup, but inspect the result rather than assuming it is always correct.
Summary
- The standard Emacs answer for bulk indentation is
indent-region. - For the whole file, use
C-x h C-M-\. - Syntax errors often explain bad indentation results better than editor bugs do.
- Fix structure first, then let Emacs re-indent the region or buffer.
Related reading
- Encrypt and decrypt using PyCrypto AES-256
- Ensemble of different kinds of regressors using scikit-learn or any other python framework
- EOFError Compressed file ended before the end-of-stream marker was reached - MNIST data set
- e.printStackTrace equivalent in python
- Error after upgrading pip cannot import name 'main
- ERROR Cannot uninstall 'wrapt'. when installing tensorflow-gpu1.14
- ERROR Could not build wheels for scipy which use PEP 517 and cannot be installed directly
- ERROR Could not find a version that satisfies the requirement tensorflow from versions none ERROR No matching distribution found for tensorflow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.