79

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?

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
YvesR
  • 5,922
  • 6
  • 43
  • 70

5 Answers5

113

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.

SleepyFox89
  • 1,209
  • 1
  • 10
  • 12
  • 7
    Meanwhile this option is newly available, but default is false because it still do not work as well compared to e.g. Notepad++. To many false detection, but this will probably be better in the future. – YvesR Aug 18 '17 at 09:02
  • 1
    The path to the settings,json file is `%appdata%\Code\User\settings.json` – HackSlash Nov 05 '19 at 21:26
  • This resolved the error shown in my vs code. Thanks. – tarekahf May 05 '22 at 15:11
  • If anyone comes here looking for an answer, this option still doesn't correctly guess file encoding (assumes windows 1250 is utf8 and messes up the content) – Richard Mišenčík Sep 27 '22 at 13:25
29
  • 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 .

Wosi
  • 41,986
  • 17
  • 75
  • 82
  • 2
    Thank you for the info. I did find this setting before but thought it is just a default. How does the editor determine a proper encoding information? So this settings reverse the problem that I can destroy utf8 files saving them with windows-1252, no? – YvesR Jul 25 '16 at 11:50
  • This doesn't answer the question, it will always open files using windows-1252 encoding. He asked for something automatically. – Marcelo Agimóvel Jan 08 '18 at 12:59
  • 1
    Update: I did work with auto guessing a while and turned it off as there to many wrong guessing. Wonder how e.g. Notepad++ does it so well and having problem doing this in such a great editor like VScode. – YvesR Jun 04 '18 at 11:25
  • 2
    Citing from https://github.com/Microsoft/vscode/pull/21416: `One thing to keep in mind is that currently we only allow the first 512 bytes of the file to be used when detecting the encoding. This might cause some encodings to not get detected properly.` This explains the bad recognition behavior and why it is turned off by default. – whY Mar 07 '19 at 09:55
  • You can create a WorkSpace in Visual studio code and set the default encoding and the VSCode will handle with the Encoding process! – Agostinho Neto May 04 '20 at 12:44
14

Add guide by image :

File >> Preferences >> Settings

File >> Preferences >> Settings

Enter autoGuessEncoding and make sure checkbox is checked

Enter autoGuessEncoding

VietDD
  • 1,048
  • 2
  • 12
  • 12
  • 1
    This is months later a working feature now, yes. Thanks for update. – YvesR Sep 25 '20 at 11:13
  • for me it does not work well, i need to tell 2 basic settings, windows1250 (some old code) or utf8 (new code). so i aet default to 1250 and enabled guess believing the utf8 characters should have been recognized even without bom, but is not – Peminator Jul 26 '21 at 07:09
4

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

Peminator
  • 783
  • 1
  • 9
  • 24
  • absoluteley, the guessing is horrible, we use a lot of mixed repos with a lot of utf8 bit also iso-8859-1 and the iso-8859-1 mostly open as iso-8859-7 or whatever is wrong. it should be possible to put a comment in source code of the file like or so, that let vs code open the file as indiated instead of guess. – Raffael Meier Feb 13 '22 at 11:33
1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250