How to Enable Substring Matching in PowerShell Tab Completion? - Stack Overflow

I'm trying to modify PowerShell's tab completion behavior so that it doesn't just match

I'm trying to modify PowerShell's tab completion behavior so that it doesn't just match from the beginning of a word but also allows substring matching

Example

  • Normally, if I type under<TAB>, PowerShell won't suggest code-understanding because it doesn't start with under
  • I would like PowerShell to suggest any word that contains my input anywhere (e.g., code-understanding, file-underlined, etc.)

What I've Tried

I've already set up PSReadLine enhancements for better menu-style completion :

Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete

This improves predictions but still only matches the start of words

My Question

Is there a native way in PowerShell 7+ to make TAB completion match substrings instead of just the start of a word ?

I'm trying to modify PowerShell's tab completion behavior so that it doesn't just match from the beginning of a word but also allows substring matching

Example

  • Normally, if I type under<TAB>, PowerShell won't suggest code-understanding because it doesn't start with under
  • I would like PowerShell to suggest any word that contains my input anywhere (e.g., code-understanding, file-underlined, etc.)

What I've Tried

I've already set up PSReadLine enhancements for better menu-style completion :

Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete

This improves predictions but still only matches the start of words

My Question

Is there a native way in PowerShell 7+ to make TAB completion match substrings instead of just the start of a word ?

Share Improve this question asked Feb 2 at 22:46 TheSmartMonkeyTheSmartMonkey 1,0921 gold badge13 silver badges29 bronze badges 5
  • 1 There isn't a direct, built-in configuration setting to achieve substring matching with tab completion in PowerShell 7+. – user29469970 Commented Feb 2 at 23:20
  • 3 Is typing *code<TAB> undesirable? – Mathias R. Jessen Commented Feb 3 at 13:03
  • Yes it is. Every day I work on 100+ different repo and I never remember there exact names. All the time I am like I need to go to the "auth" repo I know there is auth in it but I don't remember the start. Also all my repo start by the name of the company that I don't want to type each time – TheSmartMonkey Commented Feb 4 at 18:23
  • 3 Well you don't have to, you just have to type * :) – Mathias R. Jessen Commented Feb 4 at 19:00
  • oh ok sorry my bad I didn't understand – TheSmartMonkey Commented Feb 4 at 19:09
Add a comment  | 

1 Answer 1

Reset to default 1

tl;dr

Mathias has provided the crucial pointer:

PowerShell's tab completion (aka command completion) supports wildcard expressions so that - rather than trying to roll a complication custom solution - you can simply prefix your substring with *; to use your example:

*underTab will complete to both code-understanding and file-underlined (repeatedly pressing Tab cycles through the matches; with your custom Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete, all completions will be presented in a menu).


Background information:

Wildcard-based command completion:
  • With literal strings, PowerShell's command completion uses prefix matching, i.e. the input string must match the start of commands.

    • Expressed in wildcard terms, underTab behaves like (Get-Command).Name -like 'under*', i.e. a trailing * is implied.
  • Wildcard expressions are matched as such (except that a trailing * is always implied), and a preceding * therefore effectively performs substring matching, i.e. the literal part is matched anywhere in command names.

    • You can even use wildcard metacharacters multiple times; e.g., *readl*handlerTab completes to Get-PSReadLineKeyHandler, Remove-PSReadLineKeyHandler, and Set-PSReadLineKeyHandler.
Capitals-based command completion:

PowerShell (Core) 7 offers a little-known command-completion method based on matching capital letters in command names.

E.g., t-mm completes Test-ModuleManifest, but note the following:

  • The capital of the verb part of the command name (Test in this example) must be followed by -

  • Every capital letter must be specified (in order) in order to match a command name; e.g., s-t completes to Start-Transcript and Stop-Transcript, but not to Start-ThreadJob, because the latter has one additional capital, J (that is, s-tj is needed to match it).

  • This approach can not be combined with wildcards.

Also note that many built-in cmdlets come with superficially similar aliases that use a standardized alias prefix that is defined for each approved verb, e.g. sa for Start-, followed by - without an intervening - - a non-standardized (sequence of) letter(s) for the noun part. E.g., jb is used to represent the Job part in Start-Job.
That is, submitting alias sajb as-is is an alternative to using capital-based command completion with
s-jTab.
To see which alias(es), if any, are defined for a given command, use
Get-Alias -Definition <command-name>


[1] Note that the completion works even with non-approved verbs.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745254490a4618873.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信