taiji812
taiji812

Reputation: 51

How to define and use custom variable for VS Code snippets

Is there any way to define custom variables for VS Code snippets? Particularly, I want to set variable values from settings. For example, with config setting "my-user-name-test.user.name" in setting.json, I want to define snippets like the below example.

{
    "Input User Name": {
        "prefix": "user-name",
        "body": [
            "{",
            "    \"user_name\": \"${config:my-user-name-test.user.name}\"",
            "}"
        ],
        "description": "Test config user name"
    }
}

Upvotes: 5

Views: 1005

Answers (1)

starball
starball

Reputation: 51139

This is the direct subject of issue ticket Support custom variables in snippet #59557, which is closed as "out-of-scope" (see this comment accompanying the closure).

But not to worry (yet). There's a more general/powerful feature-request issue ticket: Execute functions during snippet expansion #10561. It's still open / in the backlog. I suggest that you give that issue ticket a thumbs up to show support for it. You can also subscribe to it to get notified about discussion and progress. Please avoid making noisy comments there like ones that just consist of "+1" / "bump". But even if/once that goes through, this might require a VS Code extension- either to directly implement a function that you can use like a custom variable, or an abstraction layer that allows you to write definitions in some language, and then implement functions for them.

Upvotes: 1

Related Questions