Reputation: 11221
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:
What can I do to include quotation marks in my snippet?
Upvotes: 22
Views: 13848
Reputation: 429
Using @Wosi 's answer to create a snippet that creates a snippet template.
We covert this:
"snip": {
"prefix": "snip",
"body": [
""
],
"description": "Template to create a new snippet"
},
INTO:
"snip": {
"prefix": "snip",
"body": [
"\"$1\": { "
" \"prefix\": \"$1\", "
" \"body\": [ "
" \"$2\" "
" ], "
" \"description\": \"$3\" "
"}, "
],
"description": "Template to create a new snippet"
},
Upvotes: 0
Reputation: 538
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\">",
Upvotes: 0
Reputation: 1102
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.
Upvotes: 0
Reputation: 1664
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>
Upvotes: 0
Reputation: 19
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"
}
Upvotes: 1
Reputation: 502
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"
}
Upvotes: 0
Reputation: 45243
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"
Upvotes: 41