I cannot Copy-Paste from the Visual Studio Code console. In ISE one can copy-paste some of the output, but it does not seem to be possible in Visual Studio Code Terminal. How can I copy-paste the output from the console when running PowerShell commands? I have the PowerShell Extension.
Ctrl+C and Ctrl+V for copying / pasting work as-is in Visual Studio Code's integrated terminal.
By contrast, right-click behavior is configurable:
On Windows, the default behavior is to copy, if text is currently selected, and paste otherwise - as in regular console windows.
To get the same behavior as in the Windows PowerShell ISE, i.e. to instead show a shortcut menu, which contains Copy and Paste commands, add the following line to your settings.json file (before the closing }):
"terminal.integrated.rightClickBehavior": "default",
Alternatively, use the settings GUI (press Ctrl+,):
Note:
The screenshot was taken on macOS, where selectWord is the default setting; on Windows, it is copyPaste, with the behavior as described above.
Also note the GUI's convenient search feature: typing right click in the search field was sufficient to locate the relevant setting.
Related
Powershell ignores tab characters in copied-and-pasted code snippets. This is annoying because I like to write my Python code with single tabs instead of quadruple spaces, and in testing I frequently copy and paste snippets in to test them.
So far I've been using other terminals like Cygwin that properly address pasted tabs, but it would be more convenient for me to use Powershell. How can it be made to address these pasted tabs correctly?
The MWE is to copy and paste anything with a tab into Powershell. For example,
test
appears in Powershell as
>>> test
with no indentation. I've copied and pasted from both Notepad and Notepad++ with the same effect; I'm not using any formatted word processors or anything like that.
Caveat: The next section only applies to PowerShell's own interactive prompt. Different rules may apply to external programs launched from PowerShell that present their own interactive prompt, such as python and, preferably, ipython - see the bottom section for considerations specific to these programs.
Preserving tabs on pasting directly into PowerShell's interactive prompt:
Note: Strictly speaking, when it does work, tabs are converted to 4 spaces each on pasting.
Only works when all of the following prerequisites are met:
When running PowerShell (Core) 7+ (as opposed to Windows PowerShell)
When pasting via Ctrl-V rather than via right-click (the latter emulates typing and therefore triggers tab completion).
When running in one of the following console environments:
Regular console window (conhost.exe)
Windows Terminal
Note:
Does not work in Visual Studio Code's integrated terminal (which seemingly always emulates typing in PowerShell).
Haven't tried third-party consoles such as Cygwin and ConEmu.
Notably, this categorically excludes Windows PowerShell (where a tab ends up as ^I).
Preserving tabs on pasting into the interactive python / ipython REPL, from PowerShell or cmd.exe:
Note:
ipython provides a superior REPL experience compared to python.
Unlike the above, the following also applies to launching from Windows PowerShell, as well as from cmd.exe.
When it works, pasted tabs are converted to 4 spaces, except where noted otherwise.
ipython
Regular console windows (conhost.exe):
Works, with Ctrl-V only.
Windows Terminal:
Works, with Ctrl-V only.
Visual Studio Code's integrated terminal:
Does NOT work (strips tabs).
python
Regular console windows (conhost.exe):
Works, with both Ctrl-V and right-click, as long as Filter clipboard contents on paste is turned OFF on the Options tab of the console window's Properties dialog; pastes actual tabs.
Windows Terminal:
Works, with right-click only(!); pastes actual tabs.
Visual Studio Code's integrated terminal:
Works.
The only way I was able to make this work was by using Windows Terminal. Which is a new terminal application built by Microsoft that supports a bunch of new features.
I don't know the technical reasons why, but it appears it supports pasting both tabs and spaces appropriately, without loss or conversion into the Python CLI REPL.
I tested this using Windows Terminal while running Windows PowerShell, PowerShell Core 7+ and Command Prompt, while running the Python CLI and all supported pasting tabs.
You can install Windows Terminal through a number of sources...Windows Store, Binary download, WinGet, Choco, etc.
Here's the github repo with instructions on various ways to install.
https://github.com/microsoft/terminal
In general, control-v is better for pasting characters than right-click. Note control-v has to be bound to psreadline's paste function (so no emacs mode). Here's an example with an a and an em dash (0x2013). Right click will only paste the a.
a–
I am using VS Code and i have my powershell terminal in VS Code as well as Powershell ISE. I have my own personal script file in the form of .ps1 which has some list of commands. When i select a command in the file and right click and choose "Run Selection" , that doesn't get reflected in the Powershell Terminal in VS code. I even tried Function + F8 key of the laptop, still the same. Any idea what do i need to do to fix this.
I had the same issue, F8 stopped running selected script suddenly. Had to reinstall the PowerShell extension for VScode and it fixed it.
Check key bindings as well by hitting Ctrl+K, Ctrl+S and search "Run Selection". This should be a sub function of the PowerShell extension.
I ran into a similar problem and I came into the solution. Just install the powershell extension for VS code and you'll see the Run Selection/F8 work.
In the terminal (PowerShell) in Visual Studio Code, I'm trying to hit Ctrl+Backspace to delete last word, but it just adds ^W to end of the line, any ideas how to fix this? It works fine outside Visual Studio Code in PowerShell.
ctrl+backspace is somehow mapped to ctrl+w in vscode integrated terminal (possibly a bug) and now we need to set ctrl+w binding to delete the word explicitly. weird right?
Set-PSReadLineKeyHandler -Chord 'Ctrl+w' -Function BackwardKillWord
Note that this'll work for the current terminal session only.
To make this behaviour persistent, you can set it in profile Microsoft.PowerShell_profile.ps1 file. Create the file if it doesn't exist in your powershell version folder.
C:\Program Files\PowerShell\6\Microsoft.PowerShell_profile.ps1
write at the top
if ($env:TERM_PROGRAM -eq "vscode") {
Set-PSReadLineKeyHandler -Chord 'Ctrl+w' -Function BackwardKillWord
}
See more: Power up your PowerShell
Keybinding references:
https://learn.microsoft.com/en-gb/previous-versions/powershell/module/psreadline/Get-PSReadLineKeyHandler?view=powershell-5.0
https://learn.microsoft.com/en-gb/previous-versions/powershell/module/psreadline/set-psreadlinekeyhandler?view=powershell-5.0
Based on the latest comment https://github.com/microsoft/vscode/issues/68167 I have modified JerryGoyal's answer so we don't have to modify bindings:
Put the following at the top of your Microsoft.PowerShell_profile.ps1 config file (type $profile in the terminal to find it, you might have to create one if it doesn't exist already)
if ($env:TERM_PROGRAM -eq "vscode") {
Set-PSReadLineOption -EditMode Emacs
}
This works for me (vscode 1.43)
It was showing ^W when I pressed Ctrl+Backspace.
Just run this command in the vscode console
Set-PSReadLineOption -EditMode Emacs
NOW IT WORKS!
Looks like an issue that is being tracked: see workbench.action.terminal.deleteWordLeft Doesn't Work, Outputs ^W.
Works fine for me in git bash terminal but not powershell as of vscode 1.36.
vscode v1.45 made some changes to these terminal commands, like deleteWordLeft. Does Ctrl+Backspace work better for you now?
See https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_45.md#removal-of-several-prompt-related-commands
VSCode 1.48 (July2020) might help.
Issue 98404 does allow ctrl+backspace to delete entire word in cmd.exe, which could work for a powershell session too.
See PR 98494:
Before (when it was not working):
After (working):
Check you keybindings in your settings to be sure it is still set.
I have had some oddities with keybindings getting removed / changed when adding a new extension or an update. You may just need to add it back.
Key Bindings for Visual Studio Code
Of course VSCode and PowerShell are two different environments. If you split your terminal window (I do this all the time for my own work use case), you will end up with the VSChost and the standard consolehost and you'll see that though you are in VSCode, they behave differently.
If you have not done so, you may want to customize you VSCode profile which is what the integrated console will read, where as the consolehost will read your PowerShell console profile not you ISE profile.
To add to JerryGoyal answer:
If you have difficulties finding profile folder and/or making it works. Here's what helped me.
I created folder c:\Users\YOUR_USER_NAME\Documents\WindowsPowerShell\
Put Microsoft.PowerShell_profile.ps1 there
Powershell than tells you about security problems. You have to allow this by running powershell as admin.
Than type Set-ExecutionPolicy RemoteSigned.
Answer Y(yes). This is obviously at your own risk!
Add this to keybindings.json in vs code
{
"key": "ctrl+backspace",
"command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus",
"args": { "text": "\u0017" }
},
I was using clink with ConEmu for various node related tasks on windows, but now I'm trying Visual Studio code.
How do I inject clink into Visual Studio Code's integrated terminal, so that I can get real command history persistence between sessions, incremental history search, etc.?
With ConEmu I could inject clink by dropping the clink folder into a specified pickup directory.
I've tried using the path to the included clink bat file, and the clink exe in the VS Code setting terminal.integrated.shell.windows but these spawn and then close the command shell immediately.
Thanks!
I discovered that you can pass arguments to the integrated shell in Visual Studio Code. Combined with the cmd.exe /K option which Carries out the command specified by string but remains, clink can be injected.
In VS Code, go to File > Preferences > Settings or use Ctrl , and add the settings:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\sysnative\\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/K", "C:\\path\\to\\clink\\clink_x64.exe inject"]
This is the equivalent of opening a command prompt in Windows, and running clink_x64 inject.
It is not answer for your question, but there is another trick to see cmder and text editor in one window. You can open your text editor as another tab in cmder, which I described here:
https://medium.com/#WMorkowski/protip-integrating-cmder-with-text-editor-7f08a6e76de7
from article:
Run your cmder.
Go to ‘Settings -> Startup -> Environment’
Type: set EDITOR_PATH=C:\Program Files (x86)\Microsoft VS Code\Code.exe alias editor="%EDITOR_PATH%" $1 -new_console:s50V Where
in the first line you should type path to your text editor (I was
testing it on Visual Studio Code and Sublime, but it should work with
other editors).
Save your settings
Type ‘editor’ command in command line.
Whoa! We almost finished. But in most cases you don’t want console tab
to be attached to the top of the window. You should close console tab,
and open it again, paying attention to check “New console split to
bottom” checkbox and choose the right console type. Now when you
finally set everything up, you should go to ‘Settings -> Startup’, and
check “Auto save/restore opened tabs” checkbox to save our new
workflow. Now every time you run cmder, your tabs setup will be
restored.
Expanding on my comment:
Open settings.json with:
File > Open > %APPDATA%\Code\User\settings.json
And assuming you installed clink with the magic of chocolatey:
choco install clink-maintained
Then your clink_x64.exe lives here:
C:\Program Files (x86)\clink\clink_x64.exe
And the lines you add to settings.json look like:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\sysnative\\cmd.exe",
"terminal.integrated.shellArgs.windows": [
"/K",
"C:\\Program Files (x86)\\clink\\clink_x64.exe",
"inject",
"--profile",
"~\\clink"
],
Note the addition of --profile ... this allows the history to be persistent between vscode sessions.
I'm new to Visual Studio code so thanks in advance for the help.
I often use the PowerShell ISE with an interactive PowerShell window. I open it, type a few commands, then run a script (such as opening a PowerShell session with Office 365) and then type a few more commands.
Is there any way to do the same thing with Visual Studio code? I see a way to run scripts and debug them but not have them complete but the PowerShell window still stay around so I can continue working.
Decided to convert to answer, since this is a bit too much for comments:
Go to settings > user settings and change "terminal.integrated.shell.windows" to:
"C:\\WINDOWS\\Sysnative\\WindowsPowerShell\\v1.0\\powershell.exe",
Ctrl + Shift + ` opens the terminal. Using the + button on the terminal window you can launch additional powershell instances.
Also there's an extension that allows for launching several different terminals under the same VSCode process (cmd,powershell,bash,etc). Minor nuance, shells are being started with the same permissions VSCode has, but you can re-elevate once inside the shell.
References:
https://code.visualstudio.com/docs/editor/integrated-terminal
https://code.visualstudio.com/docs/customization/userandworkspace
https://github.com/Microsoft/vscode/issues/10893
Edit: but I would say its not really convenient, terminal takes away too much space on the screen (well, unless you have like 2k or 4k display), so I use powershell + vscode.