YvesR
YvesR

Reputation: 6212

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

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 VS Code 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 VS Code detect the encoding and set it automatically when I open a file?

Upvotes: 91

Views: 139110

Answers (6)

Jairo Martínez
Jairo Martínez

Reputation: 525

2024 - Same issue working with T-SQL files.

From here: Manage encoding files by extension of filenames, VS Code can now handle encoding by adding this setting:

// ---- My settings.json ----
{
    "[asp]":{
      "files.encoding": "iso88591"
    },
    "[javascript]":{
      "files.encoding": "utf8"
    },
    "[html]":{
      "files.encoding": "utf8"
    }
}

Upvotes: 0

VonC
VonC

Reputation: 1323175

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.

Upvotes: 1

Peminator
Peminator

Reputation: 852

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

Upvotes: 6

VietDD
VietDD

Reputation: 1148

Add guide by image :

File >> Preferences >> Settings

File >> Preferences >> Settings

Enter autoGuessEncoding and make sure checkbox is checked

Enter autoGuessEncoding

Upvotes: 23

Wosi
Wosi

Reputation: 45163

  • 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 .

Upvotes: 31

SleepyFox89
SleepyFox89

Reputation: 1399

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.

Upvotes: 132

Related Questions