I'm having an issue setting up aliases for github copilot.
gh copilot suggest and gh copilot explain work fine, I want to reduce them to ghcs and ghce.
When I run ghcs "create new dir" this is the error I get,
ghcs : The term 'ghcs' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ ghcs "new dir"
+ ~~~~
+ CategoryInfo : ObjectNotFound: (ghcs:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
CONTEXT I use VSC with Powershell on Windows, also just downloaded the Microsoft's Powershell extension, but it didn't help.
On github's manual page, they say it should be as simple as running this in the terminal gh copilot alias.
This created two files in my powershell folder: New Files in the folder
PS.
I've also tried to create a new powershell profile by New-Item -Force $PROFILE and copied this (this is what GH also suggested in their manual) $GH_COPILOT_PROFILE = Join-Path -Path $(Split-Path -Path $PROFILE -Parent) -ChildPath "gh-copilot.ps1" gh copilot alias -- pwsh | Out-File ( New-Item -Path $GH_COPILOT_PROFILE -Force ) echo ". $GH_COPILOT_PROFILE" >> $PROFILE, but still had no luck.
Thanks in advance! :)
tl;dr
Run the alias-installation commands detailed in the instructions once, in order to programmatically update your PowerShell
$PROFILEfile - do NOT add those instructions themselves to the latter.As of this writing, the generated "aliases" (which are actually wrapper
functions) work in PowerShell (Core) 7+ only. See below for a Windows PowerShell solution.Current problems:
As of this writing, there are two problems with the Copilot extension for
gh, the GitHub CLI:gh copilot alias -- pwshproduces PowerShell code that works in PowerShell (Core) 7+ only; your symptom implies that you ran it from Windows PowerShell.However, the code could easily be fixed to run in Windows PowerShell - see below.
See GitHub issue #32189.
The instructions for setting up aliases are confusing:
The code shown is setup code, meant to be run once in order to programmatically add the "aliases" (which are actually functions) to the profile / initialization files of supported shells, whereas the instructions, e.g. "Add the following to your PowerShell profile:", mistakenly suggest that the setup code itself should be added to the profile / initialization file.
See GitHub issue #32187.
Installing Windows PowerShell-compatible aliases (functions):
Hopefully, the need for the following workarounds will go away, once the Copilot extension is updated to be compatible with Windows PowerShell too - once that has happened, simply run the original setup code from the instructions again.
gh copilotcommands, you can update your$PROFILEas follows:gh copilot alias -- pwshgenerates, based on the version current as of this writing:The wrapper functions add support for debugging and, for
ghcs, the ability to execute a suggested command and add it to PowerShell's command history.General notes on profile loading:
Note that installing Visual Studio Code's PowerShell extension, which comes with its own special-purpose shell - the PIC (PowerShell Integrated Console) - changes the file path that the automatic
$PROFILEvariable points to, because the PIC uses a separate profile file.PowerShell: Enable Profile Loading; searching forprofile loadshould be enough to locate it.Therefore, installing the aliases from the PIC will not make them available in regular PowerShell console / Windows Terminal sessions, and also not in regular (non-PIC) PowerShell sessions in Visual Studio Code's integrated terminal.
To make all these environments share definitions, you have two options:
Either: Target
$PROFILE.CurrentUserAllHostsrather than just$PROFILE; the path that the former points is loaded by all PowerShell environments run by the current user, irrespective of what application hosts them.Or: Modify
$PROFILEfrom a regular (non-PIC) session, then add the following to the PIC-specific$PROFILEfile (open it for editing withpsedit $PROFILE), which simply loads the the.CurrentUserCurrentHostprofile that console-based (terminal-based) PowerShell sessions use:After modifying a profile, you must either start a new PowerShell session or explicitly reload it, e.g. with
. $PROFILE, using., the dot-sourcing operator.