What is Ruff?
Ruff is a modern Python tool that’s incredibly fast and combines multiple tools in one:- Linting - Finding issues and potential errors
- Formatting - Fixing code style automatically
- Import sorting - Organizing imports cleanly
A linter checks your code for style issues and potential errors. A formatter automatically fixes these issues for you.
Install Ruff in VS Code
- Open VS Code
- Press
Ctrl+Shift+X(Windows/Linux) orCmd+Shift+X(Mac) - Search for “Ruff”
- Install the official Ruff extension by Astral
The Ruff extension replaces older tools like Pylint, Black, and isort. You only need this one extension.
Enable format on save
Let’s make VS Code automatically format your code when you save:- Press
Ctrl+,(Windows/Linux) orCmd+,(Mac) to open settings - Search for “format on save”
- Check the box for “Editor: Format On Save”
- Search for “default formatter”
- Set “Python > Formatting: Provider” to “Ruff”
See it in action
Here’s messy code before formatting:- Separated imports
- Added proper spacing
- Fixed indentation
- Made quotes consistent
- Formatted the list for readability
If formatting doesn’t work on save, right-click in your Python file and select “Format Document”. This should trigger Ruff manually.
Common formatting rules
Ruff follows Python’s style guide (PEP 8) automatically:- 4 spaces for indentation
- Spaces around operators (
x = 1, notx=1) - Two blank lines between functions
- Maximum line length of 88 characters
Understanding linting errors
Ruff will underline code issues in your editor:- What the issue is
- Why it matters
- How to fix it
Don’t ignore linting warnings! They help you write better code and catch bugs early.