How to add anaconda powershell to vscode? - visual-studio-code

I'm trying to add anaconda prompt to start up instead of powershell to avoid having to add python to env variables.
"terminal.integrated.shellArgs.windows": [
<args>
]
I tried putting them into single line, splitting them "-Foo Goo" as well as "-Foo","Goo". Each version leads to either error or simply ignoring the "-Command" parameter (the lines simply get pasted, but not executed).

First of all, I I'd like to give a hint for everyone that uses PowerShell to use the new one.
So, with the Anaconda ready to go (and it been equal or greater than 4.6 - use conda --version) run in sequence (from base environment in cwd terminal):
conda update conda
conda init
This will update your conda root environment and the init will setup all you need to run it on both cwd and powershell.
After this, you can start any powershell (inside vscode or not) and it will be conda ready.
Look at this article for further information.
Hope it helps!

I ended up using this (although it has tendency to break).
"terminal.integrated.shellArgs.windows": [
"-ExecutionPolicy"
, "ByPass"
, "-NoExit"
, "-Command"
, "& 'C:\\ProgramData\\Anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\ProgramData\\Anaconda3'"
],

Thanks Zerg! Your answer worked for me but I also got a warning message to say that this approach has been depreciated. After some googling I got this working by adding a new terminal profile to settings.json.
"terminal.integrated.profiles.windows": {
"PowerShell (Anaconda)": {
"source": "PowerShell",
"args": [
"-ExecutionPolicy"
, "ByPass"
, "-NoExit"
, "-Command"
, "& 'C:\\Users\\<username>\\AppData\\Local\\Continuum\\anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\<username>\\AppData\\Local\\Continuum\\anaconda3'"
]
}
},
Then changing the default profile:
"terminal.integrated.defaultProfile.windows": "PowerShell (Anaconda)",

