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
- Install Sublime Text 2: If not already installed, download and install Sublime Text 2 from the official website.
- Package Control: Make sure Package Control is installed. It lets you install plugins that reformat HTML. Press
Ctrl+Shift+P(Win, Linux) orCmd+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:
- Open the command palette with
Ctrl+Shift+PorCmd+Shift+P. - Type "Install Package", then select "Package Control: Install Package".
- Type the name of the plugin (for example "HTML-CSS-JS Prettify") and select it to install.
Format with HTMLTidy
Once HTMLTidy is installed:
- Open your HTML file in Sublime Text 2.
- Access the command palette (
Ctrl+Shift+PorCmd+Shift+P). - Type "Tidy" and select "TidyHTML" to tidy your document.
Format with HTML-CSS-JS Prettify
- After installation, open your HTML document.
- From the menu, select
Tools -> Command Paletteor use the shortcut (Ctrl+Shift+PorCmd+Shift+P). - 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
| Command | Description | Shortcut |
| Indent | Increases indentation level | Ctrl+ ] (Win, Linux), Cmd+ ] (Mac) |
| Unindent | Decreases indentation level | Ctrl+ [ (Win, Linux), Cmd+ [ (Mac) |
| Reindent | Automatically adjusts indentation based on syntax | Edit -> 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.
Related reading
- How do I resolve Cannot find module error using Node.js?
- How do I retrieve an HTML element's actual width and height?
- How do I run a single test using Jest?
- How do I serve index.html in subfolders with S3/Cloudfront?
- How do I set distance between flexbox items?
- How do I set/unset a cookie with jQuery?
- How do I shake an Android device within the Android emulator to bring up the dev menu to debug my React Native app
- How do I store an array in localStorage?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.