Code Snippet Formatter formats and indents source code in over 20 programming languages: Python, JavaScript, TypeScript, Java, C/C++, C#, Go, Rust, PHP, Ruby, HTML, CSS, JSON, XML, YAML, SQL, and more. Paste unformatted or minified code to get properly indented output with consistent spacing and line breaks β ready to copy into your editor or documentation.
Consistent code formatting improves readability, reduces code review friction, and makes diffs cleaner. Most teams use automated formatters: Prettier (JavaScript/TypeScript/HTML/CSS/JSON), Black (Python), gofmt (Go), rustfmt (Rust), clang-format (C/C++/Java). This tool lets you format snippets without installing or configuring these formatters β useful for quick formatting in documentation, Stack Overflow answers, or blog posts.
The formatter uses language-specific rules: Python uses 4-space indentation (PEP 8); JavaScript and TypeScript default to 2-space (Prettier default); C-family languages use 4-space or brace-on-same-line (K&R style) or brace-on-new-line (Allman style). For HTML and CSS, attribute sorting and property ordering can be optionally applied. SQL formatting capitalizes keywords and aligns clauses.
Format minified JavaScript
Result: var x={a:1,b:2};function f(n){return n*2;} β properly indented with line breaks
Normalize inconsistent Python indentation
Result: Mixed tabs/spaces β uniform 4-space PEP 8-compliant output
Format SQL query
Result: select * from users where id=1 β SELECT * FROM users WHERE id = 1 (capitalized, aligned)
What is the difference between a formatter and a linter?
A formatter automatically rewrites code to apply style rules β spacing, indentation, line length, bracket placement. It changes whitespace and ordering but not logic. A linter analyzes code for potential bugs, code smells, and style violations β it reports issues but does not automatically fix them (though many linters have --fix modes). Tools like ESLint are primarily linters with optional auto-fix; Prettier is purely a formatter.
What is the Prettier philosophy?
Prettier is 'opinionated' β it makes formatting decisions for you and provides few configuration options. The goal is to eliminate debates about style within a team by having exactly one correct format. Prettier reformats code by parsing it to an AST and reprinting from scratch according to its rules. It ignores your original formatting entirely. The tradeoff: you lose some control but gain consistency and stop arguing about style.
What is PEP 8 and why does it matter for Python?
PEP 8 is the official Python style guide (Python Enhancement Proposal #8). Key rules: 4-space indentation, max 79-character line length (72 for docstrings), 2 blank lines between top-level functions/classes, 1 blank line between methods, spaces around operators. Black is an opinionated Python formatter that enforces PEP 8 plus its own rules (88 char line limit, double quotes). Following PEP 8 makes Python code readable across projects.
How do I set up Prettier in a project?
1. Install: npm install --save-dev prettier. 2. Create .prettierrc with options: {"semi": false, "singleQuote": true, "printWidth": 100}. 3. Add a format script: 'prettier --write .' 4. Set up the Prettier VS Code extension with 'Format on Save'. 5. Add to your CI pipeline: 'prettier --check .' to fail builds on unformatted code. Most teams add Prettier to pre-commit hooks via lint-staged.
What is EditorConfig and how does it relate to formatters?
EditorConfig is a file format (.editorconfig) that defines basic indentation and whitespace settings shared across editors and IDEs. It handles: indent_style (space/tab), indent_size (2/4), end_of_line (lf/crlf/cr), charset (utf-8), and trim_trailing_whitespace. EditorConfig is editor-level configuration for basic whitespace; Prettier/Black/gofmt are language-specific formatters for full code style. They complement each other β EditorConfig prevents tabs vs spaces wars; Prettier handles the rest.