You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support bash style alias's to support tab completion in command string aliases #12962
Perhaps a new kind of alias that will funnel tab completion all the way down to the alias'd command string?
Set-Alias gb 'git branch'-AliasType CommandString # vs ... CommandLiteral or something... naming is hard but you get the idea
Reacted by Cologler, RedDragonWebDesign, Marco Kamner, Peter Lorenzen, Stéphane BARIZIEN, ratijas, Rob Cannon, Aditya Thakral, Matthew A Cargile, Marcel Arndt and 7 more
This is pretty much the first thing people (including myself) try to do with aliases. I've seen countless threads/discord conversations with a ton of folks tripping on this.
I'm tempted to say that Set-Alias should do this automatically if the definition:
Has spaces
Doesn't resolve to a existing command
Has a first "part" that does resolve
Reacted by Thomas Nieto, Rain Sallow (/u/ta11ow), TylerLeonhardt, Martin Gill, RedDragonWebDesign, ratijas, Matthew A Cargile, Michael Klement and Sam Erde
This is pretty much the first thing people (including myself) try to do with aliases. I've seen countless threads/discord conversations with a ton of folks tripping on this.
Really? The first thing you do is to do complex aliasing?
An alias is a short cut to a command name only and has been since V1. And in all the years I have moderated the PowerShell forum on Spiceworks this is not a highly requested feature. There have been requests but certainly not many.
I'd say this might be a nice to have - but only if the implementation does not break things.
How does the AST capture something like set-alias gb 'git branch', what gets passed in to existing mechanisms for argument completion or pseudoparameterbinding?
What happens in the following cases:
set-alias gb 'git bran'
set-alias gc 'git commit --a'
set-alias gc 'git commit --'
set-alias gi 'get-item -path'
set-alias gi 'get-item -f'
set-alias gi 'get-item -'
set-alias gi 'get-item @('
Basically, I'd like to understand where the breakdown lies between PowerShell's parser/AST design vs bash's expand and execute approach. Once we have a better understanding of that, we'll be better placed to evaluate whether this feature is achievable.
Rob Holt (@rjmholt) I don't think the parser would need to change. Just the parameter binder and possibly a new CommandInfo (or alternatively, just some different code in AliasInfo.Parameters). Well I say "just" but it's a pretty substantial work item, though I think it's worth it.
For native command argument completers, it might be fine to just expand the text and reparse before invoking them. Kinda dirty, but maybe doable just for completions.
Want to increase developer productivity? Then create terminal aliases! Check out this guide to learn how to create bash aliases for git commands and more, making full use of your `.bash_profile`.
Reacted by Rain Sallow (/u/ta11ow), Patrick Meinecke, Martin Gill and ratijas
Really? The first thing you do is to do complex aliasing?
I'm pretty sure the first alias I tried to make way back when was to ipconfig /all. I know that fits well into a function as well, but I don't think I'd call Set-Alias dap 'dotnet add package' all that complex.
And in all the years I have moderated the PowerShell forum on Spiceworks this is not a highly requested feature. There have been requests but certainly not many.
Haven't heard of Spiceworks, but I see it a lot on reddit/discord. It's up there with Select-Object -Property vs ExpandProperty in starter walls.
I'd say this might be a nice to have - but only if the implementation does not break things.
That's fair, luckily I'm pretty sure breaks won't be needed for it.
That would work for many cases, but doesn't handle 'weird' syntax, so you probably still need to have --% for those any way. I like the & --% idea as it reuses the existing operator.
It would handle it just the same way as you'd need to handle it even if there wasn't a separate -Parameters thing to work with.
You'd probably have to wrap everything in quotes there; you can't directly use %-- for this since you're passing data to a cmdlet, not a native executable, so you don't really have the option of not using PS's parsing rules for the input.
Really? The first thing you do is to do complex aliasing?
Yes. People who came from bash/zsh/fish will do this.
I'd like to add, this difference in mindsets comes from the fact that commands in UNIX land (unlike PowerShell) are usually already short enough that you won't need to alias them. So the primary use case indeed is for stacking up some commonly used arguments.
This brings a performance penalty in the case of pipeline input - if I run dir c:\ -s | topeonef it will get to wait for the base stream completes (Compare to that - dir c:\ -s | Selelct-Object -First 1 works just fine doing early termination)
More in #15855 on the technical challenge to get parity in function
Supporting Bash-style alias definitions would provide the following benefits:
Ease of definition, closer alignment with POSIX-compatible shells.
The most frictionless syntax is the one suggested by Patrick Meinecke (@SeeminglyScience)above, so that, say, Set-alias gb 'git branch' would just work, with no need for additional parameters.
If implemented as such, such enhanced aliases could provide implicit pipeline-input support with support for streaming processing (line by line), whereas using a function to support that currently requires a cumbersome implementation as a proxy function based on a steppable pipeline (because a function based on $input invariably collects all input up front; note that use of a filter is not an option in this case).
The tab-completion aspect is really an independent one, and for current aliases, which are merely name mappings, already works for PowerShell target commands, but not for external (native) programs.
For instance, say you've used Register-ArgumentCompleter to define completions for pwsh, and you've also defined Set-Alias psh pwsh; currently, alias psh then unexpectedly does not use the tab-completions defined for pwsh.
Given that the proposal at hand also requires the enhanced aliases to know the external executable name, fixing the above asymmetry could also provide tab-completion support for enhanced aliases.
Another benefit would be that the PSScriptAnalyzer is able to identify and (automatically) replace aliases (in an IDE as VSCode) in formal scripts.
Besides, a possible other way to implement such a feature could be by adding a -Definition parameter that is mutual exclusive with the -Value parameter, also resides at position 1, and only accepts ScriptBlocks. Which would than support syntaxes as:
Set-Alias gc { git -parameters commit,"--a" }
(I guess this is also safer which regards to code injections)
Activity
p0W3RH311 commentedon Jun 14, 2020
and if someone add gb alias it shadow your gb function. dont work as expected.
SeeminglyScience commentedon Jun 16, 2020
This is pretty much the first thing people (including myself) try to do with aliases. I've seen countless threads/discord conversations with a ton of folks tripping on this.
I'm tempted to say that
Set-Aliasshould do this automatically if the definition:doctordns commentedon Jun 16, 2020
Really? The first thing you do is to do complex aliasing?
An alias is a short cut to a command name only and has been since V1. And in all the years I have moderated the PowerShell forum on Spiceworks this is not a highly requested feature. There have been requests but certainly not many.
I'd say this might be a nice to have - but only if the implementation does not break things.
rjmholt commentedon Jun 16, 2020
If we pull on this string a bit:
set-alias gb 'git branch', what gets passed in to existing mechanisms for argument completion or pseudoparameterbinding?set-alias gb 'git bran'set-alias gc 'git commit --a'set-alias gc 'git commit --'set-alias gi 'get-item -path'set-alias gi 'get-item -f'set-alias gi 'get-item -'set-alias gi 'get-item @('Basically, I'd like to understand where the breakdown lies between PowerShell's parser/AST design vs bash's expand and execute approach. Once we have a better understanding of that, we'll be better placed to evaluate whether this feature is achievable.
SeeminglyScience commentedon Jun 16, 2020
Rob Holt (@rjmholt) I don't think the parser would need to change. Just the parameter binder and possibly a new
CommandInfo(or alternatively, just some different code inAliasInfo.Parameters). Well I say "just" but it's a pretty substantial work item, though I think it's worth it.For native command argument completers, it might be fine to just expand the text and reparse before invoking them. Kinda dirty, but maybe doable just for completions.
TylerLeonhardt commentedon Jun 16, 2020
Thomas Lee (@doctordns)
Yes. People who came from bash/zsh/fish will do this.
Take a look at this guide:
https://www.netlify.com/blog/2020/04/12/speed-up-productivity-with-terminal-aliases/
every single example is complex in the sense that it's a command string and not a particular command.
SeeminglyScience commentedon Jun 16, 2020
I'm pretty sure the first alias I tried to make way back when was to
ipconfig /all. I know that fits well into a function as well, but I don't think I'd callSet-Alias dap 'dotnet add package'all that complex.Haven't heard of Spiceworks, but I see it a lot on reddit/discord. It's up there with
Select-Object -PropertyvsExpandPropertyin starter walls.That's fair, luckily I'm pretty sure breaks won't be needed for it.
rjmholt commentedon Jun 16, 2020
/cc James Truher (@JamesWTruher) Dongbo Wang (@daxian-dbw) Steve Lee (@SteveL-MSFT)
rkeithhill commentedon Jul 8, 2020
Perhaps a more PowerShelly way to implement this would be to add a parameter like
-Parameterse.g:doctordns commentedon Jul 8, 2020
That would work for many cases, but doesn't handle 'weird' syntax, so you probably still need to have --% for those any way. I like the & --% idea as it reuses the existing operator.
vexx32 commentedon Jul 8, 2020
It would handle it just the same way as you'd need to handle it even if there wasn't a separate
-Parametersthing to work with.You'd probably have to wrap everything in quotes there; you can't directly use
%--for this since you're passing data to a cmdlet, not a native executable, so you don't really have the option of not using PS's parsing rules for the input.7 remaining items
ratijas commentedon Jun 26, 2021
I'd like to add, this difference in mindsets comes from the fact that commands in UNIX land (unlike PowerShell) are usually already short enough that you won't need to alias them. So the primary use case indeed is for stacking up some commonly used arguments.
This one might as well be my favorite one:
Thomas Lee (@doctordns)
liang2zhu1 commentedon Aug 2, 2021
Just want to add a pressing scenario that can't easily implement without aliasing -
Ttrying to implement a 'Take top one' function and following would only work functionally but have bad perf.
This brings a performance penalty in the case of pipeline input - if I run
dir c:\ -s | topeonefit will get to wait for the base stream completes (Compare to that -dir c:\ -s | Selelct-Object -First 1works just fine doing early termination)More in #15855 on the technical challenge to get parity in function
iSazonov commentedon Aug 3, 2021
"filter" function works more fast. :-)
kilasuit commentedon Feb 13, 2024
reopening as I think there is value in having this in future irrespective of however it gets implemented
mklement0 commentedon May 6, 2024
Let me try a summary of sorts:
Supporting Bash-style alias definitions would provide the following benefits:
Ease of definition, closer alignment with POSIX-compatible shells.
Set-alias gb 'git branch'would just work, with no need for additional parameters.If implemented as such, such enhanced aliases could provide implicit pipeline-input support with support for streaming processing (line by line), whereas using a
functionto support that currently requires a cumbersome implementation as a proxy function based on a steppable pipeline (because a function based on$inputinvariably collects all input up front; note that use of afilteris not an option in this case).The tab-completion aspect is really an independent one, and for current aliases, which are merely name mappings, already works for PowerShell target commands, but not for external (native) programs.
For instance, say you've used
Register-ArgumentCompleterto define completions forpwsh, and you've also definedSet-Alias psh pwsh; currently, aliaspshthen unexpectedly does not use the tab-completions defined forpwsh.This asymmetry should be fixed: Provide the tab-completions of an alias' target command on the alias itself also when targeting native programs #21609
Given that the proposal at hand also requires the enhanced aliases to know the external executable name, fixing the above asymmetry could also provide tab-completion support for enhanced aliases.
iRon7 commentedon Jul 3, 2025
Another benefit would be that the PSScriptAnalyzer is able to identify and (automatically) replace aliases (in an IDE as VSCode) in formal scripts.
Besides, a possible other way to implement such a feature could be by adding a
-Definitionparameter that is mutual exclusive with the-Valueparameter, also resides at position1, and only accepts ScriptBlocks. Which would than support syntaxes as:(I guess this is also safer which regards to code injections)