我在为 github copilot 设置别名时遇到问题。
gh copilot suggest
和 gh copilot explain
工作正常,我想将它们减少为 ghcs
和 ghce
。
当我运行
ghcs "create new dir"
时,这是我收到的错误,
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
背景 我在 Windows 上使用 VSC 和 Powershell,也刚刚下载了 Microsoft 的 Powershell 扩展,但没有帮助。
在 github 的手册页上,他们说它应该像在终端中运行一样简单
gh copilot alias
。
PS。 我还尝试通过
New-Item -Force $PROFILE
创建一个新的 powershell 配置文件并复制此内容(这是 GH 在其手册中也建议的内容)$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
,但仍然没有运气。
提前致谢! :)
tl;博士
运行说明中详细介绍的别名安装命令一次,以便以编程方式更新您的PowerShell
$PROFILE
文件 - 不要将这些指令本身添加到后者。
在撰写本文时,生成的“别名”(实际上是包装器
function
s)仅适用于PowerShell(核心)7+。请参阅下文了解 Windows PowerShell 解决方案。
在撰写本文时,GitHub CLI的
Copilot 扩展
gh
存在两个问题:
gh copilot alias -- pwsh
生成仅适用于 PowerShell (Core) 7+ 的 PowerShell 代码;您的症状表明您是从 Windows PowerShell 运行的。
如果您对底层
gh copilot
$PROFILE
:
$GH_COPILOT_PROFILE = Join-Path (Split-Path $PROFILE) gh-copilot.ps1
@'
function ghce { gh copilot explain @args }
function ghcs { gh copilot suggest @args }
'@ >> (New-Item -FORCE $GH_COPILOT_PROFILE)
". $GH_COPILOT_PROFILE" >> $PROFILE
如果您想使用
gh copilot alias -- pwsh
$GH_COPILOT_PROFILE = Join-Path (Split-Path $PROFILE) gh-copilot.ps1
@'
function ghcs {
# Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
param(
[ValidateSet('gh', 'git', 'shell')]
[Alias('t')]
[String]$Target = 'shell',
[Parameter(Position=0, ValueFromRemainingArguments)]
[string]$Prompt
)
begin {
# Create temporary file to store potential command user wants to execute when exiting
$executeCommandFile = New-TemporaryFile
# Store original value of GH_DEBUG environment variable
$envGhDebug = $Env:GH_DEBUG
}
process {
if ($PSBoundParameters['Debug']) {
$Env:GH_DEBUG = 'api'
}
gh copilot suggest -t $Target -s "$executeCommandFile" $Prompt
}
end {
# Execute command contained within temporary file if it is not empty
if ($executeCommandFile.Length -gt 0) {
# Extract command to execute from temporary file
$executeCommand = (Get-Content -Path $executeCommandFile -Raw).Trim()
# Insert command into PowerShell up/down arrow key history
[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($executeCommand)
# Insert command into PowerShell history
$now = Get-Date
$executeCommandHistoryItem = [PSCustomObject]@{
CommandLine = $executeCommand
ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::NotStarted
StartExecutionTime = $now
EndExecutionTime = $now.AddSeconds(1)
}
Add-History -InputObject $executeCommandHistoryItem
# Execute command
Write-Host "`n"
Invoke-Expression $executeCommand
}
# Clean up temporary file used to store potential command user wants to execute when exiting
Remove-Item -Path $executeCommandFile
# Restore GH_DEBUG environment variable to its original value
$Env:GH_DEBUG = $envGhDebug
}
}
function ghce {
# Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
param(
[Parameter(Position=0, ValueFromRemainingArguments)]
[string[]]$Prompt
)
begin {
# Store original value of GH_DEBUG environment variable
$envGhDebug = $Env:GH_DEBUG
}
process {
if ($PSBoundParameters['Debug']) {
$Env:GH_DEBUG = 'api'
}
gh copilot explain $Prompt
# Restore GH_DEBUG environment variable to its original value
$Env:GH_DEBUG = $envGhDebug
}
}
'@ >> (New-Item -FORCE $GH_COPILOT_PROFILE)
". $GH_COPILOT_PROFILE" >> $PROFILE
ghcs
能够执行建议的命令并将其添加到 PowerShell 的命令历史记录中。
请注意,仅对原始代码应用了最小的更改集;简而言之:
clean
块的内容(仅 PowerShell 7+ 支持)已移至 end
/
process
块中。配置文件加载的一般注意事项:
PowerShell 扩展(它带有自己的专用 shell - PIC(PowerShell 集成控制台)) - 会更改 automatic $PROFILE
变量 指向的文件路径,因为 PIC 使用单独的
profile文件。 此外,可以在 PIC 中禁用
PowerShell: Enable Profile Loading
;搜索 profile load
应该足以找到它。
要使所有这些环境
要么:目标
$PROFILE.CurrentUserAllHosts
$PROFILE
;前者指向的路径由当前用户运行的所有
PowerShell 环境加载,无论托管它们的应用程序是什么。或者:从常规(非 PIC)会话修改
$PROFILE
$PROFILE
文件(使用
psedit $PROFILE
打开它进行编辑),这只需加载
.CurrentUserCurrentHost
基于控制台(基于终端)的 PowerShell 会话使用的配置文件:
. ($PROFILE -replace '\.VSCode_', '.PowerShell_')
. $PROFILE
.
,点采购运算符
。