22

I have some code with quotation marks that I want to use as a snippet. Unfortunately, JSON requires me to use them in "body" array in my settings. I tried to use different types of quotation marks, but Visual Studio highlights it in red: enter image description here

What can I do to include quotation marks in my snippet?

Piotrek
  • 10,919
  • 18
  • 73
  • 136

6 Answers6

38

Use \ to escape the double quotes. A sample snippet looks like this:

"My quoted snippet": {
    "prefix": "quot",
    "body": [
        "Hello \"User\""
    ],
    "description": "Print snippets with quotes"
}

When you execute this snippet it will print: Hello "User"

Wosi
  • 41,986
  • 17
  • 75
  • 82
  • 2
    This doesn't work: `Invalid characters in string. Control characters must be escaped` – Toni Leigh Sep 11 '19 at 11:49
  • 1
    It doesn't work for me too. With other symbols to escape (for example $) \\ (double back slashes) help. But with " even \\ doesn't help. – Zvezdochka Aug 03 '20 at 09:02
  • Trying to translate some coffescript from Atom (which does handle quotes and double quotes perfectly). This trick *does* work for me at least in this example: `"echo \"
    \";",`
    – cdsaenz Mar 23 '21 at 15:42
0

I was hoping there was a online converter available for this purpose to convert the code to correctly encoded code, but seems noone did that as of yet...

Remember that json also needs escaping with \ so you might have to \ for it to work correctly, an example from #42669459 has this sollution:

What to encode: ${code}

Sollution:

"Return Code With Squirly And Dollar": {
    "prefix": "code_snippet",
    "body" : [
        "\\${code\\}"
    ],
    "description": "Code Snippet"
}
Kim Steinhaug
  • 478
  • 3
  • 13
0

Use can simply use \ (backward slash) if you want to use double quotes or any other thing in json

Example :

"print statement":{
    "prefix": "print",
    "body": "print(\"$1\")",
    "description": "print anything in python"
}
0

My environment is Visual Studio 2022.

I wanted to create a snippet that produces the following line of code:

_logger.LogInformation($"")

The following worked for me

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>logger information</Title>
      <Shortcut>loginfo</Shortcut>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[_logger.LogInformation($$" ");]]>
      </Code>
      <Imports>
        <Import>
         
        </Import>
      </Imports>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Sau001
  • 1,451
  • 1
  • 18
  • 25
0

I don't know if you used tabs in your real snippet, but if yes, this answer fixed my issue.
You can replace tabs characters by spaces, or maybe use \t instead.

Sebastien
  • 1,014
  • 9
  • 29
0

Maybe this will help someone, I found that I getting an "invalid escape character" message but the problem was with a path rather than the double quotes themselves. Here's what I ended up doing and it works fine.

"body": "<cfdump var=\"#arguments#\" output=\"C:\\webmx\\www\\myDump.html\" format=\"html\">",
ChipnCharge
  • 508
  • 5
  • 7