Last active
August 15, 2024 23:41
-
-
Save MattJeanes/839c7adfef085f4b6001927e3eb96fd4 to your computer and use it in GitHub Desktop.
Alias commands to use GitHub Copilot CLI in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You should insert this script into your PowerShell Profile script so it exists in every session | |
# Fun fact: This script was mostly generated by ChatGPT by giving it the bash version of the output | |
# from `github-copilot-cli alias -- "$0"` with a few fixes from me | |
function Invoke-CopilotWhatTheShell { | |
$TMPFILE = New-TemporaryFile; | |
try { | |
github-copilot-cli what-the-shell $args --shellout $TMPFILE | |
if ($LASTEXITCODE -eq 0) { | |
if (Test-Path $TMPFILE) { | |
$FIXED_CMD = Get-Content -Raw $TMPFILE; | |
Invoke-Expression $FIXED_CMD; | |
} | |
else { | |
Write-Host "Apologies! Extracting command failed"; | |
} | |
} | |
else { | |
Write-Error "Apologies! Copilot failed to generate a command"; | |
} | |
} | |
finally { | |
Remove-Item $TMPFILE; | |
} | |
} | |
Set-Alias '??' 'Invoke-CopilotWhatTheShell'; | |
Set-Alias 'wts' 'Invoke-CopilotWhatTheShell'; | |
function Invoke-CopilotGitAssist { | |
$TMPFILE = New-TemporaryFile; | |
try { | |
github-copilot-cli git-assist $args --shellout $TMPFILE | |
if ($LASTEXITCODE -eq 0) { | |
if (Test-Path $TMPFILE) { | |
$FIXED_CMD = Get-Content -Raw $TMPFILE; | |
Invoke-Expression $FIXED_CMD; | |
} | |
else { | |
Write-Host "Apologies! Extracting command failed"; | |
} | |
} | |
else { | |
Write-Error "Apologies! Copilot failed to generate a command"; | |
} | |
} | |
finally { | |
Remove-Item $TMPFILE; | |
} | |
} | |
Set-Alias 'git?' 'Invoke-CopilotGitAssist'; | |
function Invoke-CopilotGitHubAssist { | |
$TMPFILE = New-TemporaryFile; | |
try { | |
github-copilot-cli gh-assist $args --shellout $TMPFILE | |
if ($LASTEXITCODE -eq 0) { | |
if (Test-Path $TMPFILE) { | |
$FIXED_CMD = Get-Content -Raw $TMPFILE; | |
Invoke-Expression $FIXED_CMD; | |
} | |
else { | |
Write-Host "Apologies! Extracting command failed"; | |
} | |
} | |
else { | |
Write-Error "Apologies! Copilot failed to generate a command"; | |
} | |
} | |
finally { | |
Remove-Item $TMPFILE; | |
} | |
} | |
Set-Alias 'gh?' 'Invoke-CopilotGitHubAssist'; |
I've updated the script with your suggestion @qqii, thank you!
@MattJeanes
Been trying to use Copilot CLI for almost a month now and I just came across this and it worked!
Thank you!
Thanks Matt, it's working fine 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@halfprice06, @MattJeanes
To support multi line commands, add
-Raw
toGet-Concent
.