Search code examples
powershellpowershell-7.0

Double angle bracket mode


I'm using PowerShell 7.1.3 and have noticed a strange behavior when entering a single quote to my prompt:

PS: '
>>
>>

The prompt is styled with >> and no matter what I type, nothing gets executed (or that's how it looks). I was wondering:

  • Is this activated by entering ' on the shell, or is this a side effect?
  • Is this mode called somehow?
  • Is this mode useful for a specific case?

Solution

  • Is this activated by entering ' on the shell, or is this a side effect?

    It's an intentional side-effect of entering an unmatched ' as a command, yes.

    It's technically not PowerShell itself, but rather the PSReadLine module (which ships with PowerShell 7 and is enabled by default).

    PSReadLine provides a number of command line editing improvements - including syntax highlighting, customizable tab- and menu-completion, and multi-line editing - and it works by continously feeding your (partial) input to PowerShell's parser, and then uses the results returned by the parser to provide the correct syntax highlighting etc.

    So when you press [Enter] after having typed a single ', PSReadLine sends your input to the parser like this:

    # Parse the input script the same way PSReadLine would
    $ParserErrors = @()
    $AST = [System.Management.Automation.Language.Parser]::ParseInput("'", [ref]$null, [ref]$ParserErrors)
    
    # Let's inspect any parser errors 
    $ParserErrors
    

    If you run this in PowerShell 7.1.3, you'll see the following errors were reported:

    Extent ErrorId                         Message                                  IncompleteInput
    ------ -------                         -------                                  ---------------
    '      TerminatorExpectedAtEndOfString The string is missing the terminator: '.            True
    

    Notice the last property of the error, IsInputIncomplete - PSReadLine sees that what you've typed in the prompt is incomplete - and it therefore assumes you're not done writing your command and continues the prompt on the next line for you.

    [...] no matter what I type, nothing gets executed

    That's not quite true - if you enter something that completes the input (eg. makes it valid PowerShell again), it'll let you submit it - in your case that would be by typing a closing ':

    PSReadLine prompt continuation behavior for Incomplete Input

    Is this mode called somehow?

    No idea, I tend to think of >> as a "prompt continuation indicator", but it's not really a distinct mode per se.

    Is this mode useful for a specific case?

    Well, as shown above it prevents you from trying to execute syntactically invalid or incomplete code, that's pretty useful in itself I think.

    The overall ability to do multi-line editing at the prompt is, in my personal opinion, one of the biggest strengths of PowerShell - you can start prototyping small functions and compose quite complex pipelines without having to switch back and forth between an editor.

    As beatcracker mentions, you can also get prompt continuation (at least in the default Windows Console Host) by pressing Shift+Enter - this works regardless of whether the existing input is incomplete or not.

    You can basically write and edit a whole script at the prompt if you wanted to.