Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change encoding on a per file or per extension basis

I'm using Microsoft Visual Studio Express 2012 for Web. It seems that every file which I open with it gets encoded into UTF-8. For most files which are going to be web-facing, that's fine. However, I have files in my projects that are specifically for build purposes (e.g., .bat files), which must be encoded in ANSI.

Are there any configuration settings in VS to either designate on a per file or a per extension basis the encoding? Or, if not specify the encoding, at least disable the auto-conversion to UTF-8?

like image 412
merv Avatar asked Sep 05 '12 07:09

merv


People also ask

How do I change the encoding of a file in Visual Studio?

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. Under Encoding, select the encoding to use for the file.

What is the encoding of a file?

An encoding converts a sequence of code points to a sequence of bytes. An encoding is typically used when writing text to a file. To read it back in we have to know how it was encoded and decode it back into memory. A text encoding is basically a file format for text files.


2 Answers

Open the problematic file in Visual Studio and...

  • On the File menu, click Advanced Save Options.
  • In the Encoding dropdown, select Unicode (UTF-8 … or the encoding you require.
  • Click OK.

Also see: how to change source file encoding in csharp project (visual studio / msbuild machine)?

like image 143
Tbyang2004 Avatar answered Sep 28 '22 04:09

Tbyang2004


An option to handle the encoding of all files of a given extension on a per open basis can be configured in the Options dialog. See MSDN page on Options, Text Editor, File Extension.

Navigate to Tools > Options > Text Editor > File Extension.

For the bat extension, I selected Source Code (Text) Editor with Encoding. The with Encoding part means that the user will be given options as to what encoding to use when opening the file. The default in this mode is Auto-detect, which preserves the ANSI encoding, if that is what the file already uses. Otherwise, one can explicitly designate it for the individual file.

Unfortunately, it doesn't seem to remember the setting last used when opening a file, and will thus prompt for an encoding setting every time a file is opened.

like image 22
2 revs Avatar answered Sep 28 '22 05:09

2 revs