HTML
Sublime Text 2
Code Formatting
Web Development
Programming Tools

How to format HTML in Sublime Text 2

Codemia Knowledge Hub ·

Install a formatting plugin such as HTML-CSS-JS Prettify or HTMLTidy through Package Control and run it from the command palette. For indentation only, the built-in Edit -> Line -> Reindent command adjusts indentation based on syntax. These fix HTML made hard to read by bad indentation, mixed tabs and spaces, or several editors with different styles.

Set up Package Control

  1. Install Sublime Text 2: If not already installed, download and install Sublime Text 2 from the official website.
  2. Package Control: Make sure Package Control is installed. It lets you install plugins that reformat HTML. Press Ctrl+Shift+P (Win, Linux) or Cmd+Shift+P (Mac) and type "Install Package Control", then hit Enter.

Install an HTML formatting plugin

Two plugins handle HTML formatting:

  • HTMLTidy: An excellent tool for cleaning up HTML code.
  • HTML-CSS-JS Prettify: A plugin that formats HTML, CSS, and JavaScript.

To install these plugins:

  1. Open the command palette with Ctrl+Shift+P or Cmd+Shift+P.
  2. Type "Install Package", then select "Package Control: Install Package".
  3. Type the name of the plugin (for example "HTML-CSS-JS Prettify") and select it to install.

Format with HTMLTidy

Once HTMLTidy is installed:

  1. Open your HTML file in Sublime Text 2.
  2. Access the command palette (Ctrl+Shift+P or Cmd+Shift+P).
  3. Type "Tidy" and select "TidyHTML" to tidy your document.

Format with HTML-CSS-JS Prettify

  1. After installation, open your HTML document.
  2. From the menu, select Tools -> Command Palette or use the shortcut (Ctrl+Shift+P or Cmd+Shift+P).
  3. Type "Prettify" and select "HTML-CSS-JS Prettify: Prettify Code".

The plugin adjusts the indentation, spacing and structure of your HTML.

Built-in indentation commands

CommandDescriptionShortcut
IndentIncreases indentation levelCtrl+ ] (Win, Linux), Cmd+ ] (Mac)
UnindentDecreases indentation levelCtrl+ [ (Win, Linux), Cmd+ [ (Mac)
ReindentAutomatically adjusts indentation based on syntaxEdit -> Line -> Reindent

Keeping HTML tidy afterwards

  • Consistency: Stick to a consistent style of indentation (tabs or spaces).
  • Use a Linter: HTML linting tools find problems that are not strictly formatting but matter for clean code.
  • Settings Configuration: Configure your Sublime Text 2 settings to automatically match your preferred style, such as tab size, whether to insert spaces instead of tabs, etc.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms