I am at a loss as to why my terminal in VSCode while using PowerShell has stopped running commands like it use to. I highlight a few commands in the editor and then press F8. I believe the issue has something to do with VSCode Terminal. Integrated vs External.
What I expect:
- Highlight code and press F8 key
- Terminal displays and runs all code that was highlighted
- Terminal displays all output of the commands run
What I am now getting:
- Highlight code and press F8 key
- Terminal displays and runs first line of code
- If there is any output, the terminal displays it
- Terminal displays and runs next line of code
- If there is any output, the terminal displays it
- Repeat steps 4-5 until all code has run
Test Code that is highlighted and run using F8:
$a = 5
Write-Host $a
$b = $a + 5
$c = $b + 5
Write-Host $c
What my terminal shows now:
PS C:_MyPSModuleshab_hpe_win_driverpack> $a = 5
PS C:_MyPSModuleshab_hpe_win_driverpack> Write-Host $a
5
PS C:_MyPSModuleshab_hpe_win_driverpack> $b = $a + 5
PS C:_MyPSModuleshab_hpe_win_driverpack> $c = $b + 5
PS C:_MyPSModuleshab_hpe_win_driverpack> Write-Host $c
15
PS C:_MyPSModuleshab_hpe_win_driverpack>
What the terminal use to/should show:
PS C:_MyPSModuleshab_hpe_win_driverpack> $a = 5
Write-Host $a
$b = $a + 5
$c = $b + 5
Write-Host $c
5
15
2
Answers
Your symptom implies one of two things:
Either: The PIC (PowerShell Integrated Console) that comes with Visual Studio Code’s PowerShell extension – which is a special-purpose edition of PowerShell that facilitates editing and debugging of PowerShell code – is not the active shell in Visual Studio Code’s integrated terminal at the time you’re pressing F8
If it is the active shell (see below for how to verify), the selection as a whole is submitted to the PowerShell instance running in the PIC, which is what you expect.
Otherwise, the selection is submitted line by line to whatever shell happens to be running in Visual Studio Code’s integrated terminal.
Or: A different Visual Studio Code extension has remapped the F8 key to submit the active editor’s selection line by line to the active shell.
To see if it is the still the PowerShell extension’s F8 key mapping is in effect, execute "PowerShell: Run Selection" from the command palette (Ctrl-Shift-P) and see it shows "F8" to the right.
Conversely, to see what F8 is currently bound to, execute
"Preferences: Open Keyboard Shortcuts" from the command palette (Ctrl-K, Ctrl-S), click on the icon to the right of the search field and press F8, and see what command(s) show up with the word "editorTextFocus" in the "When" column.
If a different extension has indeed remapped F8, either uninstall that extension or remap the key as desired.
How to determine if the PIC is the active shell:
Programmatically:
Get-Module PowerShellEditorServices.Commands
in the integrated terminal:PowerShellEditorServices.Commands
implies that the PIC is running.Visually, looking at the top-right corner / the rightmost panel of the integrated terminal:
Look for the "PowerShell Extension" shell:
If only one shell is running, look for this:
If multiple shells are running, look for this entry in the stack of running shells and see if it is the selected (active) one; e.g.:
It looks like this will be fixed in vscode v1.75, see Release Notes: bracket paste mode:
The commands in a multiline selection will be run as a group rather than individual commands.