Skip to content

Fish-like autocompletion #687

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
felixfbecker opened this issue May 17, 2018 · 79 comments
Closed

Fish-like autocompletion #687

felixfbecker opened this issue May 17, 2018 · 79 comments
Assignees
Labels
Issue-Enhancement It's a feature request.

Comments

@felixfbecker
Copy link

felixfbecker commented May 17, 2018

I tried to figure this out but couldn't find out. Is there a way to get autocompletion like in the fish shell?

fish-shell-demo

Basically what I am looking for is automatically searching the history on every keystroke, then showing a grey suggestion behind the cursor that can be accepted with (if the cursor is at the end of the line).

We can get close with Set-PSReadLineKeyHandler -Key ArrowRight -Function HistorySearchBackward but it doesn't show the suggestion and it prevents moving the cursor to the right.

@lzybkr lzybkr added the Issue-Enhancement It's a feature request. label Jun 4, 2018
@lzybkr
Copy link
Contributor

lzybkr commented Jun 4, 2018

This feature wasn't much of a priority with limited console support for VT sequences, but now Windows has real support for VT sequences, it makes sense to add this capability.

@SteveL-MSFT
Copy link
Member

This type of experience is what we envisioned when we originally thought about "Machine Learning autocompletion". Since then, we dropped the ML part and thought about simply exposing an interface to enable this (https://github.com/PowerShell/PowerShell-RFC/pull/123/files) and just use local history.

@felixfbecker
Copy link
Author

@lzybkr is there any way this is currently already possible with PSReadLine customization? If not, what would be needed to make it possible? It would really improve my workflow so I would be willing to put some work in

@SteveL-MSFT So the plan would be to ship a function that handles the searching logic with Get-History so it is easy to opt-in to using that with PSReadLine?

@lzybkr
Copy link
Contributor

lzybkr commented Jun 10, 2018

PSReadLine doesn't use Get-History - it keeps it's own history.

Here are my thoughts on implementing this feature:

  • Add an option to Set-PSReadLineOption and document it
  • Add a configuration color for the autosuggestion text with a suitable default.
  • In SelfInsert, you will add smarts to find the autosuggestion. If you want command history, HistorySearchBackward has much of what you want, for other completions, GetCompletions is probably what you should use.
  • The autosuggestion text which is not accepted could be stored in a different StringBuilder. In ReallyRender, you would write this text out with the autosuggestion color.
  • In ForwardChar, if you are at the end of the buffer and there is autosuggestion text, add it to the buffer and clear the autosuggestion.
  • Probably add a binding to Ctrl+f in Windows mode to ForwardChar.
  • Add a new function AcceptFirstSuggestedWord and maybe AcceptFirstSuggestedShellWord, binding one or the other to Alt+RightArrow and Alt+f if the keys are not already bound (Emacs binds Alt+f by default, and hmm, maybe Alt+RightArrow won't work at all.) At any rate, the functions should be available so folks can get this functionality if they want as well.

As for relying on PowerShell for any new fancy suggestions - I'm not excited about that unless it means no api changes. Today a single build of PSReadLine works with any version of PowerShell > 5.0 and I'd like to keep it that way.

@phiter
Copy link

phiter commented Jan 14, 2020

So, is it coming? :D

@potatoqualitee
Copy link

Yessss! Please. Looks like there's a whole lot of interest in this. I just saw similar functionality on a livestream and it was awesome!

https://i.vgy.me/PAn45j.mp4

@daxian-dbw
Copy link
Member

daxian-dbw commented Feb 15, 2020

I'm working on a prototype of this, and here is the zip package if any of you want to try it (any feedback would be very helpful!).

PSReadLine.zip

Currently:

  • The only suggestion source is the PSReadLine history. The ultimate goal is to call into a prediction API in S.M.A that is powered by a prediction plugin.
  • Currently there is no way to configure the style that the suggestion text is rendered with, the default style (color only) works OK with a black background. (of course, more work is needed here)
  • VI mode - now the suggestion is disabled when switching to the command mode, and enabled when switching to insert mode.
  • So far, I haven't really focused on the VI/Emacs experience, so it could be pretty raw, and feedback is very welcome.

When using this PSReadLine, please add the following key binding to your profile:

Set-PSReadLineKeyHandler -Key "Ctrl+f" -Function ForwardWord
  • Pressing RightArrow at the end of current typing will accept the suggestion text
  • Pressing Ctrl+f at the end of current typing will accept a word from the suggestion text
  • Pressing Ctrl+z will get you back to where you were before accepting all or part of the suggestion

Important note: don't forget to unblock the zip file after you download it, otherwise you will face weird exceptions thrown from PowerShell.

predict

@mpearon
Copy link

mpearon commented Feb 20, 2020

This is great to hear! I would love to see the previous pipeline command considered in the prediction.
For instance, Get-ADUser shouldn't suggest Get-ACL when used in the pipeline.

@daxian-dbw
Copy link
Member

@mpearon Can you please elaborate on it a bit more? A screenshot would be useful :)

@daxian-dbw
Copy link
Member

Issues found

  1. prediction shouldn't kick in during history operations like Ctrl+r, but it's shown today:
    image

@mpearon
Copy link

mpearon commented Feb 25, 2020

@daxian-dbw This isn't the way it is currently behaving (just a suggestion), so there isn't a screenshot to share.

Maybe I'm missing the point, but my thought was that when ML is leveraged, the suggestions that it makes (not necessarily from history), should take the previous pipeline item into consideration.
So, if I use Get-ADUser, the next pipeline commands to be suggested should be those included in that module (or related to the previous command).

@vexx32
Copy link

vexx32 commented Feb 25, 2020

Yeah, that probably makes sense. I'd generally assume that you could do something like this, suggested in order of importance or probability to be accurate.

  1. Suggest commands that accept the previous command's declared [OutputType()] as pipeline input by value.
  2. Suggest more general utility commands like Where-Object and Select-Object (and so forth)
  3. Suggest commands that accept the previous command's OutputType as pipeline input by property name.
  4. Suggest other commands from the module that the previous command is in.

I'd also recommend that commands that match either 1 or 3 and are in the same module as the previous command should be suggested first. Any sorting beyond that should probably be related to frequency of usage or just alphabetical perhaps.

@Southclaws
Copy link

Great to see this being worked on, it's the one feature I miss from Fish!

Question: Have you considered the up/down prefixed history search? So if you type the start of a command then hit the Up Arrow, history searches only through commands with that prefix. Example:

fish

@lzybkr
Copy link
Contributor

lzybkr commented Feb 26, 2020

@Southclaws - do you already set these bindings (which I think should be changed to be the default)?

Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

@Southclaws
Copy link

Oh, amazing, thanks!

Looking forward to the above update too, I'd be interested in testing it out but not sure how to install a module directly from a .zip

@mpearon
Copy link

mpearon commented Mar 2, 2020

@Southclaws - I wasn't entirely sure how to force PS to default to a different PSReadline module either (short of removing/reimporting the module in a profile, or something).
Maybe a bad practice, but I just replaced the contents of the original PSReadline directory. (You can get that by issuing (Get-Module PSReadline).path)

@mpearon
Copy link

mpearon commented Mar 2, 2020

@daxian-dbw Are we creating new issues for the preview here?
After registering the Set-PSReadlineKeyHandler options mentioned by @lzybkr, my cursor moves to character 0 of the line instead of the end of the command.
I understand that in the case that I've type a bit of a command, this would be desired - but if my cursor is already at character 0, I would expect it to move to the end of the line.
PSReadLineIssue

@lzybkr
Copy link
Contributor

lzybkr commented Mar 2, 2020

@mpearon - try:

Set-PSReadLineOption -HistorySearchCursorMovesToEnd 

@daxian-dbw
Copy link
Member

@mpearon Replacing the original PSReadline directory is the way to use the private zip package. It should be stable enough to not crash. If you decide to get the official PSReadLine module, just use the same "upgrading" instructions from the README.md.

Also, if not yet, take a look at https://github.com/PowerShell/PSReadLine/blob/master/PSReadLine/SamplePSReadLineProfile.ps1. It contains many useful configurations to PSReadLine from @lzybkr.

@mpearon
Copy link

mpearon commented Mar 3, 2020

@lzybkr and @daxian-dbw - Those configuration items have corrected the behavior for me! Thank you.

@kort3x
Copy link

kort3x commented Mar 19, 2020

just wanted to say: Awesome work - Thank you!

@SeeminglyScience
Copy link
Contributor

  • VI mode - now the suggestion is disabled when switching to the command mode, and enabled when switching to insert mode.
  • So far, I haven't really focused on the VI/Emacs experience, so it could be pretty raw, and feedback is very welcome.

Suggestion seems to stay when switching modes, until an edit is made (then it's removed). I'd like to see it always update even in VI mode personally. Also ViForwardChar doesn't seem to trigger accepting the suggestion.

Very cool though.

@JustinGrote
Copy link

JustinGrote commented Mar 27, 2020

@daxian-dbw FYI I may be messing something up but I think something changed in the latest 2020.3.1 preview and it stopped working in the integrated console.

EDIT: And yes I made sure to replace it in the extension modules folder

@chinmayanagpal
Copy link

I just installed the latest release of Powershell Core 7, is there any way to turn this off/replace the suggested text with the tab-complete text instead of history suggestions?

@JustinGrote
Copy link

@chinmaya1011 that part has not been completed yet, please follow: #1468

And contribute your input for what you would like it to look like if it doesn't meet your needs.

@mnooseman
Copy link

Hi All - Just found an issue with the autocompletion through an RDP session. Issue created here: #1558

@Fooughhy
Copy link

Maybe it's just me but I have to specify -NonInteractive when creating powershell in windows terminal for it to do the autosuggest (I'm doing -Command Import-Module from the command line).

* it's _so_ worth it though. It's fantastic.

I am having an issue with this aswell... I didn't have the "NonInteractive" parameter for my pwsh session before, but I still had this functionality working. Now, all of a sudden, it doesn't work anymore. I have tried to set the "NonInterative" parameter for my pwsh now, but it still doesn't work. I am importing PSReadLine in my profile file (along with oh-my-posh and other things) and it is picking it up when using powershell. But this fish-autocomplete isn't working.

WindowsTerminal_d1fPclJ1r1

@JustinGrote
Copy link

@Fooughhy can you verify that you are using the new setting? This recently changed to not be on by default, you want to add set-psreadlineoption -predictionsource history

get-psreadlineoption | % predictionsource

@Fooughhy
Copy link

Fooughhy commented Jul 13, 2020

There we go! I was trying to find a setting for this, but couldn't... Thank you! It works now.

One more question, does it require "NonInteractive" as my quoted comment suggested? I guess I could try it out.

EDIT: It doesn't seem to be required for me, at least.

@qubeio
Copy link

qubeio commented Jul 18, 2020

This is so awesome - Have all the awards

@tgeng
Copy link

tgeng commented Oct 11, 2020

Thank you for the awesome update! BTW, can function EndOfLine accept the suggestion similar to ForwardChar as well?

@lwndwsk1
Copy link

lwndwsk1 commented Dec 6, 2020

This is epic! Thanks!

@Praful
Copy link

Praful commented Jan 6, 2021

This is a great new feature. I was looking for it after seeing it on Fish. Thank you for providing it.

I've installed 2.2.0 beta1 and have two questions:

  1. What is the equivalent of right-arrow (to accept the prediction) when using Vi mode? Comments above refer to ViForwardChar but that doesn't appear to be an option for me. Please can you give an example of setting this to CTRL-O.

  2. How do I change the prediction colour? Again, an example would be useful.
    Edit: answered in Predictive IntelliSense #1468
    Edit 2: I get this error when using Prediction as a colour:

Set-PSReadLineOption: D:\data\PowerShell\profile.ps1:224
Line |
 224 |  Set-PSReadLineOption -Colors @{ Prediction = [ConsoleColor]::DarkGray …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | 'Prediction' is not a valid color property

Thanks

@daxian-dbw
Copy link
Member

@Praful For VI mode, RigthArrow at the end of the editing line will also accept the prediction:

demo1

You can certainly bind a different key to the AcceptSuggestion function as needed.

As for the color, the example in #1468 was out-dated, see the "Change the Color for Predictions" section in https://devblogs.microsoft.com/powershell/announcing-psreadline-2-1-with-predictive-intellisense/

@Praful
Copy link

Praful commented Jan 9, 2021

@daxian-dbw thank you for answering my question. For those who want to see the answer in one place, I put the following in my PowerShell profile:

Set-PSReadLineOption -Colors @{ InlinePrediction = '#000055'}
Set-PSReadLineKeyHandler -chord Ctrl+o -function {ViForwardChar}

For those who use Vi mode, they may, like me, not want to take their hands off the home keys to reach for the arrow key, the default key to accept the prediction. I have bound that above to CTRL+o for no other reason than it's easy to press from the home row. I used lowercase O, simply to avoid having to press SHIFT also.

Thanks again for making an essential module even better!

@yihuajack
Copy link

yihuajack commented Feb 14, 2021

Thanks for your awesome work. BTW, do you plan to support coloring existing cmdlets, functions, script files, or executable programs with green and non-existing ones with red and underlining paths or filenames users input if they are existing just as ZSH's plugin zsh-autosuggestions or fish does? I'm really looking forward to this feature.

@LuanVSO
Copy link

LuanVSO commented Feb 14, 2021

Thanks for your awesome work. BTW, do you plan to support coloring existing cmdlets, functions, script files, or executable programs with green and non-existing ones with red and underlining paths or filenames users input if they are existing just as ZSH's plugin zsh-autosuggestions or fish does? I'm really looking forward to this feature.

i think those are from fast-syntax-highlighting

@SteveL-MSFT
Copy link
Member

@yihuajack the coloring for existing and non-existing commands seems like a good idea, please open a new issue for this

@daxian-dbw
Copy link
Member

Given that the original feature request was finished, I will close this issue. Thank you all for all the feedback and comments!
For any new bug reports and enhancement requests, please open new issues for them. Thanks again!

@potatoqualitee
Copy link

Thanks again for this, it's awesome 🤩

@JustinGrote
Copy link

JustinGrote commented Jul 26, 2021

Thanks again for this, it's awesome 🤩

Agreed, use it constantly, and looks like Github Copilot aped the same style so it's really consistent looking in my vscode experience these days :)

@panekj
Copy link

panekj commented Jul 26, 2021

Now we just need it to be actually powered by GitHub Copilot! ❤️

@JustinGrote
Copy link

@panekj you can see the API in use in the console logs in vscode, I'm sure it'd be a flagrant license violation but it would be probably to hijack this session and use the API to fetch Powershell completions :)
{5F49CE90-A073-47F9-8EED-A74B6D2167C5}

@hongwen000
Copy link

Thank you for the awesome update! BTW, can function EndOfLine accept the suggestion similar to ForwardChar as well?

Found one way to make this possible:

Set-PSReadLineKeyHandler -Chord "Ctrl+e" -ScriptBlock {
    [Microsoft.PowerShell.PSConsoleReadLine]::AcceptSuggestion()
    [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}

@ToxicSmurf
Copy link

Sorry but how do I turn this off?

@rkeithhill
Copy link
Contributor

@ToxicSmurf Add this to your profile script to disable predictions:

Set-PSReadLineOption -PredictionSource None

@zloyle
Copy link

zloyle commented Oct 20, 2023

UpArrow HistorySearchBackward Search for the previous item in the history that starts with the current input -
like PreviousHistory if the input is empty
DownArrow HistorySearchForward Search for the next item in the history that starts with the current input - like
NextHistory if the input is empty

This is only working for history matches that begin with the string not containing the string the way fish shell supports. Reading the chord binding description in PSReadline, this appears to be intentional.

example:

$Version = (Get-Module -Name PSReadLine).Version.ToString()

The only way to find this via HistorySearch is by first typing $ and clicking the UpArrow.

Typing Version or PSReadLine or Module etc. does not work, please look into adding this functionality if time permits.

Regards,
Thank you.

@owenstake
Copy link

Thank you for the awesome update! BTW, can function EndOfLine accept the suggestion similar to ForwardChar as well?

Found one way to make this possible:

Set-PSReadLineKeyHandler -Chord "Ctrl+e" -ScriptBlock {
    [Microsoft.PowerShell.PSConsoleReadLine]::AcceptSuggestion()
    [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}

You method do not work perfect. When cursor is at the begin, "Ctrl+e" leads to complete the prediction, not to the end of line.

I fix it by this sciprt

    Set-PSReadLineKeyHandler -Key 'Ctrl+e' `
            -BriefDescription "Go to EndOfLine or AcceptSuggestion" `
            -LongDescription `
                "If cursor is not at the end of line, then go to end of line, `
                otherwise try to AcceptSuggestion" `
            -ScriptBlock {
        $line = $null
        $cursor = $null
        [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
        if ($cursor -ne $line.Length) {
            [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
        } else {
            [Microsoft.PowerShell.PSConsoleReadLine]::AcceptSuggestion()
        }
    }

@yyancy
Copy link

yyancy commented Dec 11, 2023

@owenstake Thank you!! It save me a lot of time !!😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement It's a feature request.
Projects
None yet
Development

No branches or pull requests