Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default encoding for source files in Visual Studio 2017?

It seems Visual Studio 2017 always saves new files as UTF8-BOM. It also seems this was not the case with earlier versions of Visual Studio, but I could not find any documentation.

Also there has been an option "Advanced Save Options\Encoding" which did allow to change the encoding of newly saved files which is missing in VS2017.

Questions:

  • Are all file types saved with UTF8-BOM encoding in VS2017?
  • Is it possible to configure the encoding for new files in VS2017?
  • Will VS2017 change the encoding of "old" files which don't have UTF8-BOM?
  • Is there any documentation about this topic?
like image 895
Manuel Avatar asked Apr 03 '18 06:04

Manuel


2 Answers

Also there has been an option "Advanced Save Options\Encoding" which did allow to change the encoding of newly saved files which is missing in VS2017.

This feature Already exists! You can save files with specific character encoding to support bi-directional languages. You can also specify an encoding when opening a file, so that Visual Studio displays the file correctly.

save a file with encoding

To save a file with encoding

  1. From the File menu, choose Save File As, and then click the drop-down button next to the Save button. The Advanced Save Options dialog box is displayed.
  2. Under Encoding, select the encoding to use for the file.
  3. Optionally, under Line endings, select the format for end-of-line characters.

Are all files types saved with UTF8-BOM encoding in VS2017

In my case, VS stores all the files with CodePage 1252 encoding.

Is it possible to configure the encoding for new files in VS2017

However, My Visual Studio version is 15.6.1 and some people have the same problem like yours in previous versions of 2017, but they said "We have fixed this issue and it's available in Visual Studio 2017 15.3"

If not working, for C++ projects Take a look at /utf-8 (Set Source and Executable character sets to UTF-8).

Will VS2017 change the encoding of "old" files which don't have UTF8-BOM

By default, Visual Studio detects a byte-order mark to determine if the source file is in an encoded Unicode format, for example, UTF-16 or UTF-8. If no byte-order mark is found, it assumes the source file is encoded using the current user code page, unless you have specified a code page by using /utf-8 or the /source-charset option. Some people encountered a problem which is came from .editorconfig file, as below:

root = true

[*]
indent_style = tab
indent_size = 4
tab_width = 4
trim_trailing_whitespace = true
insert_final_newline = true
charset = utf-8 

That final charset line is probable doing it... but I'm not asking for 'utf-8-with-bom'!

like image 172
Amirhossein Mehrvarzi Avatar answered Sep 23 '22 09:09

Amirhossein Mehrvarzi


You can use EditorConfig with the charset property to define encoding for source files in VS 2017.

like image 42
Sergey Vlasov Avatar answered Sep 23 '22 09:09

Sergey Vlasov