Search code examples
powershellcommand-line-argumentstab-completion

Using tab-completion in the shell for suggested command


*"PowerShell provides completions on input to provide hints, enable discovery, and speed up input entry. Command names, parameter names, argument values and file paths can all be completed by pressing the Tab key." Using tab-completion in the shell

I'm no stranger with using tab to complete a command, but I can't figure out how to use the hints. For example, after typing Connect-ExchangeOnline it shows me greyed out text of an argument I used recently.

Connect-ExchangeOnline -UserPrincipalName <[email protected]>

However, pressing tab cycles through the list of arguments for the command. I know I can use Ctrl + Space to see all the arguments, but how do I use the hinted argument it suggests?

Example

Google - I was expecting to find the answer, but I'm not quite sure how to describe this...


Solution

  • The grayed out suggestion is an inline prediction, using your command history as the prediction source (other sources can be installed).

    From Using predictors in PSReadLine:

    Pressing [the] RightArrow key accepts an inline suggestion.

    Specifically, it accepts the whole suggestion and places the cursor at the end of the line (without submitting it, so you can still edit it).

    You can customize the key bindings via Set-PSReadLineKeyHandler, by mapping a key (chord) to the following PSReadLine functions:[1]

    • AcceptSuggestion ... exhibits the behavior described above.

      • ForwardChar does too, and that is the one that Right arrow is bound to by default.
    • AcceptNextSuggestionWord ... only accepts the next word from the inline suggestion being offered.

      • ForwardWord does too, but it is only bound to a key chord by default on Unix-like platforms (Alt+f), not on Windows (where AcceptNextSuggestionWord isn't mapped either).

      • To define Alt+f, for instance, on Windows too, use the following:

        Set-PSReadLineKeyHandler -Chord Alt+f -Function AcceptNextSuggestionWord
        

    If you prefer to have predictions presented in a list, so that you can select an entry using the up- an down-arrow keys (which invariably uses the whole prediction):

    • Press F2 to toggle between inline and list view, in-session (non-persistently).

    • To default to list view in future session, place the following in your $PROFILE file:

      Set-PSReadLineOption -PredictionViewStyle ListView
      

    [1] See about_PSReadLine_Functions for a list of all functions, along with descriptions.