7

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

1
  • 1
    Added since question: files.defaultLanguage setting. Commented Jan 27, 2020 at 23:21

2 Answers 2

32

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.