Keyboard shortcut to fix indentation in vs code: practical guide

Learn how to fix indentation in VS Code quickly using built-in shortcuts. This guide covers Windows and macOS mappings, settings, and best practices for consistent formatting across multiple languages.

Shortcuts Lib
Shortcuts Lib Team
·5 min read
Quick AnswerDefinition

To fix indentation in VS Code, use the built-in Format Document shortcut. On Windows, press Shift+Alt+F; on macOS, Shift+Option+F. This reformats the current file according to your language rules and editor settings. Shortcuts Lib recommends using Format Document to maintain consistent indentation across your project.

Introduction: Indentation matters and how shortcuts accelerate coding

Indentation isn’t just about looks; it affects readability, tooling, and how linters and formatters judge your code. In VS Code, the keyboard shortcut to fix indentation in vs code can dramatically speed up this routine, especially when switching between languages with different styles. According to Shortcuts Lib, mastering built-in shortcuts for formatting reduces context switching and helps teams stay aligned on style guidelines. In this guide, you’ll learn how indentation works in VS Code, how to trigger a consistent reformat, and how to tailor the editor to your project’s conventions. You’ll also see practical examples across languages and tips to avoid common mistakes that break alignment across files.

Core shortcut: Format Document (the fast path to clean indentation)

The core technique for quickly correcting indentation is the Format Document command, which reflows indentation based on the active language rules and your editor settings. Here are concrete examples in action and how to trigger them:

Before formatting (Python):

Python
def greet(name): print("Hello, "+name) if name: print("Nice to meet you")

After formatting (Python):

Python
def greet(name): print("Hello, "+name) if name: print("Nice to meet you")

In JavaScript:

JavaScript
function sayHi(user){ console.log("Hi "+user) }

Formatted (JavaScript):

JavaScript
function sayHi(user) { console.log("Hi " + user) }

The shortcut is visible in the VS Code key bindings: Windows users typically press Shift+Alt+F; macOS users press Shift+Option+F. You can also trigger the same action from the Command Palette (Ctrl+Shift+P on Windows or Cmd+Shift+P on macOS) by selecting “Format Document.” If your project includes formatters like Prettier, the behavior will follow language-specific rules and your editor settings.

Activate formatting on save and project-wide consistency

Automatic formatting on save is a powerful way to keep indentation consistent without thinking about shortcuts in every file. Enable this in Settings: set editor.formatOnSave to true. This ensures that every time you save a file, the indentation is corrected according to the active formatter and language rules. In multi-language projects, it’s common to pair Format Document with a per-language configuration so that HTML, CSS, and JavaScript each follow their own indentation standards without manual intervention. Shortcuts Lib recommends configuring a shared formatter (like a linter/formatter combo) to prevent drift across the codebase.

Code snippet: user settings snippet to enable on-save formatting

JSON
{ "editor.formatOnSave": true, "editor.detectIndentation": false, "editor.tabSize": 2, "editor.insertSpaces": true }

If your team uses Prettier, ensure Prettier’s own settings align with VS Code so formatting decisions don’t collide. This is a common source of confusion when multiple formatters are configured in the workspace.

Alternative: Reindent Lines vs Format Document and when to use which

Sometimes you only want to fix a block of code rather than the entire document. VS Code provides a related command: Reindent Lines. This is useful when working with a small section that’s misaligned due to manual edits. The general approach is to select the block and run the reindent command, or bind it to a keyboard shortcut for quick access. In contrast, Format Document reflows the entire file using language-specific rules, which is ideal when you have large misalignment or multiple languages in a single file.

Example keybindings (illustrative; adjust as needed):

JSON
[ { "key": "ctrl+k ctrl+f", "command": "editor.action.reindentLines", "when": "editorTextFocus" } ]

Tip: If your repository uses a shared formatter configuration, prefer Format Document for whole-file fixes and reserve Reindent Lines for targeted blocks to minimize unintended style changes.

Editor settings: enforce indentation style and size across the project

Consistency starts with settings. Turn off auto-detection and explicitly set your preferred indentation style. This section shows how to configure a project to use spaces, a 2-space tab size, and a fixed indentation policy. You can place these settings in your global settings.json or per-workspace .vscode/settings.json to ensure all contributors align with the same rules. When combined with Format Document, this helps keep the code uniformly indented across languages—key for readability and tooling compatibility.

Code example: language-agnostic indentation policy

JSON
{ "editor.detectIndentation": false, "editor.insertSpaces": true, "editor.tabSize": 2 }

Optional: create an .editorconfig at the repo root to enforce per-language indentation rules across editors. This reduces drift when contributors use different IDEs.

Real-world example: multi-language project with HTML, CSS, and JS

Web projects often mix HTML, CSS, and JavaScript. Indentation rules differ by language, so a single indentation policy can still apply: 2 spaces for all but HTML where nested blocks should also respect 2 spaces. The Format Document shortcut helps keep these rules intact while you refactor. Consider the following sample before and after; this demonstrates how a single reformat action harmonizes multi-language files.

Before:

HTML
<!DOCTYPE html> <html> <head> <title>Demo</title> </head> <body> <div> <p>Text</p> </div> </body> </html>

After (formatted):

HTML
<!DOCTYPE html> <html> <head> <title>Demo</title> </head> <body> <div> <p>Text</p> </div> </body> </html>

The key takeaway is that one shortcut, the editor’s language-aware formatter, can drive consistency across languages, reducing manual edits and surfacing indentation anomalies early in the development cycle.

Pitfalls to avoid and how to diagnose indentation problems

