202

I have javascript files that I need them to be saved in UTF-8 (without BOM), every time I convert them to the correct format in Notepad++, they are reverted back to UTF-8 with BOM when I open them in Visual Studio. How can I stop VS2010 from doing that?

Another question, is UTF-8 without signature in Visual Studio the same as UTF-8 without BOM?

Signal15
  • 558
  • 5
  • 16
kabaros
  • 5,083
  • 2
  • 22
  • 35
  • 15
    a file saved with (utf-8 without BOM) is reverted to (utf-8 with BOM) .. this is the problem – kabaros Mar 24 '11 at 13:33
  • Possible duplicate of [Force Visual Studio (2010) to save all files in UTF-8](http://stackoverflow.com/questions/1838899/force-visual-studio-2010-to-save-all-files-in-utf-8) – Matt Sach Jul 17 '12 at 13:40
  • Solution to kabaros problem is simple - just save file with visual studio using uft-8 without BOM. Yes, it's possible - as Keavon noticed "UTF-8 Without Signature is near the very bottom of the list." Quite weird that utf-8 with signature is at the beginning and utf-8 without signature is almost at the end, but at least it solves this frustrating problem. – cyriel Jan 11 '15 at 19:29

12 Answers12

255

BOM or Byte Order Mark is sometimes quite annoying. Visual Studio does not change the file unless you save it (as Hans said).

And here is the solution to your problem: If you want to save a file with other encodings, select save as and extend the save button in file dialog and select "Save with encoding". Or if you want to get rid of this setting permanently, just open File menu and select "Advanced save options" and there you should select "UTF-8 without signature" (and that also answered your last question :). Yes "UTF-8 without signature" is same as without BOM.

Pang
  • 9,564
  • 146
  • 81
  • 122
Dave81
  • 2,975
  • 1
  • 16
  • 9
  • 16
    I was wondering if there's an option to enable this by default for all files. – mihai Mar 27 '12 at 08:34
  • 4
    It saved the file properly when I chose "Save with encoding", but I don't see "Advanced save options" in my File menu, no matter what kind of file I've got open in the editor. Hm.... – Chris Jaynes May 17 '12 at 15:17
  • What version of visual studio are you using? – Dave81 May 22 '12 at 11:34
  • 23
    @ChrisJaynes right-click the menu bar, in an open spot, then click customize... commands... (*) menubar: File ... [Add Command] ... add in the "Advanced Save Options" I put it right above "Save Selection" – Tracker1 Jul 09 '12 at 22:42
  • Thanks, UTF-8 without signature did the trick. I had this problem when working on a Python/Django project with Visual Studio. – Fredrik Stolpe Aug 14 '13 at 13:38
  • 4
    Well, I don't have the entry "UTF-8 without signature" ... what is the problem then? – Marco Klein Dec 28 '13 at 20:13
  • Just a note: I've noticed on my own machine that when shelving changes, then pulling them back down, this advanced save option does not persist through the shelving. They go back to UTF-8 With Signature – mvanella May 13 '14 at 18:29
  • 16
    Note: UTF-8 Without Signature is near the very bottom of the list. – Keavon Oct 19 '14 at 20:06
  • I don't see "Advanced Save Options" in VS2013. – Duderino9000 Jul 07 '15 at 23:32
  • 1
    In VS2017, advanced save options is gone. To add it back to the file menu, use `Tools->Customize->Commands`. See also https://developercommunity.visualstudio.com/content/problem/8290/file-advanced-save-options-option-is-missed.html – Roi Danton Oct 12 '17 at 11:02
  • 1
    @RoiDanton In VS2017, after adding the "Advanced Save Options" menu entry, it's disabled unless I have a file-open - so when using the dialog with a file open it's unclear if I'm only setting the encoding for only the currently-open file or all other files in future. – Dai Apr 24 '20 at 07:46
  • @Dai, in VS2019 the setting definitely only applies to the current file. – mkjeldsen Apr 17 '21 at 05:41
41

Now with pictures.

  1. Go to File -> Save As.

    File/Save As

  2. Then on Save button click on triangle and click Save with Encoding....

    Save with Encoding

  3. Click ok to overwrite the file then from list of encodings find UTF-8 Without signature -> Click OK.

    UTF-8 Without signature

Hope this saves you some time.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
28

I've created the Fix File Encoding extension that prevents Visual Studio 2010+ from adding BOM to UTF-8 files.

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
  • 1
    Thanks for that plugin! There should be some other way of specifying file types than using a regular expression in the settings matching file suffixes. Also, the plugin only acts on files when I save them, when I create a javascript, it initially has a BOM (3 bytes added). – Anders Lindén Jan 22 '14 at 09:28
  • Many thanks for your extension however FYI it doesn't work for csproj files (when manually opened and edited then saved). – Mahmoud Al-Qudsi Mar 21 '23 at 22:04
  • @MahmoudAl-Qudsi In VS 2022, with csproj in FFE options, manually opening and saving WPF csproj removes BOM on my machine. – Sergey Vlasov Mar 22 '23 at 17:50
  • I just checked and made sure to open the file (by clicking the project name once) and then save manually after making a change. My regex is set to `^` as a catch-all. Still no dice (Visual Studio 17.6.0 Preview 2.0) – Mahmoud Al-Qudsi Mar 30 '23 at 20:07
7

UTF-8 - "Save As" (Without Signature) Default - Request to include Default for UTF Without Signature

VS 2017 natively supports EditorConfig so a recommended solution is to add .editorconfig file to your code base and set charset => utf-8. Then once you save a file it will be saved as UTF-8 without BOM.

Dave Anderson
  • 11,836
  • 3
  • 58
  • 79
  • 1
    I'm afraid VS 2017 still doesn't support `charset` in .editorconfig https://github.com/editorconfig/editorconfig-visualstudio/issues/54#issue-231881648 – BlazingFrog Oct 27 '18 at 17:39
6

Unfortunately this does not work with csproj files. There is no "Advanced save option" and even though you have set it to "UTF-8 without signature" for a cs file, csproj files still are saved with BOM. If you use VSS it still complains about project files.

Wray
  • 61
  • 1
  • 1
  • Maybe you should move from VSS to SVN. I remember seeing a tool that can make the conversion for you. SVN is free and with a small price you can the visual SVN plugin in to your visual studio and use SVN directly from the UI. – Dave81 Oct 18 '12 at 07:37
  • 7
    "Maybe you should move from VSS to SVN.". What kind of a suggestion is that? Most of us working on enterprise software do not get to pick and choose our Source Control System. SVN has it owns issues which was what prompted the creation of git, because they were fed up with SVN. – Shiva Dec 05 '15 at 04:23
5

For Visual Studio Code do the following:

  1. From bottom right, select current encoding enter image description here
  2. From options, Select Save with enconding enter image description here
  3. From options, select UTF-8 enter image description here
Mahesh
  • 3,727
  • 1
  • 39
  • 49
4

Recently I found this tiny command-line tool which adds or removes the BOM on arbitary UTF-8 encoded files: UTF BOM Utils (new link at github)

Little drawback, you can download only the plain C++ source code. You have to create the makefile (with CMake, for example) and compile it by yourself, binaries are not provided on this page. However, for a software developer this should not be any issue.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
2

For vs2010 c++, there will be problems with UTF8 without BOM, when source files contain multi-byte characters(eg. Chinese).

Those characters will not be recognized correctly without BOM, and result in failed compling.

Martin Wang
  • 957
  • 14
  • 18
1

Even with Dave81's solution, Visual Studio 2015 Community was still reverting my file to UTF8-BOM every single time I save that html file.

When I created that html file, I right-clicked on the project and selected "Add" then added an HTML file.

By default, Visual studio will include a <meta charset="utf-8" /> tag in your HTML file.

Simply removing the tag then applying Dave81's solution made the problem go away for real this time.

It seems the Visual Studio parse your html file and when it sees that tag it converts the file to UTF8-BOM without any consideration to the original file format (UTF-8 without BOM).

I would have made a comment directly under Dave81's solution, but I didn't have enough points to do so...

phuclv
  • 37,963
  • 15
  • 156
  • 475
Jonny
  • 81
  • 6
1

It is recommended that there is no BOM as standard, but, when using multiple bytes such as [Korean or Chinese], a warning message is displayed when there is no BOM in Visual Studio.

Actually, I created a Korean String array, and the corresponding String array was broken at runtime.

When using Visual Studio, it is recommended to use UTF -8 including BOM according to MS warning message. (when using multi-byte characters)

1

To perform Advanced Saving on a .csproj file, you must first unload the project by right clicking the project and selecting Unload Project. This will allow you to save the .csproj file with a different encoding.

-2
UTF8Encoding utf8EmitBOM = new UTF8Encoding(false);
StreamWriter  sw = new StreamWriter(Path.Combine(sourcefilePath, fileName), false, utf8EmitBOM);

This code will create file in UTF-8 without BOM

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • the question is about encoding while saving a source code file in the IDE, not about setting the encoding while writing some file in C# code – phuclv Nov 29 '19 at 02:07