Working on a Python project and I want to define the path to where I store my virtualenv for the project.
I have linting settings in my .vscode/settings.json workspace settings, however this is checked into my git repository and is common across any collaborators on the project, thus I don't think it would make sense to reference where I personally keep my virtualenv for this project in the workspace settings.
As it is a project-specific virtualenv, it does not make sense to reference it in my user settings either.
Is there a way I can store my path to my virtualenv for this project?
You can override .vscode/settings.json with settings in code-workspace.json, but more general and flexible overriding does not seem to be possible - I recommend voting for Add ability to extend from other settings files. If you commit both .vscode/settings.json and [name].code-workspace, then it seems like it'll be difficult for team members to customize their settings.
Nested settings in .vscode/settings.json seem to override [name].code-workspace settings, so you could try committing a workspace file. Some people also commit example files, e.g. settings.json.default and instruct team members to remove the default extension.
I messed around with an example:
example.code-workspace
{
"folders": [
{
"path": "."
},
{
"path": "nested"
}
],
"settings": {
"window.zoomLevel": 1,
"editor.fontSize": 8
}
}
With nested containing .vscode/settings.json:
{
"window.zoomLevel": 2,
"editor.fontSize": 16
}
This works as you'd probably expect: the nested folder settings override the workspace settings, although window.zoomLevel became disabled with a tooltip message saying that it would only be applied if opened directly.
This should be possible if you're keeping the virtualenv in the same folder as the project code itself. Then you can use the following setting in .vscode/settings.json:
"python.venvPath": "${workspaceRoot}/venv"
Just exclude venv from your SCM and you're done.
If you prefer to keep the virtualenv elsewhere, this can be solved by symlinking the location to venv inside the workspace root.
Related
How could I use User settings inside VS Code if there are always Workspace settings?
I am working with a project directory. I am working using VS Code, but other team members use different IDEs to work with the directory. So, we are not allowed to add any IDE specific folders (e.g. the .vscode folder). That means in reality the project has no VS Code Workspace settings. But whenever I go to the Settings I still can see that Workspace tab.
I tried to update the prettier.tabWidth property in User to be 4, while in Workspace it is 2 and still VS Code uses the value from Workspace even though there are no Workspace files and the value is merely default. And I can not change the Workspace settings, since then the Workspace settings file will get added to the project.
So, basically the default Workspace settings override the manually put User settings. That is depressing actually since it makes User settings useless.
E.g. is there any way for me to set the prettier.tabWidth to 4 without adding Workspace settings files to the project?
In Notepad++ there is an option called 'Save session' that saves a list of all opened files (in the current window), bookmarks, code foldings, cursor position, etc., so I can reopen that state later even if I close the files.
In Visual Studio Code, I have not found an option like that. It seems the Workspace only saves the folder path and some settings defined to that workspace.
Is there a way to create something like a Notepadd++ session in Visual Studio Code?
I think the most similar thing to what you are asking for are Workspaces. From testing, they do in fact keep the files you had open, so long as you close the workspace in the menu first.
A Visual Studio Code "workspace" is the collection of one or more folders that are opened in a VS Code window (instance). In most cases, you will have a single folder opened as the workspace but, depending on your development workflow, you can include more than one folder, using an advanced configuration called Multi-root workspaces.
The concept of a workspace enables VS Code to:
Configure settings that only apply to a specific folder or folders but not others.
Persist task and debugger launch configurations that are only valid in the context of that workspace.
Store and restore UI state associated with that workspace (for example, the files that are opened).
Selectively enable or disable extensions only for that workspace.
If you look under File, you'll see the Workspace options:
The way it works is it saves a file that is a .code-workspace extension, but really the underlying structure is JSON. For example, the file might look like:
{
"folders": [
{
"path": "..\\..\\..\\..\\dev\\Project Files\\Project\\MyProject"
}
],
"settings": {}
}
Is there a simple command that will tell me what my workspace folder is? I tried ${workspaceFolder} in the terminal but that didn't work.
Alternatives that currently come to my mind:
1.) If you want to see the workspace folder in the titlebar, you could adjust window.title setting (workspace or user settings):
"window.title": "${dirty}${activeEditorShort}${separator}${folderPath}${separator}${appName}"
Multiple variables can be used here - see Defaults -> window.title. ${folderPath} works best for me, if you prefer the absolute workspace path.
2.) Define a task that can print your workspace folder at the terminal:
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo ${workspaceFolder}"
}
]
3.) File -> save as workspace should actually show the current workspace folder (seems to be not consistent with Windows/Mac though)
4.) Just open the terminal and look at your cwd. I am not sure, if all terminals default to the workspace folder.
VSCode 1.52 (Nov. 2020) will simplify that use case.
Before:
/ws
/.vscode
launch.json
/proj_a
/scr
/proj_b
/scr
/proj_c
/scr
/proj_d
/scr
Where /ws and /proj_* are all folders added to create the multi folder workspace.
But ${workspaceFolder} always is /ws
${workspaceFolder:proj_a} is possible but not convenient, when you have a debug configuration in /ws/.vscode/launch.json that uses ${file} and you want to be able to use this configuration on all files in your multi-root setup.
And in order to establish the correct working directory for your debuggee you need a way to derive the workspace folder path from ${file}.
Introducing ${fileWorkspaceFolder}.
With VSCode 1.52, see:
issue 84162: "Get the workspace folder of the current file "
issue 108907: "Multi root workspace - Variable for the current file's workspace directory"
commit 876d1f0: introduce new variable ${fileWorkspaceFolder}
That will complement the Predefined variables examples.
Whenever I open a new folder to edit some code (code .), opening the settings dialog (⌘+,) immediately creates a directory .vscode with a mostly empty file settings.json.
Since there are no workspace-specific settings, the file looks like:
{
}
For the time being, I do not want to have .vscode directories spread across my filesystem. I am happy with global settings.
Is there a way to disable this behavior and prevent Visual Studio Code from creating these files automatically?
Currently this isn't an option, but it is an open issue (see here). This post also contains some work-arounds for git projects.
For each project (with Git enabled), VSCode auto generate a project specific config file called .vscode.
How to disable this behavior?
Maybe some extention is doing it? It shouldn't create folder unless workspace settings were changed or created runner or tasks or debug.
Look what's inside that settings.json file.
Try to run vscode without extentions.
You can also exclude it from explorer:
setting.json (ctrl+,) >>
"files.exclude": {
"**/.vscode": true,
},