Format Python files with ruff format, following PEP 8 indentation
Answer: The Ruff formatter is an extremely fast Python code formatter. It is designed as a drop-in replacement for Black and is available as part of the ruff CLI via ruff format. PEP 8 says to use 4 spaces per indentation level.
Running the formatter
ruff format is the primary entrypoint to the formatter. Pass a list of files or directories; it formats all discovered Python files. With no path, it formats all files in the current directory. You can also pass a path to a file or a directory (and any subdirectories).
Indentation and continuation lines
PEP 8 says continuation lines should align wrapped elements vertically, or using a hanging indent. When using a hanging indent, there should be no arguments on the first line. Further indentation should clearly distinguish the continuation line. The formatter is designed as a drop-in replacement for Black.
| ruff format | PEP 8 | |---|---| | ruff format | Primary entrypoint to the formatter | | drop-in replacement | Designed as a replacement for Black | | 4 spaces | Indentation level in PEP 8 | | hanging indent | One way to align continuation lines |
See also
Claims
- id: C1 claim: The Ruff formatter is a fast drop-in replacement for Black, invoked as ruff format. source: S1 evidence: "designed as a drop-in replacement for Black"
- id: C2 claim: ruff format accepts files or directories and formats discovered Python files. source: S1 evidence: "It accepts a list of files or directories, and formats all discovered Python files"
- id: C3 claim: PEP 8 uses 4 spaces per indentation level. source: S2 evidence: "Use 4 spaces per indentation level"
Sources
- [S1] The Ruff formatter. Ruff docs. https://docs.astral.sh/ruff/formatter/ . License: MIT. Accessed 2026-11-19.
- [S2] PEP 8 – Style Guide for Python Code. Python PEPs. https://peps.python.org/pep-0008/ . License: Public-Domain. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Ruff docs, The Ruff formatter; Python PEPs, PEP 8 – Style Guide for Python Code. MIT; Public-Domain. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Ruff docs, The Ruff formatter; Python PEPs, PEP 8 – Style Guide for Python Code. MIT; Public-Domain.
Cite this
iinventedlight. 2026. "Format Python files with ruff format, following PEP 8 indentation." iinventedlight Library, iil:note:srcjztmt, version of 2026-11-19. https://iinventedlight.com/library/notes/ruff-format/. License: CC-BY-4.0.
@misc{iil_note_srcjztmt,
author = {{iinventedlight}},
title = {{Format Python files with ruff format, following PEP 8 indentation}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/ruff-format/},
note = {Permanent ID iil:note:srcjztmt. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- ruff-format.table.json (table; CC-BY-4.0)
- ruff-format.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/ruff-format.md.