Sub topic 14
Using Ruff in your editor
Running ruff check and ruff format in a terminal works, but you get faster feedback if your editor shows violations as you type. This chapter assumes you can run ruff check and ruff format from the command line and can read a pyproject.toml file.
How the editor integration works
Ruff ships a language server, started with ruff server. A language server is a background process that an editor talks to using the Language Server Protocol (LSP). The editor sends the file contents. Ruff replies with diagnostics (the underlines), code actions (quick fixes), and formatted text.
The server reads the same pyproject.toml (or ruff.toml) that the command line uses. Rule selection, per-file-ignores, and formatter options therefore behave the same in the editor, in pre-commit, and in CI. Keep your configuration in the project file and avoid duplicating it in editor settings.
Setting up VS Code
Install the Ruff extension (identifier charliermarsh.ruff) from the marketplace. It bundles a copy of Ruff, so it works even if Ruff is not installed in your environment. Add this to .vscode/settings.json to format and fix on save:
{
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.fixAll.ruff": "explicit",
"source.organizeImports.ruff": "explicit"
}
}
}The two code actions map to features you already know:
source.fixAll.ruffapplies the safe fixes thatruff check --fixwould apply.source.organizeImports.ruffsorts imports using theIrules. It only has an effect if you selected theIrules in your configuration.
Setting up Neovim
Neovim has a built-in LSP client. With the nvim-lspconfig plugin, you can enable Ruff in one line. Ruff must be on your PATH, for example from pip install ruff or from a package manager.
require("lspconfig").ruff.setup({})Many people run Ruff alongside a type checker such as Pyright. Both servers can offer hover text, so you may see duplicate popups. If that bothers you, disable the hoverProvider capability for Ruff in its on_attach function.
Other editors
Any editor with LSP support can run ruff server. Helix, Zed, Emacs, and Sublime Text have either a Ruff plugin or a documented language-server setting. The behavior is the same, and only the way you register the command differs.
Common problems
- Editor and CLI disagree. The extension's bundled Ruff may be a different version from the one in your virtual environment. A newer version can add rules or change formatting. Pin Ruff in your project's dev dependencies, and check which binary your editor uses. In VS Code, the extension's
ruff.importStrategysetting controls whether it uses the bundled copy or the one from your environment. - Nothing is underlined. Check that the file is saved inside a project with a configuration file. Without one, Ruff uses its default rules, which are a small set.
- Imports do not sort. The
Irules are not in the defaults. Add"I"toselect. - Save feels slow or edits seem odd. Two formatters may be enabled at once, such as Black and Ruff. Set a single default formatter for Python.
Exercise
Your teammate's editor underlines an unused import, but yours does not, even though both use VS Code and the same repository. Name two likely causes and how you would check each.
Show answer
First, the extensions may use different Ruff versions, or yours may use a different binary. Compare the version shown in the Ruff output panel with ruff --version in the project environment, and check ruff.importStrategy. Second, the extension may not be reading the project's configuration. Confirm that pyproject.toml is in the opened workspace folder, that F (or F401) is selected, and that the file is not matched by an ignore or exclude entry.