How to setup Visual Studio Code to detect and set the correct encoding on file open

EncodingVisual Studio-Code

Encoding Problem Overview


I recently started to use Visual Studio Code on Server Systems where I did not have Studio IDE installed. I like it very much but I'm running into a problem.

When I open a file (used Notepad++ before) the editor detects the encoding and sets it for me. I have many files on windows servers that are still with windows-1252 but vscode just uses UTF-8 by default.

I know I can reopen with encoding Western (Windows 1252) but I often forget it and I have sometimes destroyed some content while saving it.

So I did not find any parameter yet, is there a way to make vscode detect the encoding and set it automatically when I open a file?

Encoding Solutions


Solution 1 - Encoding

To allow Visual Studio Code to automatically detect the encoding of a file, you can set "files.autoGuessEncoding":true (in the settings.json configuration file).

https://github.com/Microsoft/vscode/pull/21416

This obviously requires an updated verison of the application compared to when the question was originally asked.

Solution 2 - Encoding

  • Go to File-> Preferences -> User Settings
  • Add (or update) the entry "files.encoding": "windows1252" to the right editor window and save

Now VSCode opens all text files using windows-1252 when there is no proper encoding information set.

EDIT: In 2017's June release the files.autoGuessEncoding setting was introduced. When enabled it will guess the file's encoding as good as possible. Its default value is false .

Solution 3 - Encoding

Add guide by image :

File >> Preferences >> Settings

File >> Preferences >> Settings

Enter autoGuessEncoding and make sure checkbox is checked

Enter autoGuessEncoding

Solution 4 - Encoding

beware, auto guessing in vscode still does not work as expected, the guessing, is VERY inaccurate, and does still open as guessed encoding, even when the guess library returns also the confidence score being low - they use jschardet (https://www.npmjs.com/package/jschardet)

if the score of guess is not close to 100%, it simply should rather open in "files.encoding" instead of guessed encoding, but that does not happen, vscode authors should make better use of the confidence score jschardet returns

i open either utf-8 which guesses ok, and second encoding i use is windows-1250, which in 99% cases detects wrong as some other windows-... encoding or even iso-8859-... and such... cry daily at the computer having to experience this

tuning up the confidence checking and fallback to default encoding would do, it needs someone skilled to check their source and offer them a fix

Solution 5 - Encoding

From Peminator's answer:

> Beware: auto guessing in VSCode still does not work as expected, the guessing, is VERY inaccurate,

This should slightly improve with VSCode 1.67 (Apr. 2022) with (insider released):

> ## Allow to set files.encoding as language specific setting for files on startup > > - we now detect changes to the editor language and there is almost always a transition from plaintext in the beginning until languages are resolved to a target language > - if we detect that the new language has a configured files.encoding override > - and that encoding is different from the current encoding > - and the editor is not dirty or in save conflict resolution >:
> we reopen the file with the configured encoding > > Unfortunately I cannot tell apart this from happening vs. the user changing the language from the editor status bar. > > So if the user changes language mode to bat from to ps1 and ps1 has a configured encoding, the encoding will change.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYvesRView Question on Stackoverflow
Solution 1 - EncodingSleepyFox89View Answer on Stackoverflow
Solution 2 - EncodingWosiView Answer on Stackoverflow
Solution 3 - EncodingVietDDView Answer on Stackoverflow
Solution 4 - EncodingPeminatorView Answer on Stackoverflow
Solution 5 - EncodingVonCView Answer on Stackoverflow