skip to Main Content

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:

  1. Highlight code and press F8 key
  2. Terminal displays and runs all code that was highlighted
  3. Terminal displays all output of the commands run

What I am now getting:

  1. Highlight code and press F8 key
  2. Terminal displays and runs first line of code
  3. If there is any output, the terminal displays it
  4. Terminal displays and runs next line of code
  5. If there is any output, the terminal displays it
  6. 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


  1. 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.

        • If that shell happens to be PowerShell too (running as a general-purpose shell), the submitted code may or may not work, depending on what the selection contains.
    • 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 keyboard icon 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:

      • Run Get-Module PowerShellEditorServices.Commands in the integrated terminal:
        • (Non-error) output 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:

          • screenshot one shell
        • 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.:

          • screenshot multiple shells
    Login or Signup to reply.
  2. It looks like this will be fixed in vscode v1.75, see Release Notes: bracket paste mode:

    Bracketed paste mode used in "Run Selected Text In Active Terminal"

    The Run Selected Text In Active Terminal command will now run the
    text using "bracketed paste mode" in supporting shells, so a
    multi-line selection will be treated as a single input, rather than
    multiple commands. This makes running actual scripts much more
    intuitive with less errors occuring.

    The commands in a multiline selection will be run as a group rather than individual commands.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search