Skip to content

Instantly share code, notes, and snippets.

@jerkovicl
Last active April 2, 2023 11:36
Show Gist options
  • Save jerkovicl/c658a1a10155bb7d1b22dcda6133531b to your computer and use it in GitHub Desktop.
Save jerkovicl/c658a1a10155bb7d1b22dcda6133531b to your computer and use it in GitHub Desktop.
How to set alias in powershell for Github Copilot CLI
function checkResponse {
  if ( $Args[0] -eq $true ) {
    $fileContents = Get-Content $tempFile
    Write-Host $fileContents
    # write contents of file to console (basically EVAL())
    Invoke-Expression $fileContents
  }
  else {
    Write-Host "User cancelled the command."
  }
  # clear file contents
  Set-Content $tempFile ""
}

function githubCopilotCommand {
  $tempFile = "c:\temp\copilot.txt"
  $firstItem, $remaining = $Args
  if ( $Args[0] -eq "-help" ) {
    $codeToRun = "github-copilot-cli help"
    Invoke-Expression $codeToRun
  }
  elseif ( $Args[0] -eq "-g" -or $Args[0] -eq "-git" ) {
    Write-Host "github-copilot-cli git-assist --shellout $tempFile $remaining"
    github-copilot-cli git-assist --shellout $tempFile $remaining
    checkResponse $?
  }
  elseif ( $Args[0] -eq "-gh" -or $Args[0] -eq "-github" ) {
    Write-Host "github-copilot-cli gh-assist --shellout $tempFile $remaining"
    github-copilot-cli gh-assist --shellout $tempFile $remaining
    checkResponse $?
  }
  else {
    Write-Host "github-copilot-cli what-the-shell --shellout $tempFile powershell $Args"
    github-copilot-cli what-the-shell --shellout $tempFile powershell $Args
    checkResponse $?
  }
}

Set-Alias /cp githubCopilotCommand
Set-Alias copilot githubCopilotCommand

# To persist above, add to PS Profile by doing the following
cd $env:USERPROFILE\Documents
md PowerShell -ErrorAction SilentlyContinue
cd PowerShell
New-Item Microsoft.PowerShell_profile.ps1 -ItemType "file" -ErrorAction SilentlyContinue

# open with PowerShell ISE to edit
powershell_ise.exe .\Microsoft.PowerShell_profile.ps1
# OR open with vscode to edit
code .\Microsoft.PowerShell_profile.ps1

# make sure you save the .ps1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment