Is there any way I can force Visual Studio (2010) to save all files in UTF-8, always?
-
This is possible with [EditorConfig](https://editorconfig.org/) with `charset = utf-8`, see https://stackoverflow.com/a/51716830/371 – Dave Anderson Aug 06 '18 at 23:24
3 Answers
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".

- 28,070
- 4
- 86
- 117
-
2I 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. – Andrew Theken Mar 27 '13 at 20:55
-
1If 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). – Václav Slavík Oct 04 '13 at 07:24
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...

- 117,245
- 29
- 183
- 222
-
1It 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.. – bronze man Feb 24 '14 at 09:35
I think you want to try ForceUtf8(with BOM)/ ForceUtf8(without BOM) extenstion.
Just search UTF8 on VS extension gallery(Tools -> Extension and updates)

- 101
- 1
- 1
-
When using this method is the conversion automated? or there are an option to convert manually each file? – xzegga Jun 14 '16 at 00:00
-