Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make VS Code create HTML file by default?

I want to create .HTML file whenever I choose New File, is this possible?

like image 376
mshwf Avatar asked Mar 08 '17 16:03

mshwf


People also ask

How do I get the default HTML code in VS code?

If you are using VS code then you can use this command shift + 1 and enter to auto-populate HTML Doctype in HTML or PHP.

Can you write HTML in VS code?

Visual Studio Code provides basic support for HTML programming out of the box. There is syntax highlighting, smart completions with IntelliSense, and customizable formatting. VS Code also includes great Emmet support.


2 Answers

In VS Code, my separate folders for different HTML projects are stored under an UNTITLED (WORKSPACE).

For a new HTML file, I simply right-click on any folder in (WORKSPACE), select New File and then name that file with an HTML extension. The file is completely empty with no HTML whatsoever. Then, because the Emmet plugin is built into VS Code, I simply type an ! (exclamation point) and press TAB or ENTER and the empty file is filed with a complete HTML5 boilerplate.

5 simple steps to create an HTML file in Visual Studio Code

  1. right-click on folder in (WORKSPACE)
  2. Select New File
  3. Name the file with an HTML extension
  4. Type ! (one exclamation point only)
  5. Press TAB or ENTER
like image 162
Argus Avatar answered Jan 03 '23 15:01

Argus


As far as I can see, there is no way to configure VS Code to do this out of the box so I wrote an extension to do it. You can download that extension from the marketplace.

It adds two types of commands:

  • editor.newFile.withCurrentLanguageMode will open a new file with the same language mode as the currently active file
  • editor.newFile.withLanguageMode.<languageId> will open a new file with specified id as the language mode. The language must be first added to the configuration and a keybinding set up

So to set up one command to open new html files and another to open markdown files, you can add settings like this:

settings.json:

"editor.newFile.languageModes": ["markdown", "html"]

keybindings.json:

{
    "key": "ctrl+shift+t 0",
    "command": "editor.newFile.withCurrentLanguageMode",
    "when": ""
},
{
    "key": "ctrl+shift+t 1",
    "command": "editor.newFile.withLanguageMode.markdown",
    "when": ""
},
{
    "key": "ctrl+shift+t 2",
    "command": "editor.newFile.withLanguageMode.html",
    "when": ""
}

Using these settings you would then press "ctrl+shift+t" then "0" to open a new file with the same language as whatever file you have active, "ctrl+shift+t" then "1" to open a new file with "markdown" as the language mode, and "ctrl+shift+t" then "2" to open a new file with "html" as the language mode. Of course, you can customize the keybindings however you want and set the languages to whatever you want.

like image 39
Llewey Avatar answered Jan 03 '23 14:01

Llewey