Visual Studio Code snippet invalid control character
Asked Answered
F

1

14

I have the following user snippet:

{
/*
    // Place your snippets for JavaScript React here. Each snippet is defined under a snippet name and has a prefix, body and 
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
    // same ids are connected.
    // Example:
    "Print to console": {
        "prefix": "log",
        "body": [
            "console.log('$1');",
            "$2"
        ],
        "description": "Log output to console"
    }
*/
    "Small comment": {
        "prefix": "//=",
        "body": [
            "// ===================================",
            "// ${1:COMMENT}",
            "// ==================================="
        ],
        "description": "Small comment"
    }
}

I get the following error on the second body line:

Invalid characters in string. Control characters must be escaped.

I thought that ${1:somestring} was a valid placeholder. What am I doing wrong in constructing this snippet?

Fairly answered 21/9, 2017 at 13:23 Comment(8)
It must be something else. I pasted your snippet into my javascript.json file (for snippets) and it worked perfectly. Is this a javascript snippet? And what file do you have it in?Ephraimite
I've updated the questions with the entire contents of the file. Yes it is Javascript. I'm wondering if I have to move the snippet outside of the object brackets.Fairly
You have it correctly within the outer object brackets. It really should work. I wonder if there is some extension/linter that is causing the error. Does the snippet actually work when you try it?Ephraimite
I accidentally selected Javascript React as the type. I've moved it over to Javascript language type. It does not work if I try it. I wonder if I have to restart VS or something now. Question about the javascript react vs javascript: Why do we differentiate between the two?Fairly
You shouldn't have to restart but should try it. I didn't have to restart to test your snippet. You are trying it in a .js file I assume? //=, then tab. Sometimes I have to hit tab twice.Ephraimite
Also I would just try copying and pasting your code from here over what you now have in javascript.json. Perhaps you have some hidden control character that won't be included if you copy/past from here.Ephraimite
Restarted. No dice. Copied and pasted as suggested, no dice. I am using a .js file when attempting to use it. I type in //= + tab. I don't even see the option. I'm assuming the snippet will show up when I type that in.Fairly
I'm sorry. I have no other ideas. Yes, it should up after the //= tab.Ephraimite
T
47

I just found this question when looking for an answer, I used snippet generator so assumed the format for each would be the same but without thinking I pasted in code that had tabs in it.

Removing the tabs and used spaces instead for the formatting fixed it for me.

Hope this helps. :)

Toluca answered 30/11, 2019 at 10:56 Comment(2)
You don't have to remove the tabs, you could just use \t in your stringClark
@MikeOttink You are my lord and savior, sir.Manhour

© 2022 - 2024 — McMap. All rights reserved.