My IDE of choice is vim. I use various tools to perform linting and code formatting, and configure them all with ALE (the Asynchronous Lint Engine).
After using several discrete tools (black, isort, flake8, etc) I have settled on using Ruff to do my python code formatting and linting. Here's the relevant fragment of my ALE config in my .vimrc:
" ALE config let g:ale_fixers = { \ 'python': ['ruff', 'ruff_format'], \} let g:ale_linters = { \ 'python': ['ruff'], \} let g:ale_python_ruff_use_global = 1
One of the last remaining wrinkles I had was getting Ruff to automatically sort import statements. Sorting imports is performed by the Ruff linter, not the formatter, which is documented here. The fix on the command line is to add an option, like this:
ruff check --select I --fix
The difficulty I had was getting this to happen in the editor when the file was saved.
It turns out, all I needed to do was add one line:
let g:ale_python_ruff_options = '--extend-select I'
--extend-select is similar to --select.
- --select specifies a list of rule codes or prefixes to enable, replacing the defaults.
- --extend-select specifies a list of rule codes or prefixes to enable in addition to those specified by select.
The final config:
" ALE config let g:ale_fixers = { \ 'python': ['ruff', 'ruff_format'], \} let g:ale_linters = { \ 'python': ['ruff'], \} let g:ale_python_ruff_use_global = 1 let g:ale_python_ruff_options = '--extend-select I'
Comments