Change VSCode Default Language for new files? - visual-studio-code

Is there a way in Visual Studio Code to change the default language that is used for new files?
By default if you open a new file, it's set for "Plain Text", I want this to be "HTML" instead.
I am often copy pasting HTML into VSC, editing a bit, then copying it back to a CMS I am using (the CMS editor is horrible). I don't want to save the code on my computer at all, just edit it a bit with HTML syntax highlighting, but I want that to be the default.

You can now set the default language at either the user or workspace settings level using files.defaultLanguage:
"files.defaultLanguage": "html"
This can be done as a one off by changing the language mode:
F1 to launch command palette
Type lang, enter
Type html, enter

Now you can do this change. Check out
"files.defaultLanguage" in settings.

With v1.42 you can either set
"files.defaultLanguage": "${activeEditorLanguage}"
and when you open a new untitled file with an html file as the last active file, the new file will automatically be assigned a languageMode of HTML.
Alternatively, pasting any html code into a new untitled file from a vscode editor will automatically be detected and the languageMode set to HTML. Unless you have specifically set
"files.defaultLanguage": "plaintext"
then the languageMode will not be automatically detected as of v1.43.
Also see https://stackoverflow.com/a/68596936/836330 for a preview language detection feature in vscode 1.59.

A slightly quicker way to accomplish what Daniel had answered:
Press Ctrl+K, then M
Type html, then press Enter

I had a similar issue with VS Code. I wanted to default to Python when opening new files.
Solution: Click on File > Preferences > Settings. In the search area, Type: "files.defaultLanguage". Then fill in the language of your choice.

Perhaps you could create a scratch file with the .html extension? Open that when you need to do some copy/paste editing.

Related

Is there a way to open any file as text in VS Code?

