How do I set up code auto-formatting in Sublime 3?
Let’s say I pasted HTML code into Sublime Text. This code with the same tabulation, respectively, turns out to be a mess.
How to make the code format itself and take its normal form after a certain action?
& lt; form & gt;
& lt; div & gt;
& lt; div & gt; & lt; / div & gt;
& lt; div & gt;
& lt; div & gt;
& lt; img & gt;
& lt; / div & gt;
& lt; div & gt;
& lt; input & gt;
& lt; / div & gt;
& lt; / div & gt;
& lt; / div & gt;
& lt; / form & gt;
Answer 1, authority 100%
By standard means
Preferences → Key Bindings - User
If the file is empty, then insert the entire code.
If not, then add the object to the array as you would add it to JSON.
[
{"keys": ["ctrl + alt + l"], "command": "reindent", "args": {"single_line": false}}
]
This code allows, by pressing the combination CTRL + ALT + L , to format the code in the whole file or only inside the selection.
Using a third party plugin
There is a plugin Sublime-HTMLPrettify . It needs node.js installed to work.
Can read settings from .jsbeautifyrc file.
Installation :
- Press CTRL + SHIFT + P or CMD + SHIFT + < kbd>P on Linux / Windows / OS X
- Type
install, selectPackage Control: Install Package - Type
prettify, selectHTML-CSS-JS Prettify
Usage (any of the options)
- Select
Tools→Command Palette(CTRL + SHIFT + P or CMD + SHIFT + P ) and typehtmlprettify. - Press CTRL + SHIFT + H (or CMD + SHIFT + H if you have a Mac).
- Right click on the selection
HTML / CSS / JS Prettify→Prettify Code - Open your HTML, CSS or JavaScript file, open the Sublime Text console via
View→Show Consoleand typeview.run_command ("htmlprettify").
An example of working on your code snippet:
