skip to Main Content

I am trying to create a snippet in VSCode that will generate a template when I type inc.

The template should be either:

#include "header-file", or #include <header-file>

based on the selected brackets.

Additionally, the cursor should be placed inside the brackets.
For example, if I select angle brackets, the generated template should be #include <cursor>.

This is what I have tried so far:

{
    "Include Header": {
        "scope": "c,cpp",
        "prefix": "inc",
        "body": "#include ${1|"$0",<$0>|}",
    }
}

However, the result is not what I expected. VSCode is treating $0 as a string literal, and the output is either #include <$0> or #include "$0"

2

Answers


  1. Try this instead:

    "Include Header": {
        "scope": "c,cpp",
        "prefix": "inc",
        "body": "#include ${1|"<",\"|}${2:header-file}${1|\">",\"|}",
        "description": "Include a header file"
    }
    
    Login or Signup to reply.
  2. try this snippet

      "Include Header": {
        "scope": "c,cpp",
        "prefix": "inc",
        "body": "#include ${1|",<|}${2:header}${1/</>/}",
      }
    

    If you have typed the header file ($2) and press TAB you are at the end of the line.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search