I just installed PowerShell 7, and since I had anaconda installed before, this seems to add the starting command to the profile.ps1 automatically.
The profile.ps1 in C:\Users\USER\Documents\PowerShell (this is version 7, directory WindowsPowerShell would be the old version 5) contains:
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "C:\Users\USER\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion
With these automatic settings at the start of PowerShell 7, adding PowerShell 7 as a new terminal type to vsccode solved it.
This is how to add PowerShell 7 to the dropdown menu:
Enter Ctrl+Shift+P, open settings.json for the User, and add
{
"terminal.integrated.profiles.windows": {
"PowerShell7": {
"path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"args": ["-NoProfile",
"-noexit",
"-file",
"C:\\Users\\USER\\Documents\\PowerShell\\profile.ps1"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell7"
}
Then in the settings.json, press Ctrl+s and restart (!) vscode. You will see PowerShell7 as the new default terminal in the dropdown of terminal types:

From the VSCode Command Palette (Ctrl+Shift+P), select
Terminal: Select default shell
and then pick PowerShell.
Then from the Command Palette (Ctrl+Shift+P), select
Python: Select Interpreter
and pick one of the conda environments. When you now open a new terminal VSCode starts PowerShell and activates the selected environment. This is exactly what the Anaconda-Prompt does. However, you should not set a PYTHONPATH in the environment in combination with an Anaconda install. Conda activation is all you need. It not only adds the selected interpreter to the PATH, but the required libraries too.

Related

Default Powershell Terminal is 7.2 but scripts run as Powershell 5.1

Notice in my VS Code, a fresh terminal, the very first line indicates it is properly defaulted to Powershell 7.2.2 - just as I believe it should.
But then in this same screenshot, the first script I run (which has a command to print out the powershell version) it shows the PSVersion is 5.1.19041.1320
Here's all my powershell specific VSCode entries from settings.json
"terminal.integrated.profiles.windows": {
"MyPowerShell_7": {
"path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"args": ["-NoProfile"]
}
},
"terminal.integrated.defaultProfile.windows": "MyPowerShell_7",
"powershell.powerShellAdditionalExePaths": [
{
"exePath": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"versionName": "MyPowerShell_7"
}
],
"powershell.powerShellDefaultVersion": "MyPowerShell_7",
Why why why is it so hard to run the Test-Json cmdlet?
finally found an answer here on reddit
I didn't know that 5.1 and 7 are side-by-side and have different ways to invoke each application
So, if I change my command from powershell to pwsh I get the right result
Actually, when you have a PowerShell or pwsh terminal open (Which you have here) you should call your script directly and it will be executed in that host.
So instead of
PS C:\>pwsh -file './scripts/ps/experiment.ps1'`
you should call
PS C:\>./scripts/ps/experiment.ps1
The way you are calling your scripts will launch a new host, run the script and then terminate.

How to I remove the Powershell start text?

Powershell 6 has a Unix-style /etc/issue that mentions a link to the docs.
PowerShell v6.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
This is fine, but:
I know where the docs are
I know I launched Powershell 6
How can I remove some, or all of the message? IIRC Powershell 5 still had the copyright message so maybe I can't remove that, but getting rid of the last 3 lines would help?
Pass the -nologo option.
-NoLogo Starts the PowerShell console without displaying the copyright banner.
pwsh.exe -nologo ...other arguments...
If you are using the new Windows Terminal then:
Go to Settings:
Add the argument -nologo to the PowerShell command line:
From #sandu's answer, it could be improved as below.
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"args": ["-noLogo"]
}
},
"terminal.integrated.defaultProfile.windows": "PowerShell",
Now you can add -nologo in Windows Terminal settings:
The easiest way would be to add Clear-Host to the top of your $profile file.
You can open powershell and input $profile, you will get Microsoft.PowerShell_profile.ps1 file's path.
open this file (create one without) by notepad and input a clear or cls command.Save and exit.
the command of this file will be executed when you open powershell
As np8 said under this answer, the way to hide the initial text in the Visual Studio Code integrated terminal is as follows:
Open the workspace settings file (./.vscode/settings.json) or the user settings file (search for Preferences: Open Settings (JSON) in the command palette, opened with F1)
Add the following inside the outer-most "layer" of curly brackets:
"terminal.integrated.shellArgs.windows": [
"-nologo"
]
What worked for me was to go to File > Preferences > Settings and type terminal.integrated.defaultProfile.windows in the search bar (you can change windows with other supported OS if you need to). There should now be only one option to choose from (that's why I included windows). Click on the Edit in settings.json link bellow it. It should now open settings.json and generate the line necessary to modify the default terminal profile. Which looks something like:
"terminal.integrated.defaultProfile.windows": "",
Above that line you can create your own profile:
"terminal.integrated.profiles.windows": {
"PowerShell -nologo": {
"source": "PowerShell",
"args": ["-nologo"]
}
},
Name the profile however you want and add whatever parameters you want. Once you're done. Set the profile name into the option that was previously generated for you. In this example it should look like:
"terminal.integrated.defaultProfile.windows": "PowerShell -nologo",
Save and close. Now when you open a terminal it should have no logo for example.
I was fix problem on the top by
There are several ways of doing it :
Creating a shortcut with value
"C:\Program Files\PowerShell\7\pwsh.exe" -nologo -NoExit -Command "Set-Location c:\Users\%username%"
If you are using Windows terminal then you can replace the source with
"commandline": "pwsh.exe -NoLogo"
Users of Chinese version can also add -nologo in Windows terminal settings:
As shown in the figure:
If you are using the new Windows Terminal in Windows 11,
Go to Settings:
Select the Windows Powershell profile from the left sidebar
Add the argument -nologo to the command line:
Save and be sure to restart your terminal to see the changes.
If you're using windows just create a profile and put the command clear inside it.
In PowerShell I'm going to open the profile with notepad:
notepad $Profile
Highly Recommended to Use
Powershell Preview: https://github.com/PowerShell/powershell/releases
Editing Context Menu
If you are using Powershell Preview, you can change context menu using regedit:
Open regedit
Go to Computer\HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShell7-previewx64
add flag -nologo to Icon
2. Editing Start Menu Shortcut
In File Explorer go to
C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
Create a shortcut with value
"C:\Program Files\PowerShell\7-preview\pwsh.exe" -nologo -NoExit -Command "Set-Location c:\\Users\\%username%"

Run Powershell as administrator with Terminal Sublime Text 3

How can I run PowerShell as administrator with Terminal in Sublime Text 3?
Here is the default configuration:
{
// The command to execute for the terminal, leave blank for the OS default
// See https://github.com/wbond/sublime_terminal#examples for examples
"terminal": "",
// A list of default parameters to pass to the terminal, this can be
// overridden by passing the "parameters" key with a list value to the args
// dict when calling the "open_terminal" or "open_terminal_project_folder"
// commands
"parameters": [],
// An environment variables changeset. Default environment variables used for the
// terminal are inherited from sublime. Use this mapping to overwrite/unset. Use
// null value to indicate that the environment variable should be unset.
"env": {}
}
Option 1: Run Sublime Text as administrator. The process started by the Terminal plugin will run under the administrator user as well. Be careful—third-party plugins will also have admin privileges.
Option 2: Configure the Terminal plugin to start a new PowerShell instance that runs as an administrator:
"terminal": "powershell.exe",
"parameters": [
"-WindowStyle", "Hidden",
"-Command", "Start-Process", "-Verb", "RunAs", "powershell.exe",
"-ArgumentList", "'-NoExit', '-Command', Set-Location, \"'$PWD'\""
],
This configuration launches a command that restarts PowerShell under an admin user. We manually reset the working directory because the new instance starts in a different process space. When UAC is enabled, we'll need to confirm execution within the UAC prompt that appears to escalate privileges. If desired, we can disable the prompt for a specific program.
Here is an article that talks to your use case using a plug in.
"Wouldn’t it be great if Sublime allowed you to run bash or PowerShell from its popup terminal?"
sublimetexttips.com/how-to-open-terminal-or-powershell-from-your-sublime-project

Open Visual Studio Code Powershell Terminal in script folder

When I right-click a ps1 script in VSCode and open it in Powershell Terminal, the current folder is not the script folder but the project folder.
Is there a way to configure it to open in the script folder ?
Assumptions:
You've configured PowerShell as the default shell - either via command Terminal: Select Default Shell (on Windows) or directly via user settings "terminal.integrated.shell.windows" / "terminal.integrated.shell.osx" / "terminal.integrated.shell.linux".
You're trying to open a PowerShell instance by right-clicking a file / subfolder in the side bar's Explorer view and choose Open in Terminal, and you want that instance's current location to be that file's containing folder / that subfolder.
As of (at least) VSCode 1.17.2, this should work by default.
If it doesn't, perhaps custom code in your $PROFILE directly or indirectly changes the current location.
If you don't want to / cannot prevent $PROFILE code from changing the location, here's a workaround via the user settings that explicitly sets the current location after the $PROFILE code has run:
On Windows:
"terminal.integrated.shell.windows": "cmd.exe",
"terminal.integrated.shellArgs.windows": [ "/c", "powershell -noexit -command 'Set-location \"%CD%\"'" ]
This uses cmd.exe as the default shell, which then invokes PowerShell with a command that explicitly makes it change to that folder.
The workarounds for other platforms below work analogously:
On macOS:
"terminal.integrated.shell.osx": "/bin/bash",
"terminal.integrated.shellArgs.osx": [ "-l", "-c", "exec pwsh -noexit -command 'set-location \"$PWD\"'" ]
On Linux:
"terminal.integrated.shell.linux": "/bin/bash",
"terminal.integrated.shellArgs.linux": [ "-c", "exec pwsh -noexit -command 'set-location \"$PWD\"'" ]

How to change the integrated terminal in Visual Studio code

I want to change integrated terminal to CMDER i use VS Code on Windows 8.1.
I checked the docs and also preference file, but I am confused
which line to change.
External Terminal
// Customizes which terminal to run on Windows.
"terminal.external.windowsExec": "%COMSPEC%",
// Customizes which terminal application to run on OS X.
"terminal.external.osxExec": "Terminal.app",
// Customizes which terminal to run on Linux.
"terminal.external.linuxExec": "xterm",
Integrated Terminal
// The path of the shell that the terminal uses on Linux.
"terminal.integrated.shell.linux": "sh",
// The command line arguments to use when on the Linux terminal.
"terminal.integrated.shellArgs.linux": [],
// The path of the shell that the terminal uses on OS X.
"terminal.integrated.shell.osx": "sh",
// The command line arguments to use when on the OS X terminal.
"terminal.integrated.shellArgs.osx": [],
// The path of the shell that the terminal uses on Windows. When using shells shipped with Windows (cmd, PowerShell or Bash on Ubuntu), prefer C:\Windows\sysnative over C:\Windows\System32 to use the 64-bit versions.
"terminal.integrated.shell.windows": "C:\\Windows\\system32\\cmd.exe",
// The command line arguments to use when on the Windows terminal.
"terminal.integrated.shellArgs.windows": [],
// Controls the font family of the terminal, this defaults to editor.fontFamily's value.
"terminal.integrated.fontFamily": "",
// Controls whether font ligatures are enabled in the terminal.
"terminal.integrated.fontLigatures": false,
// Controls the font size in pixels of the terminal, this defaults to editor.fontSize's value.
"terminal.integrated.fontSize": 0,
// Controls the line height of the terminal, this number is multipled by the terminal font size to get the actual line-height in pixels.
"terminal.integrated.lineHeight": 1.2,
// Controls whether the terminal cursor blinks.
"terminal.integrated.cursorBlinking": false,
// Controls whether locale variables are set at startup of the terminal, this defaults to true on OS X, false on other platforms.
"terminal.integrated.setLocaleVariables": false,
// A set of command IDs whose keybindings will not be sent to the shell and instead always be handled by Code. This allows the use of keybindings that would normally be consumed by the shell to act the same as when the terminal is not focused, for example ctrl+p to launch Quick Open.
"terminal.integrated.commandsToSkipShell": [
"editor.action.toggleTabFocusMode",
"workbench.action.debug.continue",
"workbench.action.debug.restart",
"workbench.action.debug.run",
"workbench.action.debug.start",
"workbench.action.debug.stop",
"workbench.action.quickOpen",
"workbench.action.showCommands",
"workbench.action.terminal.clear",
"workbench.action.terminal.copySelection",
"workbench.action.terminal.focus",
"workbench.action.terminal.focusNext",
"workbench.action.terminal.focusPrevious",
"workbench.action.terminal.kill",
"workbench.action.terminal.new",
"workbench.action.terminal.paste",
"workbench.action.terminal.runSelectedText",
"workbench.action.terminal.scrollDown",
"workbench.action.terminal.scrollDownPage",
"workbench.action.terminal.scrollToBottom",
"workbench.action.terminal.scrollToTop",
"workbench.action.terminal.scrollUp",
"workbench.action.terminal.scrollUpPage",
"workbench.action.terminal.toggleTerminal"
],
To change the integrated terminal on Windows, you just need to change the terminal.integrated.shell.windows line:
Open VS User Settings (Preferences > User Settings). This will open two side-by-side documents.
Add a new "terminal.integrated.shell.windows": "C:\\Bin\\Cmder\\Cmder.exe" setting to the User Settings document on the right if it's not already there. This is so you aren't editing the Default Setting directly, but instead adding to it.
Save the User Settings file.
You can then access it with keys Ctrl+backtick by default.
It is possible to get this working in VS Code and have the Cmder terminal be integrated (not pop up).
To do so:
Create an environment variable "CMDER_ROOT" pointing to your Cmder
directory.
In (Preferences > User Settings) in VS Code add the following settings:
"terminal.integrated.shell.windows": "cmd.exe"
"terminal.integrated.shellArgs.windows": ["/k", "%CMDER_ROOT%\\vendor\\init.bat"]
I know is late but you can quickly accomplish that by just typing Ctrl+Shift+P and then type "default" - it will show an option that says:
Terminal: Select Default Shell
It will then display all the terminals available to you.
From Official Docs
Correctly configuring your shell on Windows is a matter of locating
the right executable and updating the setting. Below is a list of
common shell executables and their default locations.
There is also the convenience command Select Default Shell that can be
accessed through the command palette which can detect and set this for
you.
So you can open a command palette using ctrl+shift+p, use the command Select Default Shell, then it displays all the available command line interfaces, select whatever you want, VS code sets that as default integrated terminal for you automatically.
If you want to set it manually find the location of executable of your cli and open user settings of vscode(ctrl+,) then set
"terminal.integrated.shell.windows":"path/to/executable.exe"
Example for gitbash on windows7:
"terminal.integrated.shell.windows":"C:\\Users\\stldev03\\AppData\\Local\\Programs\\Git\\bin\\bash.exe",
For OP's terminal Cmder there is an integration guide, also hinted in the VS Code docs.
If you want to use VS Code tasks and encounter problems after switch to Cmder, there is an update to #khernand's answer. Copy this into your settings.json file:
"terminal.integrated.shell.windows": "cmd.exe",
"terminal.integrated.env.windows": {
"CMDER_ROOT": "[cmder_root]" // replace [cmder_root] with your cmder path
},
"terminal.integrated.shellArgs.windows": [
"/k",
"%CMDER_ROOT%\\vendor\\bin\\vscode_init.cmd" // <-- this is the relevant change
// OLD: "%CMDER_ROOT%\\vendor\\init.bat"
],
The invoked file will open Cmder as integrated terminal and switch to cmd for tasks - have a look at the source here. So you can omit configuring a separate terminal in tasks.json to make tasks work.
Starting with VS Code 1.38, there is also "terminal.integrated.automationShell.windows" setting, which lets you set your terminal for tasks globally and avoids issues with Cmder.
"terminal.integrated.automationShell.windows": "cmd.exe"
I was successful via settings > Terminal > Integrated > Shell: Linux
from there I edited the path of the shell to be /bin/zsh from the default /bin/bash
there are also options for OSX and Windows as well
#charlieParker - here's what i'm seeing for available commands in the command pallette
If you want to change the external terminal to the new windows terminal, here's how.
The method explained in the accepted answer has been deprecated, now the new recommended way to configure your default shell is by creating a terminal profile in #terminal.integrated.profiles.windows# and setting its profile name as the default in #terminal.integrated.defaultProfile.windows#.
The old method will currently take priority over the new profiles settings but that will change in the future.
See an example for powershell taken from the docs
{
"terminal.integrated.profiles.windows": {
"My PowerShell": {
"path": "pwsh.exe",
"args": ["-noexit", "-file", "${env:APPDATA}PowerShellmy-init-script.ps1"]
}
},
"terminal.integrated.defaultProfile.windows": "My PowerShell"
}
change external terminal
windows terminal , which has been mentioned by others, is an alternative of alacrity, which is a terminal (emulator)
As stated in vscode, Cmder is a shell, just like powershell or bash.
"terminal.integrated.profiles.windows": {
"cmder": {
// "path": "F:\\cmder\\Cmder.exe", // 这样会开external terminal
"path": "C:\\WINDOWS\\System32\\cmd.exe",
"args": ["/K", "F:\\cmder\\vendor\\bin\\vscode_init.cmd"]
}
},
"terminal.integrated.profiles.linux": { "zsh": { "path": "zsh" }, },
"terminal.integrated.defaultProfile.windows": "PowerShell",
The statment in cmder' repo is misleading
Different shells under a terminal:
If you want to change the external terminal to the new windows terminal, here's how.
Probably it is too late but the below thing worked for me:
Open Settings --> this will open settings.json
type terminal.integrated.windows.shell
Click on {} at the top right corner -- this will open an editor where this setting can be over ridden.
Set the value as terminal.integrated.windows.shell: C:\\Users\\<user_name>\\Softwares\\Git\\bin\\bash.exe
Click Ctrl + S
Try to open new terminal. It should open in bash editor in integrated mode.
Working as of 02-Dec-2021.
In settings.json
{
"go.toolsManagement.autoUpdate":true,
"terminal.integrated.profiles.windows":{
"My Bash":{
"path":"D:\\1. Installed_Software\\Git\\bin\\bash.exe"
}
},
"terminal.integrated.defaultProfile.windows":"My Bash"
}
Reference: https://code.visualstudio.com/docs/editor/integrated-terminal#_terminal-profiles