iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🪆

Windows: Enabling Tab Completion for winget

に公開

tl;dr

More Details

The winget subcommand "complete" provides tab completion functionality based on your input. To use it in practice, you need to register a tab completion script using the Register-ArgumentCompleter command.

Tab Completion Script

Microsoft's website provides a PowerShell script to enable tab completion.
I'll reprint it here for easy access.

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', $_)
  }
}

Copy the script above into your $profile (PowerShell's startup script) and save it. After restarting PowerShell, tab completion will be enabled for winget.

About winget complete

winget complete outputs a list of completion candidates based on the command line and string being entered. It takes three parameters: commandline, word, and position. commandline is the current command line itself, word is the word to be tab-completed, and position indicates the cursor position within the command line.

For example,

>  winget complete --commandline "winget u" --word="u" --position 1
upgrade
update
uninstall

>

When you enter a command like this, it outputs a list of subcommands starting with "u". The script provides the next candidate each time you press the Tab key.

Summary

Tab completion can be used not only for winget subcommands and options but also for specifying packages.
Use this feature to make your package management even more efficient.

Happy Hacking.

References

GitHubで編集を提案

Discussion