Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 40 ยท Editing and Navigation

Formatting, IntelliSense and Code Actions

Format code on save, choose a default formatter, use IntelliSense completions and parameter hints, apply quick fixes and organize imports automatically with code actions on save.

A good editor does more than store text: it completes your code, fixes your mistakes and keeps the style tidy. VS Code does this through three related features: IntelliSense (suggestions), formatting (layout) and code actions (fixes and refactorings). Language extensions provide most of the intelligence; the editor provides the user interface.

IntelliSense

IntelliSense is the umbrella name for code completion, parameter hints and quick info.

  • Completions appear as you type. Press Ctrl+Space to open the list manually. Tab or Enter accepts; Esc closes. Icons show the kind: method, variable, class, snippet, keyword.
  • Parameter hints pop up after (, showing the signature and the current argument. Ctrl+Shift+Space shows them on demand; Up/Down switches overloads.
  • Quick Info (hover): hover over a name or press Ctrl+K Ctrl+I to see its type and documentation.
  • Accept with commit characters: with editor.acceptSuggestionOnCommitCharacter, typing . or ( accepts the highlighted suggestion.
  • Inline suggestions (ghost text) come from AI extensions such as GitHub Copilot; Tab accepts them. They are separate from IntelliSense.

IntelliSense quality depends on the language service behind it. For JavaScript and TypeScript it is built in. For Python you install the Python extension, which uses a language server (Pylance). If suggestions are missing, check the language mode in the Status Bar and that the right interpreter or project is selected.

Useful settings:

JSON
{
  "editor.quickSuggestions": { "other": true, "comments": false, "strings": false },
  "editor.suggestSelection": "recentlyUsed",
  "editor.snippetSuggestions": "top",
  "editor.parameterHints.enabled": true
}

Formatting

Formatting rearranges whitespace, line breaks and sometimes quotes according to a style, without changing what the code does.

  • Format Document: Shift+Alt+F (macOS: Shift+Option+F).
  • Format Selection: Ctrl+K Ctrl+F.
  • Format on type and format on paste are available with editor.formatOnType and editor.formatOnPaste.
  • Format on save: set "editor.formatOnSave": true. Combine it with "editor.formatOnSaveMode": "modifications" to format only the lines you changed (needs Git and a formatter that supports ranges), which keeps diffs small in a codebase that is not yet formatted.

If a language has no formatter, VS Code offers to install one when you press Shift+Alt+F.

Choosing the default formatter

Several extensions can format the same language, for instance Prettier, ESLint (with fixes) and the built-in TypeScript formatter. Set the winner per language:

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

You can also right-click in the editor and choose Format Document With... then Configure Default Formatter. The extension ID is shown on the extension's Marketplace page.

Many formatters read a project configuration file, for example .prettierrc, .editorconfig or pyproject.toml. Prefer these so everyone and the command line tools produce the same result as VS Code.

EditorConfig

An .editorconfig file at the root describes basics such as indentation across editors:

INI
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.py]
indent_size = 4

VS Code reads it through the EditorConfig extension (and some formatters read it natively).

A stand-in: a miniature formatter

A formatter parses code and prints it again in one standard layout. This JavaScript stand-in re-indents lines by counting braces, which is only a crude model of the idea (real formatters parse the full syntax tree). It shows how "format on save" produces consistent results regardless of how you typed.

JS Stand-in: re-indent by braces

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

No output.

The last line prints true: formatting an already formatted file changes nothing. That property, called idempotence, is what you should expect from a good formatter.

Code actions and the light bulb

When VS Code can fix or improve something it shows a light bulb (yellow for quick fixes, blue for refactorings) in the margin, or you press Ctrl+. (macOS: Cmd+.). The menu lists code actions such as:

  • Quick Fix: add a missing import, declare a variable, remove an unused variable, fix a typo that the spell checker or linter found.
  • Refactor: extract to a function or constant, convert to arrow function, move to a new file (see Refactoring and Symbols).
  • Source Action: organize imports, fix all auto-fixable problems.

Hovering over a squiggle shows the problem and often Quick Fix... as a link. Red squiggles are errors, yellow are warnings and blue dots are hints. The Problems panel (Ctrl+Shift+M) lists them all; F8 jumps to the next problem and Shift+F8 to the previous.

Code actions on save

Run safe fixes automatically each time you save:

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

"explicit" runs them only on an explicit save (not auto-save), "always" also runs them on auto-save, and "never" turns them off. Use them together with format on save to keep a project tidy without thinking about it.

Linters

A linter analyzes code for likely bugs and style problems and reports them as squiggles. ESLint (JavaScript/TypeScript), Pylint or Ruff (Python), and Checkstyle (Java) are examples, each added by an extension. Formatters decide layout; linters decide correctness and conventions. Their fixes appear as code actions, so the two systems work together.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which shortcut formats the whole document on Windows and Linux?

    Shift+Alt+F is Format Document; Ctrl+Shift+F is Search.
  2. 2. What does the light bulb in the editor margin offer?

    The light bulb (or Ctrl+.) opens the Code Actions menu.
  3. 3. How do you choose which extension formats Python files?

    A per-language block with editor.defaultFormatter selects the formatter for that language.
  4. 4. What is the difference between a formatter and a linter?

    Formatters change code layout, while linters analyze code and report problems.

Summary

  • IntelliSense provides completions, parameter hints and hover info; Ctrl+Space opens suggestions.
  • Format with Shift+Alt+F, or automatically with editor.formatOnSave.
  • Pick a default formatter per language and keep project config files (.prettierrc, .editorconfig).
  • Ctrl+. opens code actions: quick fixes, refactorings, organize imports.
  • editor.codeActionsOnSave applies safe fixes on every save.

Next: Refactoring and Symbols