ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

HTMLBeautify

by rareyman ST2/ST3 Top 100

A plugin for Sublime Text that formats (indents) HTML source code. It makes code easier for humans to read.

Details

  • 2015.02.23.20.05.55
  • github.​com
  • github.​com
  • 2 years ago
  • 1 hour ago
  • 5 years ago

Installs

  • Total 308K
  • Win 171K
  • OS X 90K
  • Linux 47K
Mar 30 Mar 29 Mar 28 Mar 27 Mar 26 Mar 25 Mar 24 Mar 23 Mar 22 Mar 21 Mar 20 Mar 19 Mar 18 Mar 17 Mar 16 Mar 15 Mar 14 Mar 13 Mar 12 Mar 11 Mar 10 Mar 9 Mar 8 Mar 7 Mar 6 Mar 5 Mar 4 Mar 3 Mar 2 Mar 1 Feb 28 Feb 27 Feb 26 Feb 25 Feb 24 Feb 23 Feb 22 Feb 21 Feb 20 Feb 19 Feb 18 Feb 17 Feb 16 Feb 15 Feb 14 Feb 13
Windows 12 281 282 262 168 173 257 274 296 306 311 166 183 257 272 288 311 289 175 182 258 313 285 298 281 157 158 258 302 308 264 269 158 197 299 308 289 312 249 154 215 271 335 306 272 249
OS X 1 127 109 106 50 55 104 104 138 100 99 62 70 96 106 138 93 85 53 62 104 124 119 117 122 58 65 113 96 115 127 97 50 55 102 114 129 152 116 56 65 92 142 134 106 105
Linux 2 67 68 54 34 35 48 73 74 62 64 48 40 70 56 66 68 55 39 52 65 59 49 63 61 45 36 63 64 70 52 60 38 46 76 62 70 46 66 47 37 69 98 55 50 59

Readme

Source
raw.​githubusercontent.​com

HTMLBeautify v0.85

SublimeText (version 2 & 3)

  • (Inspired by fhtml.pl by John Watson)
  • by Ross A. Reyman
  • 20 February 2015
  • url: http://reyman.name/
  • e-mail: ross[at]reyman[dot]name

A plugin for Sublime Text , that formats (indents) HTML source code. It makes code easier for humans to read.


Key Commands Reverted Back to Original

Due to a conflict with other keymaps in Sublime Text, I am reverting the keymap back to the original setting:

  • Mac OS X: Command-Option-Shift-F
  • Windows: Control-Alt-Shift-F
  • Linux: Control-Alt-Shift-F

(To change this, see instructions later in this README…)

Notes

  • This script assumes an effort has been made by the user to expand tags to different lines. This script will not automatically expand minimized/compressed code—it will only try to “clean-up” code that needs to be re-indented
    • However, you can use multiple cursors (Find All) or use Replace on the beginning of a tag < to put every tag on a new line before applying beautify, which will help in this case.
  • Currently, this script chokes a bit with inline comments.

    • For example:

      <div class="something">

    • So, a workaround is to keep comments on their own lines:

      <div class="something">

    • (TODO: Fix this!)

  • This script uses \t characters to create indentation levels and spacing—ST appears to honor whether the user prefers spaces or tabs in ST settings and adjusts accordingly.

  • Use tag_pos_inline setting to define tags that might appear on one line.

  • Windows Users: You must restart Sublime Text to complete the installation.

Installation (Package Control)

If you have Package Control installed in Sublime Text:

  • Open the Command Palette (Tools > Command Palette…)
  • Search for and choose “Package Control: Install Package” (give it a few seconds to return a list of available packages)
  • Search for “HTMLBeautify” and install.
  • Windows users will need to restart Sublime Text to finish the installation.

Installation (Manual)

  • Download the zip, re-name resulting folder to: HTMLBeautify, then put the folder into your Sublime Text Packages folder.

Usage

  • Open a file containing HTML.
  • Select HTML code you want to beautify. (If no selection is made the plugin will run on the whole file.)
  • Use the appropriate key command to run HTMLBeautify—or use HTMLBeautify from the Edit menu.
  • You can test the script with HTMLBeautifyTest.html: an HTML file with wacky indenting so you to see how this script works.

Settings

You can configure which tags should be processed with this script:

  • ignored_tag_opening : What are the opening tags that tell the script to ignore HTMLBeautify formatting?
  • ignored_tag_closing : What are the closing tags that tell the script to resume HTMLBeautify formatting?

  • tag_indent : If one of these opening tags is encountered, the contents (next line) will be indented by one level.

  • tag_unindent : If one of these closing tags is encountered, the next line will be un-indented one level.

  • tag_unindent_line : If one of these closing tags is encountered, this line is un-indented one level.

  • tag_pos_inline : These are special “one line” tags that open and close on the same line, so indenting should be ignored.

  • remove_extraline : Set true to remove empty line.

Changing the Key Binding

You can create your own custom keymap (key command/macro) in your keymap file: Packages/User/Default[OS].sublime-keymap

{
    "keys": ["super+alt+shift+f"], //  create your own key command combination here!
    "command": "html_beautify", // command that executes html_beautify
    "context": [{
        // these options ensure that the command is executed in the right files/context
        "key": "selector",
        "operator": "equal",
        "operand": "text.html,text.html.twig,text.twig,source.html,source.html.twig,source.twig"
    }]
}

For more details: Sublime Text Unofficial Documentation: Key Bindings

Disclaimer

This script has been tested for basic HTML coding situations, but your mileage may vary—use with caution if using this in a production environment. (Please report bugs or contribute corrections to the script!) Although the script does not remove or modify code directly (it only attempts to adjust indentation levels), be sure to test this script throughly to make sure it works as expected! The author is not responsible for any bugs that might be introduced to your HTML. :)