11

In VS Code settings, there are some fields which I configure the same. These would be namely font and font size. Generally I have them all set to the same. I am trying to set up a variable in the settings.json which can be applied to all of them. After reading through Variables Reference for VS Code, I came up with the following:

{
  "common": [
    {
      "font": "Anonymous Pro",
      "fontSize": 10,
    }
  ],

  // Set up the Editor
  "editor.fontFamily":              "${common:font}",
  "editor.fontSize":                "${common:fontSize}",
  "terminal.integrated.fontFamily": "${common:font}",
  "terminal.integrated.fontSize":   "${common:fontSize}",
  "debug.console.fontFamily":       "${common:font}",
  "debug.console.fontSize":         "${common:fontSize}",
}

Though, this doesn't seem to work. Is there a way to setup variables within the settings.json without having to setup environment variables?

Thanks!

lordhog
  • 3,427
  • 5
  • 32
  • 43

5 Answers5

7

Currently you cannot set variables in the settings.json file.

The current open issue for VS Code to implement this feature is here: https://github.com/microsoft/vscode/issues/2809

It has yet to have a PR and the opening of the issue occurred Feb 2016, but with comments within the last 2 months.

lbow
  • 71
  • 1
  • 3
2

This is not possible. The link you provided states:

Variable substitution is supported inside key and value strings in launch.json and tasks.json ...

So there is no support for settings.json.

John Archer
  • 2,407
  • 2
  • 25
  • 33
0

What are you trying to achieve? You can set project specific settings in VSCode. So much you can have a different set of fonts and settings based on the project. But I don't think the settings.json supports variables.

Yathi
  • 1,011
  • 1
  • 12
  • 21
0

For anyone coming to this question in 2023, VS Code has updated a way to provide this using the c_cpp_properties.json configuration.

Here is an example of variable definition:

{
"configurations": [
    {
        "customConfigurationVariables": {
            "appInstallPath": "/my/app/bin/",
            "debugRemoteHost": "192.168.1.1",
            "debugRemotePort": "2159"
        }
    }
],
"version": 4

}

And then you would reference these variables in either launch.json or tasks.json using

${input:variable-id}

where the variable-id is defined in the input section as shown below. The "args" key must match your variable defined in customConfigurationVariables:

{
"version": "0.2.0",
"inputs": [
    {
        "id": "installPath",
        "type": "command",
        "command": "cpptools.activeConfigCustomVariable",
        "args": "appInstallPath"
    },
    {
        "id": "host",
        "type": "command",
        "command": "cpptools.activeConfigCustomVariable",
        "args": "debugRemoteHost"
    },
    {
        "id": "port",
        "type": "command",
        "command": "cpptools.activeConfigCustomVariable",
        "args": "debugRemotePort"
    }
],
"configurations": [
    {
        // Enables single-click remote debugging on this device from the project install location
        "name": "Local Debug",
        "type": "cppdbg",
        "request": "launch",
        "program": "${command:cmake.launchTargetPath}",
        "cwd": "${input:installPath}",
        "environment": [],
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            },
            {
                "description":  "Set Disassembly Flavor to Intel",
                "text": "-gdb-set disassembly-flavor intel",
                "ignoreFailures": true
            }
        ],
        "stopAtEntry": true,
        "externalConsole": false,
        "logging": {
            "engineLogging": false,
            "trace": false,
            "traceResponse": false
        }
    }
]
}
Blackjack
  • 9
  • 2
0

As part of the mentioned issue "Support variables when resolving values in settings" (microsoft/vscode issue 2809), you now have (from Bohdan Shulha):

wss: Workspace Shared Settings (repository: bohdan-shulha/vs-wss)

  • Have you ever need to use ${workspaceFolder} in your settings.json?
  • Were you struggling to share settings with your team?
  • Do you have multiple devices running different OSes and you need the same settings everywhere?

This extension is an answer to all these questions.

It is still in development, but looks promising!

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250