Home > OS >  how to get tab autocompletion to echo all possible options like bash?
how to get tab autocompletion to echo all possible options like bash?

Time:11-04

In bash there's a nice feature that when a string is partially completed (like a path) and we use tab for autocompletion, we get ALL the possible options echoed out at once to aid the next character we might want to type.

In PowerShell, it just cycles through one option at a time, so you are relatively blind where numerous possibilities are involved.

Is equivalent behaviour possible in PowerShell?

CodePudding user response:

Use Set-PSReadLineKeyHandler to redefine what happens when you press Tab as follows:

Set-PSReadLineKeyHandler Tab MenuComplete

Note that you'll have to add this command to your $PROFILE file in order to make it take effect in future sessions too.


The behavior of the MenuComplete function is as follows:

  • If there is no possible completion, no action is taken.
  • If there is only one possible completion, it is performed instantly, in-line.
  • If there are two or more possible completions, an interactive menu of the possible completions is shown below the input line:[1]
    • Use Tab or the arrow keys to cycle through the completions and press Enter to select one, which completes what was typed based on that selection and closes the menu.
      • Alternatively, continue typing, which prefix-matches against the parameter names and shrinks the menu dynamically (delete characters to expand it again).
    • Note that the highlighted menu item also shows that parameter's data type below the menu.
      • Once a parameter name has been completed, if you want data-type information plus a verbal description of that parameter, you can use the ShowParameterHelp function, available in PowerShell (Core) 7.2 , which is bound to Alt h by default.

Additional PSReadLine tips:

  • To see a list of available functions:

    • Consult the about_PSReadLine_Functions help topic; if it isn't available locally (with Get-Help about_PSReadLine_Functions), run Update-Help -Module PSReadLine.

    • To list all functions grouped by functional category, using Get-PSReadLineKeyHandler:

      Get-PSReadLineKeyHandler -Bound -Unbound
      
    • To see a flat list sorted by function name instead:

      Get-PSReadLineKeyHandler -Bound -Unbound |
        Sort-Object Function |
        Select-Object Function, Key, Description
      
  • To check what key chord(s) a given function is currently bound to:

    # This example looks for function names that contain the word "menu"
    # Use -eq with a full function name, if known.
    Get-PSReadLineKeyHandler | Where-Object Function -like *Menu*
    
  • To discover what function is currently bound to a given key chord:

    • Use the WhatIsKey function, which on Windows is bound to Alt ? by default; on Unix-like platforms, where the range of usable key chords is limited, there is no default binding, but you can define one as follows, using Alt w as an example:

      Set-PSReadLineKeyHandler Alt w WhatIsKey
      
    • Pressing the key chord that WhatIsKey is bound to then prompts for pressing the key chord of interest, which, if it is bound, prints the bound function name and its description.


[1] The possible completions are not shown in alphabetical order; instead, the order appears to be based on the parameter definition order, combined with some additional logic: an exact match is listed first, alias names are listed after their original names, dynamic parameters are listed after static ones, and matching common parameter names come last.

  • Related