Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto indent nested HTML tags in VS Code

I'm trying out VS Code and I used Emmet to create a new HTML element with a class. I need to create another nested (child) HTML element inside the original element, but by default, VS Code will not indent for the new element, when you hit enter inside the original element tags. Like if you have <div class="main"></div> and you hit enter in between the div tags, you'll get -

<div class="main">
</div>

And then you need to manually go one line up, add tabs and indent for the new HTML element.

In Webstorm, hitting enter in between the parent tags automatically indents for the new child element.

Here are two GIFs which show what I mean.

VS Code -

enter image description here

Webstorm -

enter image description here

Is there an extension or some other trick that achieves this feature in VS Code?

like image 204
Manish Giri Avatar asked Jul 15 '17 07:07

Manish Giri


People also ask

How do you autocomplete HTML tags in Visual Studio code?

You can trigger suggestions at any time by pressing Ctrl+Space. You can also control which built-in code completion providers are active. Override these in your user or workspace settings if you prefer not to see the corresponding suggestions.

How do you indent multiple lines in Visual Studio code?

How do you change the indentation of multiple lines in VS Code? Select the lines you want to indent, and. use Ctrl + ] to indent them.

How do you indent HTML tags?

You can indent elements by moving them two spaces to the right. This will make your code more readable by other developers and shows the relationship between the child and parent HTML elements.


1 Answers

Download an HTML formatter Extension. 1th, download one of the below Extensions:

  • Beautify
  • JS-CSS-HTML Formatter

OR any other HTML formatter you want. 2th, in VS Code, go to one of the HTML files you are working. 3th, Press ALT + SHIFT + F then a pop out window appears. 4th select one of the suggested formatters. All done!

Whenever, you press ALT + SHIFT + F in an HTML file, it will be auto indented and beautified.

like image 69
A Farmanbar Avatar answered Nov 03 '22 04:11

A Farmanbar