Cmder with PowerShell in VSCode not registering keyboard shortcut - powershell

I was able to setup VSCode so that I use Cmder on top of powershell.exe.
I was hoping that this would allow me to use common keyboard shortcuts such as Ctrl+Backspace to remove words faster. Unfortunately, it only outputs '^W' each time I hit that shortcut. In the standard cmd.exe or Cmder outside of VSCode this shortcut works.
Any idea why that is? I was thinking that that I'm expecting form this might not even be something that's supposed to work.
Thanks!
Here're my settings for reference:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"terminal.integrated.shellArgs.windows": [
"-ExecutionPolicy",
"Bypass",
"-NoLogo",
"-NoProfile",
"-NoExit",
"-Command",
". 'C:\\Users\\..\\..\\Apps\\cmder_mini\\vendor\\profile.ps1'"
],

Related

VS Code terminal history search, Windows, Powershell

since version 1.43 the ctrl+r r keyboard shortcut has stopped working for powershell history searches. Is there another way to search in recently used commands?
Also see https://stackoverflow.com/a/70900927/836330 for how to bring up a QuickPick panel with the recent terminal commands (powershell is supported).
and
https://stackoverflow.com/a/62203544/836330 or https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_70.md#run-recent-command-as-a-replacement-for-reverse-search
I can't reproduce your issue. But you could try this keybinding in the meantime:
{
"key": "alt+r",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u0012" }
},
That sends a Ctrl+R to the terminal. Focus can be anywhere. That should trigger the reverse search of previous terminal commands. Does it do that for you?
See related info: Make a keybinding to run previous or last shell commands

Cmder integration in VS Code with PowerShell not working as expected

I'm trying to use Cmder in VS Code for PowerShell instead of powershell.exe. I followed the official instructions (here) on the cmder GitHub. Now, when I restart VS Code a terminal will be launched. Looks like this:
It's name is 'powershell' and it looks like Cmder but doesn't behave like it.
Perhaps I'm expecting something that is not designed to work but I was expecting 'CTRL+ Backspace' to delete words to the left. Instead I'm getting '^W':
Moreover, as soon as I open a .ps1 file a new terminal gets launched which is named 'PowerShell Integrated Console':
The behaviour of this terminal is the same as the 'powershell' one.
Here's my settings related to the integrated terminal and PowerShell in the settings.json:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"terminal.integrated.shellArgs.windows": [
"-ExecutionPolicy",
"Bypass",
"-NoLogo",
"-NoProfile",
"-NoExit",
"-Command",
". 'C:\\Users\\fhe\\[REMOVED]\\Apps\\cmder_mini\\vendor\\profile.ps1'"
],
"terminal.integrated.rendererType": "auto",
"terminal.integrated.scrollback": 10000,
"powershell.scriptAnalysis.enable": true,
"powershell.integratedConsole.focusConsoleOnExecute": false,
"powershell.enableProfileLoading": true,
I notice that if I change it back to what the official guide suggests under the 'Run Cmder as the VS Code terminal' (here) I will get the expected behaviour when using CTRL+Backspace.
So my questions are:
Is Cmder integration in VS Code designed to provide the CTRL+Backspace functionality for PowerShell?
If so, what might I have configured incorrectly?
Thanks.

Visual Studio Code - Ctrl+Backspace not working in Integrated Terminal

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" }
},

VSCode + Cmder integration - strange issue

could you tell me why while I integrate the vscode and cmder, like that:
"terminal.integrated.shell.windows": "cmd.exe",
"terminal.integrated.shellArgs.windows": ["/k", "%CMDER_ROOT%\\vendor\\init.bat"]
and use mini_cmder version, everything is working fine. But if I do it the same with full version of cmder, the "Open in terminal" option from contextual menu doesen't work. Anybody know how to resolve that?
Best Regards,
crova
Yes, to use the Cmder shell in VS Code, you need to create a vscode.bat file in your cmder path with the following contents:
#echo off
SET CurrentWorkingDirectory=%CD%
SET CMDER_ROOT=C:\cmder (your path to cmder)
CALL "%CMDER_ROOT%\vendor\init.bat"
CD /D %CurrentWorkingDirectory%
then in your VS Code user settings, add the following to your settings.json file:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/K", "C:\\cmder\\vscode.bat"]

How to use quotes in Visual Studio Code settings values?

I'm trying to add this as my default terminal: C:\Windows\System32\cmd.exe /k "C:\Program Files\nodejs\nodevars.bat" but how do I set the "/k "C:\Program Files\nodejs\nodevars.bat" part in terminal.integrated.shellArgs.windows settings. It doesn't allow for nested quotation marks. I tried escaping the quotation marks, double quotes etc. Each option the command prompt states the path set is invalid.
go to File menu > Preferences > workspace settings. On right hand side, you will see settings.json. In this file add below line-
"terminal.integrated.shell.windows": "C:\\Windows\\system32\\cmd.exe /k \"C:\\Program Files\\nodejs\\nodevars.bat\""
thats it. now you can run npm install and npm start commands to run your angular2 application.
see if this helps.
I think \" does not work for cmd shell args but this works for me:
"terminal.integrated.shell.windows": "C:\\Windows\\system32\\cmd.exe",
"terminal.integrated.shellArgs.windows": [
"/k",
"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat",
"&&start-ssh-agent"
],