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?

Gurnzbot
  • 3,742
  • 7
  • 36
  • 55
  • 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? – Mark Sep 21 '17 at 14:16
  • 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. – Gurnzbot Sep 21 '17 at 14:29
  • 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? – Mark Sep 21 '17 at 14:34
  • 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? – Gurnzbot Sep 21 '17 at 14:37
  • 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. – Mark Sep 21 '17 at 14:44
  • 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. – Mark Sep 21 '17 at 14:47
  • 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. – Gurnzbot Sep 21 '17 at 14:55
  • 1
    I'm sorry. I have no other ideas. Yes, it should up after the //= tab. – Mark Sep 21 '17 at 15:01

1 Answers1

41

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. :)

Mr Duck
  • 411
  • 4
  • 6