Skip to content

Instantly share code, notes, and snippets.

@OnesimusUnbound
Last active June 10, 2024 00:15
Show Gist options
  • Save OnesimusUnbound/b425c0be362473bcdae6d5a90c958fed to your computer and use it in GitHub Desktop.
Save OnesimusUnbound/b425c0be362473bcdae6d5a90c958fed to your computer and use it in GitHub Desktop.
Script to enable autocompletions of some console apps in powershell
# Ensure lalest versions of these command apps are installed. Some autocompletions have additional requirements as indicated
# requirement: Install-Module DockerCompletion -Scope CurrentUser
Import-Module DockerCompletion
# requirement: Install-Module npm-completion -Scope CurrentUser
Import-Module npm-completion
helm completion powershell | Out-String | Invoke-Expression
kubectl completion powershell | Out-String | Invoke-Expression
minikube completion powershell | Out-String | Invoke-Expression
# dotnet parameter completion
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# winget parameter completion
Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new()
$Local:word = $wordToComplete.Replace('"', '""')
$Local:ast = $commandAst.ToString().Replace('"', '""')
winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# az parameter completion
Register-ArgumentCompleter -Native -CommandName az -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$completion_file = New-TemporaryFile
$env:ARGCOMPLETE_USE_TEMPFILES = 1
$env:_ARGCOMPLETE_STDOUT_FILENAME = $completion_file
$env:COMP_LINE = $wordToComplete
$env:COMP_POINT = $cursorPosition
$env:_ARGCOMPLETE = 1
$env:_ARGCOMPLETE_SUPPRESS_SPACE = 0
$env:_ARGCOMPLETE_IFS = "`n"
$env:_ARGCOMPLETE_SHELL = 'powershell'
az 2>&1 | Out-Null
Get-Content $completion_file | Sort-Object | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, "ParameterValue", $_)
}
Remove-Item $completion_file, Env:\_ARGCOMPLETE_STDOUT_FILENAME, Env:\ARGCOMPLETE_USE_TEMPFILES, Env:\COMP_LINE, Env:\COMP_POINT, Env:\_ARGCOMPLETE, Env:\_ARGCOMPLETE_SUPPRESS_SPACE, Env:\_ARGCOMPLETE_IFS, Env:\_ARGCOMPLETE_SHELL
}
@OnesimusUnbound
Copy link
Author

Calling this function to set up autocomplete will not work as setting up autocomplete will work in powershell profile script or when you copy and paste the contents of this script in your PS session and run (there a link to the issue but I couldn't find it). Another way is to return the contents of this entire script and run invoke-expression.

Get-Content <autocompletion script> | Out-String | Invoke-Expression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment