Vscode: Auto detect ANSI encoding

Created on 31 May 2016  Â·  11Comments  Â·  Source: microsoft/vscode

  • VSCode Version:
    1.1.1
  • OS Version:
    Win 10

Steps to Reproduce:

  1. Create a new file in notepad, save it as ANSI (on my machine Ansi = Windows-1252)
  2. Open the file in vs code

Result: VSCode assumes UTF-8 and the file is corrupted. Sublime-text has no problem recognizing Windows-1252.

*duplicate feature-request file-explorer

Most helpful comment

Hi, a friend of mine found a workaround for this "ansi autodetect" problem:

  • Under "preferences / user settings" we find "files.encoding"
  • after that we choose "edit" to send it to right
  • manually we past the line 3 times, like below:
    "files.encoding": "utf8bom",
    "files.encoding": "utf8",
    "files.encoding":"windows1252"
  • ignore warnings about duplicate object key, save file, close vsCode, open again and voila, now I can open UTF8 or ANSI encoded files with no problem and no need to "reload with encode".
    We dont know why it works, but it just works fine on vsCode 1.8.1

Credits to my friend Marcos Cosmoski Filho.

All 11 comments

@swaner we do not have encoding detection so you need to either configure the encoding globally or per workspace or switch it from the status bar picker.

I support this issue. I just had a deployment using a script which had ® character inside it and I then edited with Code. And ooh, how the script failed and brought down the system. Simple editors handle ANSI files simply, and Code doesn't. That's a shame.

I attach a sample file to test. When I open this file with Code I see � instead of ®, it should leave ® alone. And I tried to change encoding to ANSI, and I couldn't find it among Change File Encoding. Windows 1252 should explicitly say it's ANSI. And Code should automatically understand it's such.

Character ®.txt

+1

Less powerful alternatives for handling this issue in the short run:

  • use the problems or errors counter to signal the presence of � or any unrecognised characters;
  • upon opening a file containing unrecognised characters, issue a question dialog suggesting Reopen with encoding or Continue. At least this will help prevent accidental overwriting;

Of course encoding detection would much more powerful.

It is better to add onOpenFile setEnconding to api extentions...

I have UTF8 files and I have Windows 1251 files. I can't change it globally to Windows 1251. So every time i open them i have to reopen them. Why, at least, not to save desired encoding per file? If once i opened it in Win1251, open it in Win1251 any time else, unless i reopen or resave it manually as other encoding?

And it has western codepages stuck at the top of codepages list rather than local codepages.

Hi,
For ANSI files, use Western (Windows 1252).

Hi, a friend of mine found a workaround for this "ansi autodetect" problem:

  • Under "preferences / user settings" we find "files.encoding"
  • after that we choose "edit" to send it to right
  • manually we past the line 3 times, like below:
    "files.encoding": "utf8bom",
    "files.encoding": "utf8",
    "files.encoding":"windows1252"
  • ignore warnings about duplicate object key, save file, close vsCode, open again and voila, now I can open UTF8 or ANSI encoded files with no problem and no need to "reload with encode".
    We dont know why it works, but it just works fine on vsCode 1.8.1

Credits to my friend Marcos Cosmoski Filho.

This setting has been added: files.autoGuessEncoding

Will hopefully be true as default soon; https://github.com/Microsoft/vscode/issues/23417

Why only CP1252? I want this for CP1251 too... Someone else will want this for another encoding and so on. I think that you should pick local encoding of this machine. What do you think of it?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chrisdias picture chrisdias  Â·  3Comments

biij5698 picture biij5698  Â·  3Comments

villiv picture villiv  Â·  3Comments

borekb picture borekb  Â·  3Comments

DovydasNavickas picture DovydasNavickas  Â·  3Comments