204

Is there any way to change the encoding of a file? For example UTF-8 to ISO 8859-1?

Setting Example Sublime Text:

"default_encoding": "UTF-8"
Gama11
  • 31,714
  • 9
  • 78
  • 100
Fernando Tholl
  • 2,587
  • 2
  • 16
  • 14

3 Answers3

374

So here's how to do that:

In the bottom bar of VSCode, you'll see the label UTF-8. Click it. A popup opens. Click Save with encoding. You can now pick a new encoding for that file.

Alternatively, you can change the setting globally in Workspace/User settings using the setting "files.encoding": "utf8". If using the graphical settings page in VSCode, simply search for encoding. Do note however that this only applies to newly created files.

DarkNeuron
  • 7,981
  • 2
  • 43
  • 48
  • 11
    "files.encoding":"utf8" will create files without BOM (which is mostly the better decision). If you want UTF8 files with BOM, the setting is "files.encoding": "utf8bom" – ESP32 Jun 21 '17 at 07:33
  • @Gerfried "If you want UTF8 files with BOM, the setting is `"files.encoding": "utf8bom"`" - it doesn't work. See https://github.com/Microsoft/vscode/issues/47089 – john c. j. Apr 04 '18 at 21:11
  • 8
    please avoid using utf8bom, you not need it – sherpya Jun 05 '18 at 14:57
  • 2
    @sherpya except when (for some annoying program) you do... -.- – RaptorX Jun 29 '21 at 23:00
  • @sherpya I agree. Yet sometimes you do, for example to make the Delphi compiler recognize your UTF-8 files ‍♂️ – Marcus Mangelsdorf Mar 11 '22 at 16:36
  • VS Code told me my file was encoded in UTF-8, but a program reading that file didn't agree with that assessment. I had to tell VS Code to save it with another encoding first, and save it back to the UTF-8 encoding afterwards for that other program to agree it was UTF-8. – MyNameIsTrez Mar 15 '22 at 18:48
  • @sherpya The popular Chocolatey package manager says "PowerShell scripts need to be saved in UTF-8 with BOM. PowerShell ... needs a BOM in order to recognize scripts as UTF-8." https://docs.chocolatey.org/en-us/create/create-packages#character-encoding – Andrew D. Bond Sep 02 '22 at 08:27
42

Apart from the settings explained in the answer by @DarkNeuron:

"files.encoding": "any encoding"

you can also specify settings for a specific language like so:

"[language id]": {
  "files.encoding": "any encoding"
}

For example, I use this when I need to edit PowerShell files previously created with ISE (which are created in ANSI format):

"[powershell]": {
    "files.encoding": "windows1252"
}

You can get a list of identifiers of well-known languages here.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • Hi, I tried to use this setting to change encoding for c files to cp850 (is the best one for Spanish language) but when I create a new file nothing changes, is always setting to utf-8 by default. I tried with different languages and encodings but it still putting the same encode – Rafael Hernández Marrero Dec 18 '21 at 14:32
4

The existing answers show a possible solution for single files or file types. However, you can define the charset standard in VS Code by following this path:

File > Preferences > Settings > Encoding > Choose your option

This will define a character set as default. Besides that, you can always change the encoding in the lower right corner of the editor (blue symbol line) for the current project.