Skip to content

Clarify how to use the PowerShell completions #695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JanPokorny opened this issue Mar 24, 2020 · 11 comments · Fixed by #4114
Closed

Clarify how to use the PowerShell completions #695

JanPokorny opened this issue Mar 24, 2020 · 11 comments · Fixed by #4114
Labels
docs enhancement a request to improve CLI help wanted Contributions welcome windows

Comments

@JanPokorny
Copy link
Contributor

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

@JanPokorny JanPokorny added the enhancement a request to improve CLI label Mar 24, 2020
@mislav mislav added the docs label Mar 26, 2020
@dougbu
Copy link

dougbu commented Apr 4, 2020

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

@JanPokorny
Copy link
Contributor Author

@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.

@dougbu
Copy link

dougbu commented Apr 17, 2020

@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.

@bc3tech
Copy link

bc3tech commented Apr 24, 2020

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.

@dougbu
Copy link

dougbu commented Apr 24, 2020

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

@musm
Copy link

musm commented Jul 31, 2020

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

@binyamin
Copy link

binyamin commented Sep 18, 2020

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

@jasonsyoung
Copy link

jasonsyoung commented Oct 7, 2020

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?

@peschkaj
Copy link

peschkaj commented Oct 7, 2020

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

@vilmibm vilmibm added the help wanted Contributions welcome label Oct 7, 2020
@thomaslevesque
Copy link

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...

@LRitzdorf
Copy link

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
Labels
docs enhancement a request to improve CLI help wanted Contributions welcome windows
Projects
None yet
Development

Successfully merging a pull request may close this issue.