VS Code is currently my favourite text editor, except for Jupyter notebooks, which are opened as interactive python environments (let's call this 'rendered').
By itself it is nice that this is possible, but not I can live with opening them in a browser if VS Code would not render them.
There used to be a setting to avoid this behaviour ("jupyter.useNotebookEditor": false) but currently my installation does not recognise this setting anymore.
Since there are more file types that can be opened as text/source, or rendered form (e.g. html, md, svg, uncompressed pdf, etc.) and both forms can be usefull, I would be very excited to learn how to switch between rendered and source, preferably without switching global settings.
Is there a way to do this? I would hope for:
a mechanism that does not depend on (or can be overwritten by) the current jupyter extension
a mechanism that works for any file type (nice-to-have)
Are you just looking for a way to view/edit the raw json?
You can right click the file in the Explorer -> open with... -> text editor.
Or use "Reopen editor with text editor" from the command Palette (ctrl+shift+p)

Disable onsave formating for .ejs files in visual studio code

I'm working with ejs files, but in order to reuse some code, I'm using the includes feature. Given that some opening/closing html tags are placed in other files, after I save my changes, something (I don't know if prettier extension or VS code editor) is including the closing tags into my code, causing several problems when I running it.
An other solution is to use a .prettierignore file and put in :
*.ejs
in your settings.json you should add these settings.
just instead of "[css]" type what you want. (the format of the file)
and just please ignore line 2 :) that's not related to this answer.
Edit
as #aegatlin said use this: "[html]".(if "[ejs]" didn't work for you)
I don't use EJS myself, but after playing around with it in VSCode, I noticed that my .ejs files were being treated as HTML files. You can see how your VSCode is interpreting the file by looking in the bottom right corner of the editor. You could search for EJS extensions as well.
You likely have the "Editor: format on save" option enabled. To disable that setting, go to Preferences, and in the search bar type "format on save". Find the setting. Uncheck the box. That should fix the problem.
If, as you mentioned, your closing HTML tags are in other files, then you have invalid HTML and the formatter (both Prettier's and the default one) will autocomplete the closing tag. (I would wager EJS also wouldn't like the lack of closing tags, but since I don't use it I'm not so sure, maybe it's fine.)
Zulhilmi Zainudin has the solution
https://medium.com/#zulhhandyplast/how-to-disable-vs-code-formatonsave-for-specific-file-extensions-c60e8f254243
In vscode setting file, associate ejs extentions files to a « language ». Then you can specify different rules for this that language :
.vscode/settings.json file content :
{
"files.associations": {
"*.ejs": "ejs" // this create the language « ejs » which refers to any .ejs file
},
"[ejs]": { // custom settings for the language « ejs »
"editor.formatOnSave": false
}
}

CSS Files Opening Up as Plain Text in VS Code

For some reason, whenever I create a new file and give it a css extension, the language mode is set to plain text. That is not the usual behavior and I don't know why it is acting that way.
Does anyone know how to fix that?
Thanks.
Click to the language at the right bottom bar of VSCode
Select Configure File Association for '.css'
Find CSS and select it.
You could run into problem cannot finding CSS in the last step I mentioned above, then here might be the fix for it btw:
Press Ctrl + , to open the Settings window.
Click Open Settings (JSON)
Find a line where there is
"files.associations": {
"*.css": "Plain Text"
}
and delete it, make sure the json file still in a correct format after deleting (no missing or extra comma ,).

Alternative way of creating new file in VS Code?

When creating new file in VS Code, it just opens a blank empty file right away.
Is there a way to create a specific type of new file, like in Notepad++ you can choose the language for the file. In VS Code you get to choose the file extension only when saving the file for the first time.
It'd be a bit faster to create a new, for example, JavaScript file instead of creating empty file and saving it as .js file and then writing to it.
So far I've created files with basic Notepad (with the "show file extensions" option enabled from File Explorer) and when naming the new file, writing the extension at the end instead of .txt
You could just open the integrated terminal CTRL+`` and touch your file.
The keyboard shortcuts mentioned by #CountingStuff in this comment worked.
Here are the step-by-step instructions:
Create a new file:
Ctrl+N
Change the syntax language:
Ctrl+K M
In the drop down that appears, auto-complete or manually choose a language option.
The vscode documentation for this can be found here:
https://code.visualstudio.com/docs/languages/overview#_language-id
I've remapped my keyboard shortcuts so that CTRL+N triggers the "File > New File" menu item. Now, when I press CTRL+N it adds a new entry in the file explorer and I can add a new file from there. I can also utilise VSCode's file explorer creation mechanism to create files in subfolders by pre-fixing the file with the directory structure, so typing src\index.js will create a new file named index.js in the src directory.
Reference: https://www.youtube.com/watch?v=2QBYlfSQA6s

Unable to create a file with foreign language characters

I get the following error, when trying to save a properties file (containing name/value pairs) with foreign language characters. How do I set the encoding?
Save could not be completed. Some characters cannot be mapped using
"Cp1252" character encoding. Either change the encoding or remove the
characters which are not supported by the "Cp1252" character encoding.
I presume its eclipse:
Go to Windows Menu –> Preferences –> General (expand it) –> Workspace (click on it)
Look for a box “Text File Encoding”. Default will be “Cp1252″.
Change radio to select other and select “UTF-8″ from combo box.
That looks like Eclipse.
Cut the whole contents of the file into the clipboard.
Save the (now empty) properties file.
Change the encoding settings of the file (Alt+Enter) to be UTF-8 or whatever you need.
Paste the clipboard into the editor.
Save the editor.
Assuming it is in Eclipse, easiest way is to choose Save as UTF-8 option present in the error dialog box.
The other answers work well too.
Best possible solution for this , which I always do .Is to go the respective file in your workspace open it in notepad++ and make the desired changes. Once done , save the file.
Your eclipse will ask you to update the code with the changes . Click yes.
And you are all good to go now.
Fist copy the file and delete that file and then cleat your code in the eclipse after
create file with same content and rewrite the code it will resolve.