Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 40 ยท Extensions

Formatters and Linters

Set up format on save, choose a default formatter per language, use ESLint code actions on save, and avoid formatter conflicts in a VS Code project.

A formatter rewrites code layout: indentation, quotes, line width. A linter analyzes code for likely mistakes: unused variables, unreachable code, risky patterns. The two jobs overlap only a little, and good setups let each tool do its own job. VS Code does not ship Prettier or ESLint; it provides the plumbing (a "format document" command, diagnostics squiggles, code actions) and extensions supply the tools.

This lesson covers the settings that connect them, and shows the exact keys you would put in settings.json. We cannot run the real tools here, so the examples are settings you copy and small JavaScript stand-ins that illustrate what formatting and linting do.

Formatting commands

  • Format Document: Shift+Alt+F (Windows/Linux) or Shift+Option+F (macOS). Reformats the whole file.
  • Format Selection: Ctrl+K Ctrl+F. Reformats only the selected lines.
  • Format Document With...: from the Command Palette, lets you choose a formatter for this run.

If several formatters apply to a language, VS Code asks which one to use. Stop the question by setting a default:

JSON
{
  "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
  "[json]":       { "editor.defaultFormatter": "vscode.json-language-features" },
  "[python]":     { "editor.defaultFormatter": "ms-python.black-formatter" }
}

The square-bracket key is a language-specific setting block. Everything inside applies only to that language.

Format on save, paste and type

Three settings control automatic formatting:

JSON
{
  "editor.formatOnSave": true,
  "editor.formatOnPaste": false,
  "editor.formatOnType": false
}

formatOnSave is the one most teams enable. formatOnSaveMode can be set to "modifications" so that only lines you changed are formatted, which keeps diffs small in a codebase that is not yet fully formatted. The mode "modificationsIfAvailable" falls back to the whole file when the source control cannot report changes.

Config files beat editor settings

Tools such as Prettier read a project configuration file (.prettierrc, prettier.config.js) and ESLint reads eslint.config.js. Put your style rules there, not in VS Code settings. Then the editor, the command line and the CI server all agree. Settings in VS Code should only decide when formatting happens and which formatter is the default.

A short .prettierrc looks like this:

JSON
{
  "semi": true,
  "singleQuote": true,
  "printWidth": 90
}

Add a .prettierignore for generated folders such as dist/.

Linting and code actions

Linters report diagnostics, shown as squiggles, in the Problems panel (Ctrl+Shift+M) and on the Explorer. Many problems come with a Quick Fix (the light bulb, or Ctrl+.). To apply fixes automatically on save, use code actions:

JSON
{
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": "explicit",
    "source.organizeImports": "explicit"
  }
}

"explicit" means the action runs when you save by hand, not on auto-save. Older guides use true; current VS Code accepts "explicit", "always" and "never".

If Prettier and ESLint both complain about the same style rule, turn off the ESLint style rules (the eslint-config-prettier config does this) and keep ESLint for real bugs only.

Formatter or linter?

Question Formatter Linter
Changes only whitespace and layout? Yes No
Finds probable bugs? No Yes
Can change meaning of code? Never Only with explicit fixes
Usually run on save continuously and in CI

Runnable stand-ins

The JavaScript below is not Prettier or ESLint. It is a tiny model of the idea: a formatter normalizes layout without touching meaning, and a linter reports problems without changing anything.

JS Stand-in: a tiny formatter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: a tiny linter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Troubleshooting

  • Nothing happens on save. Check that a formatter is installed for the language and that editor.formatOnSave is on. Open the Output panel and pick the formatter's channel.
  • "There is no formatter for X files installed." Install a formatter extension for that language.
  • Format on save changes the whole file. Use formatOnSaveMode: "modifications" until the project is formatted once.
  • The format is different from the CI result. Make sure both read the same config file and the same tool version.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which setting turns on automatic formatting when you save?

    editor.formatOnSave formats on save. files.autoSave controls when files are saved, and codeActionsOnSave runs code actions.
  2. 2. Where should style rules such as quote style and line width live?

    A project config file such as .prettierrc keeps editor, command line and CI in agreement.
  3. 3. What does [javascript]: { ... } in settings.json do?

    A language identifier in square brackets creates a language-specific settings block.
  4. 4. What is the main difference between formatter and linter?

    Formatters rewrite layout without changing meaning; linters analyze code and report problems.

Summary

  • Set a default formatter per language with [language] blocks.
  • Turn on editor.formatOnSave and, if needed, "modifications" mode.
  • Keep style rules in project config files, not editor settings.
  • Use editor.codeActionsOnSave to apply lint fixes and organize imports.
  • Let the formatter handle style and the linter handle bugs.

Next: Language Servers and IntelliSense