HTML-CSS-JS Prettify
https://packagecontrol.io/packages/HTML-CSS-JS%20Prettify
HTML, CSS, JavaScript, JSON, React and Vue code formatter for Sublime Text 2 and 3 via node.js
Sublime Text 3
JS-beautify
Node.js download
About
This is a Sublime Text 2 and 3 plugin allowing you to format your HTML, CSS, JavaScript, JSON, React and Vue code. It uses a set of nice beautifier scripts made by Einar Lielmanis. The formatters are written in JavaScript, so you'll need something (node.js) to interpret JavaScript code outside the browser.
This will work with either HTML, CSS, JavaScript, JSON, React and Vue files.
Installation
First of all, be sure you have node.js installed in order to run the beautifier. After you've installed node.js, you will need to setup this plugin.
Each OS has a different Packages
folder required by Sublime Text. Open it via Preferences -> Browse Packages, and copy this repository contents to the Sublime-HTMLPrettify
folder there.
The shorter way of doing this is:
Through Sublime Package Manager
Ctrl+Shift+P
orCmd+Shift+P
in Linux/Windows/OS Xtype
install
, selectPackage Control: Install Package
type
prettify
, selectHTML-CSS-JS Prettify
Manually
Make sure you use the right Sublime Text folder. For example, on OS X, packages for version 2 are in ~/Library/Application\ Support/Sublime\ Text\ 2
, while version 3 is labeled ~/Library/Application\ Support/Sublime\ Text\ 3
.
These are for Sublime Text 3:
Mac
git clone https://github.com/victorporof/Sublime-HTMLPrettify.git ~/Library/Application\ Support/Sublime\ Text\ 3/Packages/Sublime-HTMLPrettify
Linux
git clone https://github.com/victorporof/Sublime-HTMLPrettify.git ~/.config/sublime-text-3/Packages/Sublime-HTMLPrettify
Windows
git clone https://github.com/victorporof/Sublime-HTMLPrettify.git %APPDATA%/Sublime\ Text\ 3/Packages/Sublime-HTMLPrettify
Usage
Tools -> Command Palette (Cmd+Shift+P
or Ctrl+Shift+P
) and type htmlprettify
.
– or –
Ctrl+Shift+H
(or Cmd+Shift+H
if you're on a Mac).
– or –
Right click in the current buffer and select HTML/CSS/JS Prettify
-> Prettify Code
.
– or –
Open a HTML, CSS, JavaScript, JSON, React or Vue file, pop out the console in Sublime Text from View -> Show Console, and type view.run_command("htmlprettify")
.
Writing commands in the console is ugly. Set up your own key combo for this, by going to Preferences -> Key Bindings - User, and adding a command in that array: { "keys": ["super+shift+h"], "command": "htmlprettify" }
. You can use any other command you want, thought most of them are already taken.
Oh noez, command not found!
If you get an error about Node.js not being found or similar, you don't have node
in the right path. Try setting the absolute path to node in HTMLPrettify.sublime-settings
.
Ctrl+Shift+P
orCmd+Shift+P
in Linux/Windows/OS Xtype
htmlprettify
, selectSet node Path
Simply using node
without specifying a path sometimes doesn't work :(
For example, on Linux the path could be in /home/<user>/.nvm/<node version>/bin/node
.
On Windows, the absolute path to node.exe must use forward slashes. Must include nodejs.exe, like so: C:/Program Files (x86)/Nodejs/node.exe
Be very careful on Linux!
Depending on your distribution and default package sources, apt-get install node
(for example) will not install node.js, contrary to all human common sense and popular belief. You want nodejs
instead. Best thing is to make it yourself from http://nodejs.org/#download.
Beautify Vue
To properly beautify Vue files, make sure the file syntax is recongnized by Sublime as either HTML, XML or Vue.
Beautify JSX
To properly beautify JSX files, make sure the file syntax is recongnized by Sublime as JavaScript and that e4x
is true
in your .jsbeautifyrc
.
Beautify on Save
To beautify your code when saving the document, set the format_on_save
setting to true
in HTMLPrettify.sublime-settings
:
Ctrl+Shift+P
orCmd+Shift+P
in Linux/Windows/OS Xtype
htmlprettify
, selectSet Plugin Options
Beautify while editing (Experimental / Sublime Text 3 only)
To beautify your code when while editing a document, set the format_while_editing
setting to true
in HTMLPrettify.sublime-settings
.
Auto-beautify when opening, focusing or bluring a file (Sublime Text 3 only)
To beautify your code when on any of these events, set the format_on_open
, format_on_focus
, format_on_focus_lost
settings to true
in HTMLPrettify.sublime-settings
.
Formatting selected text only
To stop beautifying only the selected text, set the format_selection_only
setting to false
in HTMLPrettify.sublime-settings
.
Saving to a temporary file before prettifying
Before prettifying, a copy of the the current editor's text contents are saved to a temporary file. This avoids piping the text directly to the prettifier, avoiding “filename or extension is too long” errors on Windows or any potential data lowss. To operate on the original document instead, set the save_to_temp_file_before_prettifying
setting to false
in HTMLPrettify.sublime-settings
.
Specifying which files are allowed to be prettified
To add different file extensions use allowed_file_extensions
or allowed_file_syntaxes
in HTMLPrettify.sublime-settings
, under the global_file_rules
setting.
If the use_editor_syntax
setting is false, then the following apply:
If the use_editor_syntax
setting is true, then the following apply:
Ignoring certain files
To add ignore rules use disallowed_file_patterns
in HTMLPrettify.sublime-settings
, under the global_file_rules
setting. If the file (including path) matches any of the regexp patterns defined in disallowed_file_patterns
it will not be beautified.
The following apply regardless of the use_editor_syntax
setting's value:
Ignoring certain blocks of code
Parts of code that shouldn't be formatted can be ignored with beautify preserve
and beautify ignore
directive comments. This allows you to tell the beautifier to preserve the formtatting of or completely ignore part of a file. The example inputs below will remain changed after beautification.
Use preserve
when the content is javascript, but you don't want it reformatted.
Use ignore
when the content is not parsable as javascript.
Using editor indentation settings to determine formatting style
To stop using the formatting style defined in the .jsbeautifyrc
config file regarding indentation size and whether or not to use tabs or spaces, in order to use sublime's settings instead, then set the use_editor_indentation
setting to true
in HTMLPrettify.sublime-settings
.
Using editor syntax for determining file type
To stop using editor syntax and instead use file extensions for determining file type, set the use_editor_syntax
setting to false
in HTMLPrettify.sublime-settings
.
Respecting .editorconfig files
These configuration files can be placed wherever a .jsbeautifyrc
could. To stop respecting .editorconfig
files, which override the prettifier's rules, set the use_editorconfig
setting to false
in HTMLPrettify.sublime-settings
. Note that use_editor_syntax
and use_editor_indentation
have precedence and will always override any other settings from any configuration file like .jsbeautifyrc
and .editorconfig
.
Using your own .jsbeautifyrc options
The plugin looks for a .jsbeautifyrc
file in the following directories:
The same directory as the source file you're prettifying.
The source file's parent directories.
Your home folder.
Your personal Sublime settings folder.
When one is found, it stops searching, and it uses those options along with the default ones.
And here's how a .jsbeautifyrc
file in your home folder could look like:
See documentation for JS, or CSS and HTML.
A few persistent options are always applied from a .jsbeautifyrc
file located in the same directory as the plugin, if not overwritten by your own .jsbeautifyrc
file. Those are defined here. You can safely add stuff to that json file if you want:
Ctrl+Shift+P
orCmd+Shift+P
in Linux/Windows/OS Xtype
htmlprettify
, selectSet Prettify Preferences
Thank you!
Last updated