In VSCode how to set the working directory of a new python interactive window? - visual-studio-code

When executing Jupyter: Create Interactive Window in VScode a new interactive window is opened, how to control the working directory this fresh python interpreter?
Steps in Mac OS:
cmd+shift+P
type Jupyter: Create Interactive Window
a new interactive window is opened
in there type !pwd to see the working directory
How to control in which directory this is? I am working with a multi-root workspace

Changing the order of the paths listed in the work space file (e.g. my_workspace.code-workspace) controls the default working directory when launching a new interactive window.
The workspace file looks like
{
"folders": [
{
"path": "."
},
{
"path": "../my_path_1"
}, {
"path": "../my_path_2"
}
],
"settings": {
"git.enabled": false
}
}
The new interactive window will have the working directory of the first path listed above. In this case the the path where my_workspace.code-workspace is located.

Related

Can't force external terminal to open in current directory in VSCode

How can I open an external terminal in the current project directory?
I want to open a wsl session (when I invoke open external terminal) but it keeps opening in Microsoft VS Code folder
My setting is as follow:
{
"terminal.external.windowsExec": "wsl.exe"
}
I tried passing an argument
{
"terminal.external.windowsExec": "wsl.exe -d ${workspaceFolder}"
}
But vscode doesn't like that.
What am I missing?

How to run streamlit multi-command in VS Code regardless of default terminal

I have the Multi-command extension installed on VS Code and use it to launch Streamlit apps using this configuration in settings.json which is run via a keyboard shortcut:
"multiCommand.commands": [
{
"command": "multiCommand.streamlitActiveFile",
"label": "Streamlit: Run Active File",
"description": "Streamlit run active file in active terminal",
"sequence": [
"workbench.action.terminal.focus",
{
"command": "workbench.action.terminal.sendSequence",
"args": {
"text": "streamlit run ${relativeFile}\u000D"
}
}
]
}
]
This has worked fine thus far while I have been using the standard command prompt on a Windows machine. Now I've changed to using Git Bash as my default terminal, and the command above fails because the relative file path is passed as a Windows path with backslashes, and Git Bash can't parse that:
$ streamlit run visuals\streamlit\time_plot.py
Usage: streamlit run [OPTIONS] TARGET [ARGS]...
Try 'streamlit run --help' for help.
Error: Invalid value: File does not exist: visualsstreamlittime_plot.py
In short, the backslashes are ignored and the path becomes a single file name instead. I'd like some way to be able to launch Streamlit apps using a multi-command via a keyboard shortcut, and I'm open to either of these solutions, or something else I haven't considered:
Send a relative file path that can be parsed correctly regardless of which terminal is the default terminal.
Launch a Windows command prompt even when Git Bash is the default terminal, and activate the Streamlit app there.
Change the working directory to the directory containing the streamlit file (again regardless of which terminal is the default terminal) and launch the file there without having to give a path.

Set integrated Terminal name in VSCODE with settings.json

There's a few questions related to renaming or setting a terminal profile:
Change Integrated Terminal title in vscode
VSCode integrated terminal argument: current filename
But if you want to define this via settings.json per folder, how would you do it?
The answer is that it can only be done for the workspace.
Ref profile
Ref settings
Workspace.code-workspace eg:
{
"settings": {
"powershell.powerShellDefaultVersion": "PowerShell (x64)",
"python.envFile": "${workspaceFolder}/.venv",
"terminal.integrated.profiles.windows": {
"myprofilename": {
"source": "PowerShell",
"overrideName": true,
"icon": "terminal-powershell"
}
}
}
}
This will create the myprofilename as a selection for new terminal options.
However, if you want to set the default in a multi root workspace, at a folder level:
eg: somefolder_in_workspace/.vscode/settings.json:
{
"terminal.integrated.defaultProfile.windows": "myprofilename"
}
You'll see the line greyed out with a message if you hover: This setting cannot be applied in this workspace. It will be applied when you open the containing workspace folder directly.
This is how the documentation explains the behaviour:
To avoid setting collisions, only resource (file, folder) settings are applied when using a multi-root workspace. Settings that affect the entire editor (for example, UI layout) are ignored. For example, two projects cannot both set the zoom level.
So the answer is no, for folders in multi root workspaces, but yes, if you want the name for the workspace terminal

Use cmder as default shell in VSCode

Every other solution I can find to this question says to put the settings below inside my.vscode/settings.json
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
"terminal.integrated.shellArgs.windows": [
"/K",
"C:\\softwares\\cmder\\vendor\\init.bat"
],
This worked until recently, when I removed my settings file from a git repo. Now when I hover over the code above, I get this text in a popup
The path of the shell that the terminal uses on Windows (default:
C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe). Read more
about configuring the shell. This is deprecated, use
#terminal.integrated.defaultProfile.windows# instead(2)
This is a bit confusing to me, I'm not sure what I have to change to what. I tried changing the first setting to
"terminal.integrated.defaultProfile.windows": "C:\\WINDOWS\\System32\\cmd.exe",
But this has had no effect. Cmder is not being used as the default shell.
VSCode changed how integrated terminal profiles are handled in April 2021. Based on the args you provided in your question, adding this to your settings.json should work:
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [
"/K",
"C:\\softwares\\cmder\\vendor\\init.bat"
],
"icon": "terminal-cmd"
}
},
"terminal.integrated.defaultProfile.windows": "Command Prompt"
cmder comes as an executable program. You do not need to use a batch script to open it. Just firing the exe starts the program.
(from the official download page:)
Unzip
(optional) Place your own executable files into the bin folder to be injected into
your PATH.
Run Cmder (Cmder.exe)
Therefore, you just need to add a single line to settings.json:
"terminal.integrated.shell.windows": "C:\\full\\path\\to\\unzipped\\cmder.exe"
This shouldn't need to take in any flags or commands, args, unless you are wanting to change it's default functionalities (such as changing the default folder that the cursor points to on launch). For default cmder, just run the exe...
try add follow config in settings.json
"terminal.integrated.automationShell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
A path that when set will override terminal.integrated.shell.windows and ignore shellArgs values for automation-related terminal usage like tasks and debug.

Opening a new terminal tab based on the current one

I've added the following keybinding in vscode to open a new terminal window:
{
"key": "cmd+t",
"command": "workbench.action.terminal.new"
}
When I execute a command like cd Desktop then run cmd+t again while still in it, the new terminal starts from zero. Is it possible for the new terminal to be based on the previous one? meaning in our case it should open a new terminal tab that runs on /Desktop instead of the root directory.
I am using bash.
Add the following to your User Settings (Menu:File > Preferences):
"terminal.integrated.cwd": "Desktop"
This will now open up in Desktop instead of having to cd into it.
I'm not sure how to change the cwd based on other open terminals per your question.Here's the full commented setting in vscode:
// An explicit start path where the terminal will be launched, this is
used as the current working directory (cwd) for the shell process.
This may be particularly useful in workspace settings if the root
directory is not a convenient cwd.
"terminal.integrated.cwd": "",