olivier Goossens Bara
olivier Goossens Bara

Reputation: 41

How to prevent Visual Studio from adding backslashes when pasting double quote contents

I've encountered unexpected behavior in Visual Studio 2022. When you paste text containing a double quotation mark (e.g. ");) to a text line that already has a double quotation mark on it, then Visual Studio will automatically add a backslash.

For example, if you have the code line:

string myVar = " Something

And if you want to paste "); at the end of it, then it will produce:

string myVar= " Something  \");

And this will trigger a syntax error of course.

When pasting the same text (");) to a line without a double quotation mark on it, the paste works as expected:

string myVar = Something ");

Does anyone know how to avoid this?

Upvotes: 4

Views: 1000

Answers (1)

megabytes
megabytes

Reputation: 185

Crazy annoying, isn't it? You can de-select the 'Fix text pasted into string literals' option in the Tools -> Options menu. Go to the Text Editor section and select the language you're working in (in this example, I'm using C#), then select "Advanced" and scroll down to the "Editor Help" section to find that option and de-select it. This should resolve your issue.

Here's a screenshot from a GitHub bug report about this feature, shared by Cyrus Najmabadi on 2024-02-01:

Screenshot of the VS2022 Options window showing the Editor Help section for C# Advanced settings, with the specific option in question highlighted via a red border

Upvotes: 7

Related Questions