2
<div>
This line needs to be indented and closing div tag should be in a new line</div>

<div>
  This result is what I want
</div>

In Sublime Text 2, after creating tags by pressing ctrl+shift+W and pressing "enter", I want the result be like above. Do I have to install plugin or am I missing something?

Sodbileg Gansukh
  • 1,730
  • 2
  • 13
  • 19

3 Answers3

5

Think you are trying to do the same as this issue - Auto-indent new line when hitting enter to expand one-line tag in HTML.

Below is my answer there.


Try adding the following in your user key bindings.

{ "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\n"}, "context":
    [
        { "key": "setting.auto_indent", "operator": "equal", "operand": true },
        { "key": "selector", "operator": "equal", "operand": "meta.scope.between-tag-pair", "match_all": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }
    ]
}
Community
  • 1
  • 1
skuroda
  • 19,514
  • 4
  • 50
  • 34
0

Try this key binding:

{ "keys": ["ctrl+shift+w"], "command": "reindent" , "args": {"single_line": false}}

Reference: Formatting HTML Code using Sublime Text 2.

Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

I think it is a more general solution if you install the Tag plugin. As stated Dardub on his answer, the Tag plugin is also available via package control.

If you choose the Tag plugin, the easiest way to use it is highlight the text to indent and then press the shortcut Ctrl+Shift+F.

If you insist on your Enter workflow just add this line to your key bindings:

{ "keys": ["enter"], "command": "tag_indent_document" }

But beware because it replaces the Enter default behavior.

A more conservative approach that avoids the need for text highlighting is to use a more specific binding:

{ "keys": ["super+shift+f"], "command": "tag_indent_document" }

And now to indent all the HTML/XML just press the shortcut Super+Shift+F.

References:

Community
  • 1
  • 1
Miguel
  • 566
  • 1
  • 5
  • 16