3

I am looking for the shortcut keys that I should use to autocomplete the structure of the HTML.

So for instance, when I type html and press a few keys the following structure should appear:

<!DOCTYPE html>
<html>
  <head>
    <title> </title>
  </head>
  <body>

  </body>
</html>

Note: I am using a MAC

Illep
  • 16,375
  • 46
  • 171
  • 302

5 Answers5

11

"!" After typing, press the "TAB" key on the keyboard.

Dinuka Ekanayake
  • 614
  • 8
  • 11
1

Perhaps you should look into templates for your IDE, for example in Sublime Text you have snippets which I would recommend looking into.

However, there is another workflow that you are able to use using a toolkit such as Emmet. The feature that you specifically want in Emmet is called "Expand Abbreviation". Essentially, you type out "CSS like" strings, put your caret at the end of the string and hit tab and Emmet will parse it into HTML. For example, the following string:

!doctype>html>(head>title)+(body)

will compile to the above (with a trailing closing tag of </!doctype> which is a bit awkward. However Emmet is quite clever and we can go one step further.. so expanding:

html:5

Will actually give you

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

</body>
</html>

Which is pretty much what you were looking for! And if you want to take it another step further, Emmet also supports expanding ! which is an alias for html:5. Only one character.

Check out Emmet's cheat sheet for more little bits.

bitten
  • 2,463
  • 2
  • 25
  • 44
0

In TextMate these are called snippets and they can easily be assigned a tab completion shortcut;

Select Bundles » Edit Bundles… in the TextMate 2 menu.

  1. Select the bundle you want to add a snippet to, e.g. HTML. Press Cmd-N to create a new bundle item, and select Snippet.

  2. Select Menu Actions

  3. Press Command + N to create a new item, select snippet.

  4. In the text area copy and paste your template.

  5. On the drawer there's a field for "Tab Trigger", enter "html" there.

Snippet up to this point

  1. Save.

Now when you type "html" and press tab, your snippet will be inserted.

More info about creating a snippet.

Note: I've added "$0" in between your body tags. This is a tab stop and your cursor would appear at the $0. You can read more about advanced Snippet features here.


In My Humble Opinion;

html doesn't tab complete already because there's no good way to anticipate what you would want in your html template.

Sure, Emmet (as @bitten suggests in his answer) gives you something, but html:5 does too much; HTML5 does not mandate a html, head, or body tag. See this Stack post about HTML5's optional tags. Please see bitten's comment for some warranted concerns with this approach.

That said, I do use Emmet and it is worth investigating.

As for just inserting the doctype for HTML5 in TextMate, just type doctype and Tab.

Community
  • 1
  • 1
Graham P Heath
  • 7,009
  • 3
  • 31
  • 45
  • 1
    Regarding omitting the `head` and `body` tags, it's an interesting point to make. On one hand it makes sense, although you risk breaking compatibility with older versions of IE. Also another issue is incompatibility with current scripts, many async loading scripts and libraries use these tags to append their `script`s onto and some browsers do not auto fill the DOM for you (like we know most popular browsers do). I would recommend it if you were sure of compatibility with your project. – bitten Jun 10 '16 at 10:04
0

click ! and after tab and you'll get the structure.

0

In PyCharm while in an index.html I typed Exclamation Mark "!", then pressed TAB and get an HTML boilerplate code block inserted.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 03 '23 at 04:41