blog

Opening VS Code terminal in multi root workspace

I have a workspace with two root folders that are placed in separate places on my hard disk. One folder is my blog repo while the other is a Google Drive folder with various markdown notes. My .code-workspace file is placed in local _vscode folder along with other such files.

Now, workspace settings JSON looks something like this:

{
"folders": [
{
"path": "D\\DEV\\teodragovic"
},
{
"path": "D\\GoogleDrive\\notes\\+2021"
},
]
}

(double forward slashes are due to me being on Windows)

I wanted every time workspace is opened (usually via Ctrl+Alt+p) I also get VS Code terminal opened in my blog repo. By default, if I left the workspace with some file from my Drive opened, the terminal will start in my notes/+2021 folder. Not great.

The solution was this:

{
"folders": [
{
"name": "blog",
"path": "D\\DEV\\teodragovic"
},
{
"name": "notes",
"path": "D\\GoogleDrive\\notes\\+2021"
},
],
"settings": {
"terminal.integrated.cwd": "${workspaceFolder:blog}"
}
}

To break it down:

  1. terminal.integrated.cwd allows specifying the path on which to open a terminal
  2. ${workspaceFolder} is a special VS Code variable that points to the workspace folder
  3. To make workspaceFolder work in multi-root setup, write it as ${workspaceFolder:NAME} where NAME value of name property assigned to a given root folder.

StackOverflow answer that helped me figure this out: https://stackoverflow.com/a/50215725/2382115