126

I am searching for a way to setup Visual Studio so it always saves my files in UTF-8.

I have only found options to set this project wide. Is there a way to set it Visual Studio wide?

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
Thomaschaaf
  • 17,847
  • 32
  • 94
  • 128

6 Answers6

76

Do you want the files to save as UTF-8 because you are using special characters that would be lost in ASCII encoding? If that's the case, then there is a VS2008 global setting in Tools > Options > Environment > Documents, named Save documents as Unicode when data cannot be saved in codepage. When this is enabled, VS2008 will save as Unicode if certain characters cannot be represented in the otherwise-default codepage.

Also, which files are not being saved as UTF-8? All of my .cs, .csproj, .sln, .config, .as*x, etc, all save as UTF-8 (with signature, the byte order marks), by default.

Samuel Meacham
  • 10,215
  • 7
  • 44
  • 50
  • 2
    Thanks for the setting tip. I had this problem with xaml files, which caused error during compilation – surfen Jul 27 '12 at 10:09
  • Hi,do you know why Report Designer Change encoding attribute to utf-8? http://stackoverflow.com/questions/30071846/editing-client-report-definition-rdlc-file-in-report-desinger-change-encoding-at – Mohammadreza May 06 '15 at 09:53
  • 2
    Still no better solution? My VS2015 keeps resetting the codepage after every crash, and with my electricity those happen pretty often, so sad, how Microsoft came to this, how they can make the best IDE and forget about default codepage setting, what a pity(( – Movsar Bekaev May 17 '16 at 11:32
  • 4
    When you set Unicode does it actually use UTF-8 or UTF-16? – binki Nov 08 '16 at 20:45
  • @binki To know the encoding you can open the same file in notepad++ and see the value detected in the Encoding menu. In my environment the files are saved as utf-8 with BOM. – jakubiszon Apr 09 '17 at 15:04
  • 17
    The problem is data COULD be saved in default system codepage. But other software cannot always read sources in my system codepage. Therefore i seek the way to force VS save all text files in UTF-8 by default. I have no idea why the heck someone wants to save text data in codepage other then UTF-8. – BlackOverlord Jul 05 '18 at 18:58
  • @BlackOverlord this is precisely my problem. Have you figured anything out in the meantime / would you mind making this a separate question? – Vinz Oct 30 '21 at 17:15
  • @Vinz I haven't. I guess the workaround is just not using any funky characters in your code, only plain ASCII. If you really need some Unicode string literals, most languages support escape sequences with character codes such as \uXXXX – BlackOverlord Aug 21 '22 at 00:11
34

What

It is possible with EditorConfig.

EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs.

This also includes file encoding.

EditorConfig is built-in Visual Studio 2017 by default, and I there were plugins available for versions as old as VS2012. Read more from EditorConfig Visual Studio Plugin page.

How

You can set up a EditorConfig configuration file high enough in your folder structure to span all your intended repos (up to your drive root should your files be really scattered everywhere) and configure the setting charset:

charset: set to latin1, utf-8, utf-8-bom, utf-16be or utf-16le to control the character set.

You can add filters and exceptions etc on every folder level or by file name/type should you wish for finer control.

Once configured then compatible IDEs should automatically do it's thing to make matching files comform to set rules. Note that Visual Studio does not automatically convert all your files but do its bit when you work with files in IDE (open and save).

What next

While you could have a Visual-studio-wide setup, I strongly suggest to still include an EditorConfig root to your solution version control, so that explicit settings are automatically synced to all team members as well. Your drive root editorconfig file can be the fallback should some project not have their own editorconfig files set up yet.

Imre Pühvel
  • 4,468
  • 1
  • 34
  • 49
9

I work with Windows7.

Control Panel - Region and Language - Administrative - Language for non-Unicode programs.

After I set "Change system locale" to English(United States). My default encoding of vs2010 change to Windows-1252. It was gb2312 before.

I created a new .cpp file for a C++ project, after checking in the new file to TFS the encoding show Windows-1252 from the properties page of the file.

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
user10959888
  • 99
  • 1
  • 2
  • In Windows 10 has one checkbox with label "Beta: Use Unicode UTF-8 to support world languages". When I check it, I can open files encoding with Windows-1252 on visual studio 2019. But I only find this configuration because this answer. Thanks! – Gean Ribeiro Jul 21 '20 at 21:31
6

It is an old topic, but I'll put here what helped me: Change Visual Studio default encoding

Visual Studio default encoding format is UTF-8, but it has built-in encoding formats for various languages, which can be selected according to your needs. Generally, UTF-8 can be kept, because programs are usually written in English, so compatibility is better. It runs in most language systems and does not cause exceptions because they can be identified.

You set the default encoding format in the "Advanced Save Options" dialog box in Visual Studio, it can be opened through the "File" menu; but sometimes, it does not show "Advanced Save Options" in the "File" menu and is displayed.

  1. Click "Tools", select "Customize" in the pop-up menu to open the "Customize" dialog box; select the "Commands" tab, click the drop-down list box to the right of the "Menu bar", and select "File" from the pop-up options; Click "Add Command", in the dialog box that opens, select "File" in the left list box, select "Advanced Save Options" in the right list box; click "OK", it is added to the" File "menu.

  2. Click "File", select "Advanced Save Options" in the pop-up menu. Open the "Advanced Save Options" dialog box, and the currently set encoding is "US-ASCII - Codepage 20127".

  3. Click the "Encoding" drop-down list box, drag the scroll bar to the top, and select "Unicode (UTF-8 with signature) - Codepage 65001".

  4. Click the "OK", then Visual Studio sets the default encoding format to UTF-8.

TsvetiZlateva
  • 121
  • 1
  • 6
  • This should really be the accepted answer. – Osyotr Feb 17 '23 at 17:38
  • Good article. However, in case that link changes or the site gets taken down over time, it would be helpful to include a gist of the article in your answer. – Vinz Mar 31 '23 at 18:28
  • 1
    It should be noted that in the majority of cases, rather than UTF-8 with signature, you should use UTF-8 without signature rather than UTF-8 with signature. There are specific cases where you might want that, but they are the exceptions, not the rule. Refer to http://www.unicode.org/versions/Unicode5.0.0/ch02.pdf, section 2.6 (or page 36, table 2-4) for the official guideline on that. – Nicolas Coombs May 31 '23 at 14:20
  • 1
    For me, this only updates the encoding of the file currently displayed. If I create a new file, the selected encoding is not used. – agbinfo Aug 30 '23 at 15:31
2

The Problem is Windows/Microsoft applications put byte order marks at the beginning of all your files when saving as non-ASCII types (UTF-8, etc.). Other applications often break when they read these odd Microsoft UTF-8 byte order encoding marks at the top of files.

A perfect example of this problem was triggering quirsksmode in old IE web browsers when encoding in UTF-8 as browsers often displayed XHTML web pages based on what encoding falls at the start of the page. It makes a mess when other applications view those UTF-8 Visual Studio pages.

I usually do not recommend Visual Studio Extensions, but I do this one to fix that issue:

Fix File Encoding: https://vlasovstudio.com/fix-file-encoding/

The FixFileEncoding above install REMOVES the byte order mark and forces VS to save ALL FILES without a Byte Order Mark signature in UTF-8 at the top of all .NET pages. After installing go to Tools > Option then choose "FixFileEncoding". It should allow you to set all saves as UTF-8 . Add "cshtml to the list of files to always save in UTF-8 without the byte order mark as so: ".(htm|html|cshtml)$)".

Now open one of your files in Visual Studio. To verify its saving as UTF-8 go to File > Save As, then under the Save button choose "Save With Encoding". It should choose "UNICODE (Save without Signature)" by default from the list of encodings. Now when you save that page it should always save as UTF-8 without byte order mark at the beginning of the file when saving in Visual Studio.

Stokely
  • 12,444
  • 2
  • 35
  • 23
1

I don't know of a global setting nither but you can try this:

  1. Save all Visual Studio templates in UTF-8
  2. Write a Visual Studio Macro/Addin that will listen to the DocumentSaved event and will save the file in UTF-8 format (if not already).
  3. Put a proxy on your source control that will make sure that new files are always UTF-8.
Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87
  • 2
    3. can be a bad idea, since as soon as you check a file in, it will determine that your file doesn't match the checked in file. Well, with subversion, at least. If it was possible to encode both the check and the check-in, then this wouldn't be an issue. – TamusJRoyce Aug 09 '11 at 18:02
  • Hi,do you know why Report Designer Change encoding attribute to utf-8? http://stackoverflow.com/questions/30071846/editing-client-report-definition-rdlc-file-in-report-desinger-change-encoding-at – Mohammadreza May 06 '15 at 09:59