Skip to content
| Marketplace
Sign in
Visual Studio Code>Formatters>Razor FormatterNew to Visual Studio Code? Get it now.
Razor Formatter

Razor Formatter

razor-code-formatter

|
57 installs
| (0) | Free
Prettier-grade Razor (.cshtml / .razor) formatter for VS Code, Cursor, and Antigravity
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

Razor Formatter for VS Code, Cursor & Google Antigravity

Version License: MIT GitHub

A deterministic, Prettier-grade code formatter for ASP.NET Core Razor (.cshtml and .razor) files. Unifies HTML semantic structure, Razor block directives, embedded CSS/JavaScript, and C# control flow into an idempotent, lightning-fast formatting pipeline.


✨ Features

  • ⚡ Sub-Millisecond Formatting: Built-in persistent IPC daemon (--server) ensures instant, flicker-free format-on-save.
  • 🎯 100% Idempotent: Formatting an already formatted document emits 0 changes — no formatting ping-pong.
  • 🧩 Semantic Mixed-Language Support: Dedicated lexers for Razor syntax, C# expressions, HTML5 markup, comments, and raw string literals ("""...""").
  • 🔄 C# Control-Flow Directives: Cleanly indents @if, @else if, @else, @foreach, @for, @while, @switch, @try, @catch, @finally, @using, @lock, @code { ... }, and @{ ... }.
  • 🎨 Embedded CSS & JavaScript: Accurately handles inline <style> and <script> blocks without corrupting surrounding markup.
  • 📐 Attribute Wrapping Policies: Supports auto, preserve, and always attribute layout modes, keeping Tag Helpers and HTML attributes neatly aligned.
  • ⚙️ EditorConfig & .razorignore Support: Automatically respects your project's .editorconfig hierarchy and .razorignore exclusions.
  • 🔒 Security Hardened: Safe workspace trust enforcement, ReDoS timeouts, and shell-free process execution.

🚀 Getting Started

1. Set as Default Formatter

Add the following to your VS Code / Cursor settings.json:

{
  "[aspnetcorerazor]": {
    "editor.defaultFormatter": "razor-formatter.razor-code-formatter",
    "editor.formatOnSave": true
  },
  "[razor]": {
    "editor.defaultFormatter": "razor-formatter.razor-code-formatter",
    "editor.formatOnSave": true
  }
}

2. Format a File & Available Commands

  • Format Document: Shift + Alt + F (Windows) / Shift + Option + F (macOS) / Ctrl + Shift + I (Linux).
  • Format Selection: Ctrl + K, Ctrl + F.
  • Format on Save: Automatically runs whenever you save a Razor file.

Command Palette Commands (Ctrl + Shift + P / Cmd + Shift + P)

  • RazorFormatter: Format Document: Formats the active .cshtml or .razor file.
  • RazorFormatter: Format Workspace: Recursively formats all Razor files across your entire project workspace with progress reporting.
  • RazorFormatter: Check Formatting: Scans the workspace and warns you if any files have unformatted code.
  • RazorFormatter: Restart Formatter Server: Restarts the background formatting daemon.

⚙️ Configuration Settings

Configure these in VS Code Settings (Ctrl+, / Cmd+,) under Razor Formatter:

Setting Default Description
razorFormatter.serverMode true Runs Razor Formatter as a persistent background daemon for sub-millisecond formatting without process restart overhead.
razorFormatter.indentSize 4 Number of spaces per indentation level.
razorFormatter.indentStyle "space" Indentation style ("space" or "tab").
razorFormatter.wrapAttributes "auto" Attribute wrapping policy ("auto", "preserve", or "always").
razorFormatter.formatEmbeddedLanguages true Format embedded CSS inside <style> and JavaScript inside <script> blocks.
razorFormatter.collapseMultipleBlankLines true Collapse consecutive blank lines to a single blank line.
razorFormatter.cliPath "" Optional explicit path to the razor-format CLI executable. (Machine-scoped for security).

📝 EditorConfig Integration

Razor Formatter automatically detects and prioritizes .editorconfig rules in your solution:

[*.{cshtml,razor}]
indent_style = space
indent_size = 4
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true

# Razor-specific settings
razor_wrap_attributes = auto
razor_format_embedded = true
razor_collapse_empty_lines = true

🔗 Useful Links

  • GitHub Repository: Khilaraj123/RazorFormatter
  • Report an Issue: GitHub Issues
  • Visual Studio 2022 Extension: Marketplace
  • .NET Global Tool: dotnet tool install -g RazorCodeFormatter

📄 License

Licensed under the MIT License.

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
  • Your Privacy Choices
  • Consumer Health Privacy
© 2026 Microsoft