auto-close HTML tags in Sublime Text

5

2

I'm using Sublime Text 2, and I'm sure there must be a way to make it auto-close XML/HTML tags, the way for example Notepad++ can.

Is there a way to have it add </div> immediately after I type <div>?

McGarnagle

Posted 2012-10-08T02:13:59.040

Reputation: 163

Answers

6

As zoli's answer states, you can use snippets/completions to auto complete tags as you start typing them. But there should be some built into sublime. For example, as you start to type <div>, you should see a drop down box with matching snippets. Hit Tab or Enter to pick one and arrow keys to go up or down in the list. For html tags, it will automatically adds the closing tag and puts the cursor in between them.

Another option is: use Edit > Tag > Close Tag. The hotkeys are displayed in the file menu. It automatically adds the closing tag if you already added an opening tag.

d_rail

Posted 2012-10-08T02:13:59.040

Reputation: 2 809

5

Sublime's built-in snippets differ for HTML and XML.

For HTML, you can type <div then Tab to autocomplete <div></div>.

For XML, though, it's different. You first type < then Tab to autocomplete to <p></p> with the p tags multiple-selected by default. So you can then type foo and have it autocomplete to <foo></foo>. Once you've named your element, you can press Tab to jump to the content of the element.

You can use Edit > Tag > Close Tag equally on both HTML and XML.

Dan Fabulich

Posted 2012-10-08T02:13:59.040

Reputation: 2 681

3

SublimeCodeIntel has pretty good completions for HTML. You can type a tag name (w/o the opening brace) and press Tab to complete both the opening and closing tag.

zoli

Posted 2012-10-08T02:13:59.040

Reputation: 243

0

Use the Emmet plugin.

To create a closed div, simply type div then press the Tab key.

user110241

Posted 2012-10-08T02:13:59.040

Reputation: 343