How to set HTML Auto Indent format on Sublime Text 3?

前端 未结 4 566
小鲜肉
小鲜肉 2020-12-08 13:25

I have a question while I\'m writing HTML code on Sublime Text 3. I just want to set auto indent format of HTML. For example, when I write p tag like under code, the indenta

相关标签:
4条回答
  • 2020-12-08 13:47

    One option is to type [command] + [shift] + [p] (or the equivalent) and then type 'indentation'. The top result should be 'Indendtation: Reindent Lines'. Press [enter] and it will format the document.

    Another option is to install the Emmet plugin (http://emmet.io/), which will provide not only better formatting, but also a myriad of other incredible features. To get the output you're looking for using Sublime Text 3 with the Emmet plugin requires just the following:

    p [tab][enter] Hello world!
    

    When you type p [tab] Emmet expands it to:

    <p></p>
    

    Pressing [enter] then further expands it to:

    <p>
    
    </p>
    

    With the cursor indented and on the line between the tags. Meaning that typing text results in:

    <p>
        Hello, world!
    </p>
    
    0 讨论(0)
  • 2020-12-08 13:50

    This is an adaptation of the above answer, but should be more complete.

    To be clear, this is to re-introduce previous auto-indent features when HTML files are open in Sublime Text. So when you finish a tag, it automatically indents for the next element.

    Windows Users

    Go to C:\Program Files\Sublime Text 3\Packages extract HTML.sublime-package as if it is a zip file to a directory.

    Open Miscellaneous.tmPreferences and copy this contents into the file

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>name</key>
        <string>Miscellaneous</string>
        <key>scope</key>
        <string>text.html</string>
        <key>settings</key>
        <dict>
            <key>decreaseIndentPattern</key>
                <string>(?x)
                ^\s*
                (&lt;/(?!html)
                  [A-Za-z0-9]+\b[^&gt;]*&gt;
                |--&gt;
                |&lt;\?(php)?\s+(else(if)?|end(if|for(each)?|while))
                |\}
                )</string>
            <key>batchDecreaseIndentPattern</key>
                <string>(?x)
                ^\s*
                (&lt;/(?!html)
                  [A-Za-z0-9]+\b[^&gt;]*&gt;
                |--&gt;
                |&lt;\?(php)?\s+(else(if)?|end(if|for(each)?|while))
                |\}
                )</string>
            <key>increaseIndentPattern</key>
                <string>(?x)
                ^\s*
                &lt;(?!\?|area|base|br|col|frame|hr|html|img|input|link|meta|param|[^&gt;]*/&gt;)
                  ([A-Za-z0-9]+)(?=\s|&gt;)\b[^&gt;]*&gt;(?!.*&lt;/\1&gt;)
                |&lt;!--(?!.*--&gt;)
                |&lt;\?php.+?\b(if|else(?:if)?|for(?:each)?|while)\b.*:(?!.*end\1)
                |\{[^}"']*$
                </string>
            <key>batchIncreaseIndentPattern</key>
                <string>(?x)
                ^\s*
                &lt;(?!\?|area|base|br|col|frame|hr|html|img|input|link|meta|param|[^&gt;]*/&gt;)
                  ([A-Za-z0-9]+)(?=\s|&gt;)\b[^&gt;]*&gt;(?!.*&lt;/\1&gt;)
                |&lt;!--(?!.*--&gt;)
                |&lt;\?php.+?\b(if|else(?:if)?|for(?:each)?|while)\b.*:(?!.*end\1)
                |\{[^}"']*$
                </string>
            <key>bracketIndentNextLinePattern</key>
             <string>&lt;!DOCTYPE(?!.*&gt;)</string>
        </dict>
    </dict>
    </plist>
    

    Then re-zip the file as HTML.sublime-package and replace the existing HTML.sublime-package with the one you just created.

    Close and open Sublime Text 3 and you're done!

    0 讨论(0)
  • 2020-12-08 14:04

    Create a Keybinding

    To auto indent on Sublime text 3 with a key bind try going to

    Preferences > Key Bindings - users

    And adding this code between the square brackets

    {"keys": ["alt+shift+f"], "command": "reindent", "args": {"single_line": false}}
    

    it sets shift + alt + f to be your full page auto indent.

    Source here

    Note: if this doesn't work correctly then you should convert your indentation to tabs. Also comments in your code can push your code to the wrong indentation level and may have to be moved manually.

    0 讨论(0)
  • 2020-12-08 14:09

    This was bugging me too, since this was a standard feature in Sublime Text 2, but somehow automatic indentation no longer worked in Sublime Text 3 for HTML files.

    My solution was to find the Miscellaneous.tmPreferences file from Sublime Text 2 (found under %AppData%/Roaming/Sublime Text 2/Packages/HTML) and copy those settings to the same file for ST3.

    Now package handling has been made more difficult for ST3, but luckily you can just add the files to your %AppData%/Roaming/Sublime Text 3/Packages folder and they overwrite default settings in the install directory. Just save this file as "%AppData%/Roaming/Sublime Text 3/Packages/HTML/Miscellaneous.tmPreferences" and auto indent works again like it did in ST2.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>name</key>
        <string>Miscellaneous</string>
        <key>scope</key>
        <string>text.html</string>
        <key>settings</key>
        <dict>
            <key>decreaseIndentPattern</key>
                <string>(?x)
                ^\s*
                (&lt;/(?!html)
                  [A-Za-z0-9]+\b[^&gt;]*&gt;
                |--&gt;
                |&lt;\?(php)?\s+(else(if)?|end(if|for(each)?|while))
                |\}
                )</string>
            <key>batchDecreaseIndentPattern</key>
                <string>(?x)
                ^\s*
                (&lt;/(?!html)
                  [A-Za-z0-9]+\b[^&gt;]*&gt;
                |--&gt;
                |&lt;\?(php)?\s+(else(if)?|end(if|for(each)?|while))
                |\}
                )</string>
            <key>increaseIndentPattern</key>
                <string>(?x)
                ^\s*
                &lt;(?!\?|area|base|br|col|frame|hr|html|img|input|link|meta|param|[^&gt;]*/&gt;)
                  ([A-Za-z0-9]+)(?=\s|&gt;)\b[^&gt;]*&gt;(?!.*&lt;/\1&gt;)
                |&lt;!--(?!.*--&gt;)
                |&lt;\?php.+?\b(if|else(?:if)?|for(?:each)?|while)\b.*:(?!.*end\1)
                |\{[^}"']*$
                </string>
            <key>batchIncreaseIndentPattern</key>
                <string>(?x)
                ^\s*
                &lt;(?!\?|area|base|br|col|frame|hr|html|img|input|link|meta|param|[^&gt;]*/&gt;)
                  ([A-Za-z0-9]+)(?=\s|&gt;)\b[^&gt;]*&gt;(?!.*&lt;/\1&gt;)
                |&lt;!--(?!.*--&gt;)
                |&lt;\?php.+?\b(if|else(?:if)?|for(?:each)?|while)\b.*:(?!.*end\1)
                |\{[^}"']*$
                </string>
            <key>bracketIndentNextLinePattern</key>
             <string>&lt;!DOCTYPE(?!.*&gt;)</string>
        </dict>
    </dict>
    </plist>
    
    0 讨论(0)
提交回复
热议问题