Spencer
Spencer

Reputation: 23

Escaping backslashes in Visual Studio Code workspace settings variables

I have a Visual Studio Code workspace file where I'd like to be able to use the predefined ${workspaceFolder} variable, as documented here:

https://code.visualstudio.com/docs/editor/variables-reference

${workspaceFolder} - the path of the folder opened in VS Code

However, the command requires the variable to have backslashes escaped. This setting works with hard-coded, manually-escaped, absolute paths:

"settings": {
    "python.linting.pylintArgs": [
        "--rcfile",
        "C:\\path\\to\\project\\.pylintrc",
        "--init-hook",
        "import sys; sys.path.append('C:\\\\path\\\\to\\\\project\\\\')"
    ]
}

In this case, ${workspaceFolder} = C:\path\to\project

My goal is to have something that looks like this:

"settings": {
    "python.linting.pylintArgs": [
        "--rcfile",
        "${workspaceFolder}\\.pylintrc",
        "--init-hook",
        "import sys; sys.path.append('${workspaceFolder}')"
    ]
}

However, the code above does not work because the second use requires backslashes to be escaped.

Anyone know if there is a way to do this in VS Code?

Upvotes: 2

Views: 3355

Answers (1)

Nifim
Nifim

Reputation: 5021

You can convert the ${workspaceFolder} string to a raw string for python when you run the sys.path.append function. To do that you need to add a r before the opening '

Example:

"settings": {
    "python.linting.pylintArgs": [
        "--rcfile",
        "${workspaceFolder}\\.pylintrc",
        "--init-hook",
        "import sys; sys.path.append(r'${workspaceFolder}')"
    ]
}

Upvotes: 1

Related Questions