Force Visual Studio (2010) to save all files in UTF-8
Asked Answered
R

3

21

Is there any way I can force Visual Studio (2010) to save all files in UTF-8, always?

Repay answered 3/12, 2009 at 10:0 Comment(1)
This is possible with EditorConfig with charset = utf-8, see https://mcmap.net/q/126940/-utf-8-without-bomCortneycorty
B
10

I think it saves files in the current codepage. There's an option under Tools->Options->Environment->Documents that will make it save in unicode when it cannot save in current codepage. But I don't know if that helps...

Bloodstain answered 3/12, 2009 at 10:16 Comment(1)
It does not work,you have to find all files saved in gbk,then Manual tell vs2010 to save them in utf8.This stuff boring me a lot..Rinee
A
26

I do not know of a way to force it to save everything in UTF-8, but you can do so on a case-by-case basis. When you first save a document and the Save As... dialog appears, the Save button will actually be a drop-down button with two options. You want "Save with Encoding...", which will then present you the entire list of installed Windows encodings.

The encoding you really want is way down the bottom:

Unicode (UTF-8 without signature) - Codepage 65001

although if you want to save yourself a lot of pain, you will probably want to pick the option near the top:

Unicode (UTF-8 with signature) - Codepage 65001

The difference is that the latter option stick the UTF-8 signature (which is just the UTF-16 byte-order mark encoded in UTF-8). This is one of my pet peeves, as UTF-8 doesn't have multiple byte orders, so the BOM is redundant at best, and breaks all kinds of text processing tools at worst. MS uses it to "detect" UTF-8 automatically, since for single-byte character, UTF-8, ISO-8859-1, and CP-1252 are identical except for a sequence of 32 characters (0x80 - 0x9f) that MS basically made up.

If you only ever edit or process your files with Visual Studio or the .NET tools, then saving with signature will probably work fine. If you need to save files for use by other tools (batch files, SQL queries, PHP scripts, etc), the signature will cause problems, and you should save them without it. If you do this, you may want to enable the option (Under Tools -> Options -> Text Editor) to "Auto-detect UTF-8 encoding without signature", or else, right-click on the file and chose "Open With..." and select the editor option that says " editor with Encoding".

Amatory answered 21/2, 2011 at 17:5 Comment(2)
I think the BOM is included so that text editors can easily determine if a file should be treated as UTF-8 when only ASCII characters are present. So, strictly speaking, I don't think it's redundant. If you want to talk about redundant, I think UTF-32 is the way to go.Schnorkle
If you need to determine whether a file can be treated as UTF-8, there’s a very simple way: try to parse the file as UTF-8. The properties of UTF-8 are such that it is extremely unlikely that a non-UTF-8 file could also be valid UTF-8 (and of course, ASCII is a subset of UTF-8).Bolzano
B
10

I think it saves files in the current codepage. There's an option under Tools->Options->Environment->Documents that will make it save in unicode when it cannot save in current codepage. But I don't know if that helps...

Bloodstain answered 3/12, 2009 at 10:16 Comment(1)
It does not work,you have to find all files saved in gbk,then Manual tell vs2010 to save them in utf8.This stuff boring me a lot..Rinee
C
9

I think you want to try ForceUtf8(with BOM)/ ForceUtf8(without BOM) extenstion.

Just search UTF8 on VS extension gallery(Tools -> Extension and updates)

Claudicant answered 2/11, 2015 at 7:32 Comment(2)
When using this method is the conversion automated? or there are an option to convert manually each file?Differentiate
It will automatically convert on next file saveDaviddavida

© 2022 - 2024 — McMap. All rights reserved.