Skip to content

Clarify how to use the PowerShell completions #695

Description

@JanPokorny

Describe the feature or problem you’d like to solve

The page https://cli.github.com/manual/gh_completion explains to use the completions by adding eval "$(gh completion)" to a profile file.

For PowerShell, the equivalent command is gh completion -s powershell | iex, which produces the following error:

> gh completion -s powershell | iex
iex : At line:1 char:67
+ Register-ArgumentCompleter -Native -CommandName 'gh' -ScriptBlock {
+                                                                   ~
Missing closing '}' in statement block or type definition.
At line:1 char:31
+ gh completion -s powershell | iex
+                               ~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : MissingEndCurlyBrace,Microsoft.PowerShell.Commands.InvokeExpressionCommand

Proposed solution

Explain how to use the PowerShell completions

Activity

  1. dougbu commented on Apr 4, 2020

    @dougbu

    I got the following to work:

    gh completion --shell powershell |Out-File -FilePath gh.completion.ps1 -Encoding utf8
    ./gh.completion.ps1

    or

    gh completion --shell powershell |Out-String |Invoke-Expression
  2. JanPokorny commented on Apr 17, 2020

    @JanPokorny
    ContributorAuthor

    @dougbu I tried running gh completion --shell powershell |Out-String |Invoke-Expression in my PowerShell, which produced no output. Subsequently typing gh followed by space and pressing TAB triggers file autocompletion, not command autocompletion, sadly. Does this need to be placed in $PROFILE? I was under the impression that this would (temporarily) work in a shell in which the command was ran.

  3. dougbu commented on Apr 17, 2020

    @dougbu

    @JanPokorny the completions themselves are seemingly incomplete. If I type gh pr then hit tab, I get completions for the rest of the command e.g. checkout. But, I don't get completions for anything but files if I just type gh .

    W.r.t. your profile, I found the command didn't work because profiles aren't sourced. Needed the following in my profile:

    New-Item function::global:Complete-gh -value { gh completion --shell powershell | Out-String | Invoke-Expression } >$null

    I then execute Complete-gh at the PowerShell prompt. It's a bit clunky and there's probably a better way to do this.

  4. bc3tech commented on Apr 24, 2020

    @bc3tech

    in my $profile file I added:

    Invoke-Expression -Command $(gh completion -s powershell | Out-String)
    

    and I get the expected result in a new powershell window. Agreed, though, I expected gh <tab> to give me some options and it does not.

  5. dougbu commented on Apr 24, 2020

    @dougbu

    Ah, great. Thanks @bc3tech❕ (The lack of completion after gh likely confused me when I set this up.)

  6. musm commented on Jul 31, 2020

    @musm

    and I get the expected result in a new powershell window. Agreed, though, I expected gh <tab> to give me some options and it does not.

    Gotta agree, found it very odd gh <tab> doesn't have any completions

  7. binyamin commented on Sep 18, 2020

    @binyamin

    I can confirm that in git bash, gh <tab> does complete.

  8. jasonsyoung commented on Oct 7, 2020

    @jasonsyoung

    I can confirm that in git bash, gh <tab> does complete.

    Well that would not be coming from the Powershell completion, which is the topic of this issue, since you are explicitely using BASH.

    I am able to get a second level of tab expansion (as they're called in Powershell, as opposed to tab completion in bash), but not a first level. For example (note: I am not a PS expert), creating the tab completion file:

    > gh completion --shell powershell > C:\Users\<user>\Documents\PowerShell\Github.Completion.ps1
    

    then sourcing it in my $Profile to avoid the performance hit of recreating it on each launch (as others have done above):

    Invoke-Command -ScriptBlock {
        . C:\Users\<user>\Documents\PowerShell\Github.Completion.ps1
    }
    

    leads me to have this work: > gh repo <tab> -- resulting in cycling through clone, create, credits, etc. But just > gh <tab> results in the shell cycling through my directories instead of the top level items.

    Any suggestions?

  9. peschkaj commented on Oct 7, 2020

    @peschkaj

    @jasonsyoung Take a look in #1775 for a current workaround

  10. thomaslevesque commented on Oct 8, 2020

    @thomaslevesque

    I can't get any of the suggested solutions to work. I tried in PowerShell Core and Windows Powershell, in all cases I only have the usual file completion, no command completion. It would be nice to have an answer from the team...

  11. LRitzdorf commented on Jun 12, 2021

    @LRitzdorf

    As of 6/12/2021, with version 1.11.0, command and subcommand autocompletion are present.

    I used @bc3tech 's command in my PowerShell $PROFILE, and everything seems to be working as expected. I get command autocompletion, then file autocompletion when typing after a subcommand.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsenhancementa request to improve CLIhelp wantedContributions welcomewindowsRelated to Windows hosts or runners

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions