What Ruff does for you
Ruff combines the functionality of several well-known Python tools into one:- Linting — detects code issues, unused imports, undefined variables, and style violations
- Formatting — automatically rewrites your code to follow consistent style rules
- Import sorting — organises your
importstatements into a logical order
A linter reads your code and reports problems. A formatter actually rewrites your code to fix style issues. Ruff does both.
Install the Ruff extension
1
Open the Extensions panel
Press
Ctrl + Shift + X (Windows/Linux) or Cmd + Shift + X (macOS).2
Search for Ruff
Type Ruff in the search box and find the official extension published by Astral (the team behind Ruff).
3
Install it
Click Install and wait for the installation to complete.
The Ruff VS Code extension bundles the Ruff binary — you do not need to install anything via pip to use it for linting and formatting in VS Code.
Enable format on save
The most impactful way to use Ruff is to have it format your code automatically every time you save a file. This means you never have to think about code style — just write code, pressCtrl/Cmd + S, and Ruff cleans it up instantly.
1
Open Settings
Press
Ctrl + , (Windows/Linux) or Cmd + , (macOS).2
Enable format on save
Search for format on save and check the Editor: Format On Save box.
3
Set Ruff as the default formatter
Search for default formatter, then set Editor: Default Formatter to Ruff.
See it in action
Here is a messy Python file before saving:Ctrl/Cmd + S, Ruff transforms it automatically:
- Added the required two blank lines before function definitions
- Inserted spaces around operators (
=,+=,*) - Switched single quotes to double quotes (consistent style)
- Wrapped the long list into readable multi-line format
- Fixed indentation to exactly 4 spaces
Formatting rules Ruff follows
Ruff applies Python’s official style guide (PEP 8) automatically. The main rules you will notice:
You do not need to memorise any of these — Ruff handles them for you on every save.
Understanding linting warnings
Beyond formatting, Ruff underlines code issues directly in the editor. Hover over the underlined text to see an explanation:If formatting does not trigger on save
If you save a Python file and nothing changes, try this:- Right-click inside the Python file and select Format Document
- VS Code may ask you to choose a formatter — select Ruff
- Once set, automatic formatting on save should work for all future saves