This morning while working on my AoC submission, I wondered what the simplest procedure was for sharing a minimal setup for editing Python in Visual Studio Code using ruff for formatting, linting and import sorting, and doing all of that automatically on save.

It turns out that you can just store the following two files in the .vscode sub-directory of your source directory, and all of the above will be done.

If you are working with others on the same project, and you’re not using devcontainers, you could consider checking these in to git.

extensions.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// this goes in ${workspaceFolder}/.vscode/settings.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
    // List of extensions which should be recommended for users of this workspace.
    "recommendations": [
        // formatting (replaces black), linting (replaces flake8), sorting imports!
        "charliermarsh.ruff"
    ],
    // List of extensions recommended by VS Code that should not be recommended for users of this workspace.
    "unwantedRecommendations": []
}

settings.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// this goes in ${workspaceFolder}/.vscode/settings.json
{
      "editor.formatOnSave": true,
      "[python]": {
          "editor.defaultFormatter": "charliermarsh.ruff"
      },
      "editor.codeActionsOnSave": {
          "source.organizeImports": "explicit"
      },
      "python.testing.pytestEnabled": true
  }