Even with a solid shortcut strategy, indentation drift can creep in. The most common culprits are mixed spaces and tabs, language-specific formatter configurations, and conflicting extension settings. If Format Document seems to produce unexpected results, check that:

  • editor.insertSpaces and editor.tabSize align with your project policy
  • Language-specific formatters (Prettier, Black, or clang-format) aren’t overriding VS Code rules
  • Workspace settings aren’t enforcing a different indentation policy

A practical debugging tip is to temporarily disable non-essential formatters and re-run Format Document to isolate the effect. If you still see drift after saving, run a quick search for tab-size mismatches in the repo and harmonize them before continuing.

Project-wide conventions: aligning teams with a shared policy

Conscious collaboration requires a shared indentation policy. Add a repository-wide .editorconfig and a minimal VS Code workspace setting to enforce base rules, then rely on Format Document for everyday fixes. This approach reduces stylistic noise during code reviews and ensures consistent diffs across changes. Shortcuts Lib recommends pairing these conventions with a linting/formatting pipeline to catch any stray indentation issues before merges.

Example .editorconfig:

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

Troubleshooting: when formatting doesn’t apply as expected

If the format doesn’t seem to affect indentation, check a few quick things:

  • Is there a language-specific formatter with higher priority? Adjust extension order or disable conflicting formatters.
  • Is there a per-project setting that overrides the global policy? Inspect .vscode/settings.json and .editorconfig at the repo root.
  • Is the selection or document focused when you invoke the shortcut? Ensure the editor has focus and an appropriate provider is available.

If all else fails, reloading VS Code or re-opening the project can resolve transient extension conflicts. Remember, the goal of indentation shortcuts is to reduce manual edits, not to create new formatting conflicts.

Steps

Estimated time: 20-30 minutes

  1. 1

    Open your target file in VS Code

    Navigate to the file you want to format and ensure the editor is focused so the shortcut applies to the correct document.

    Tip: Use the Explorer panel or Quick Open (Ctrl+P / Cmd+P) to locate the file quickly.
  2. 2

    Trigger the Format Document shortcut

    Press Shift+Alt+F on Windows or Shift+Option+F on macOS to reindent the entire document according to the active language rules.

    Tip: If you’re using a non-English keyboard, check your OS key mappings and VS Code keybindings.
  3. 3

    Review the result and adjust if needed

    Scan the reformatted file for any section that may require language-specific tweaks or manual adjustments for readability.

    Tip: If a line looks odd, use indentation commands on the specific block.
  4. 4

    Optionally format only a selection

    Highlight a portion of code and use the same shortcut to format only that block, preserving surrounding structure.

    Tip: Use Format Selection in the Command Palette if you prefer explicit control.
  5. 5

    Enable auto-format on save for speed

    Turn on editor.formatOnSave so that formatting happens automatically when you save files in your project.

    Tip: Combine with a shared formatter to maintain consistency across the team.
Pro Tip: Enable Format on Save to keep indentation clean without manual intervention.
Warning: Watch for conflicts between VS Code formatters and project-specific tools like Prettier or ESLint.
Note: Document per-language indentation rules with .editorconfig to reduce drift across editors.

Prerequisites

Required

  • Required
  • Basic familiarity with VS Code UI (Command Palette, Settings)
    Required
  • Knowledge of core keyboard shortcuts (Tab, Shift+Tab, Shift+Alt+F, Ctrl/Cmd combinations)
    Required

Optional

  • Optional: Access to project-wide editorconfig or Prettier for consistent formatting
    Optional

Keyboard Shortcuts

ActionShortcut
Format DocumentFormats the entire file using language rules and editor settings+Alt+F
Indent selectionIndent selected lines by one tab stop
Outdent selectionOutdent selected lines by one tab stop+

Questions & Answers

What is the fastest keyboard shortcut to fix indentation in VS Code?

The fastest shortcut is Format Document: Shift+Alt+F on Windows and Shift+Option+F on macOS. This reformats the entire file according to language rules. You can also trigger it from the Command Palette by selecting “Format Document.”

Use the Format Document command with Shift+Alt+F or Shift+Option+F to quickly fix indentation across the file.

Can I format only a selection instead of the whole document?

Yes. Select the block you want to reindent and run Format Document or use a dedicated Format Selection command if your setup exposes it. You can also create a custom key binding for editor.action.formatSelection.

Yes—just select the code and format the selection to reindent only that portion.

How do I ensure consistent indentation across a multi-language project?

Configure language-specific formatters and a shared policy via .editorconfig and editor configurations. Align settings so that HTML, CSS, JS, and other languages follow the same indentation size and style.

Make sure your editor and language formatters agree on indentation rules for every language in the project.

Why might the shortcut not work on my machine?

Possible causes include a conflicting extension, an overridden keybinding, or the active formatter being unavailable for the current language. Check the Keyboard Shortcuts editor and extension settings; verify that a formatter for the language is installed.

If it doesn’t work, check keybindings and ensure a formatter is present for the file’s language.

Is there a way to disable auto-formatting on paste or save?

Yes. In Settings, disable editor.formatOnPaste or editor.formatOnSave to prevent automatic formatting. This gives you manual control when needed, especially in teams with strict formatting rules.

You can turn formatting off on paste or save if auto-formatting conflicts with your workflow.

Main Points

  • Use Format Document to fix indentation across files
  • Learn and rely on Windows/macOS mappings: Shift+Alt+F / Shift+Option+F
  • Keep a single source of truth for indentation via editor settings and .editorconfig
  • Format-on-Save helps maintain consistent indentation in teams

Related Articles