3

I have recently got myself a new Windows 11 laptop, and started exploring powershell. Never used it before on Windows, but I'm a frequest user of zsh in Mac so not completely new.

I upgraded Powershell to 7.2.1, but noticed that the auto-suggestion type feature that I'm used to with Oh My Zsh is missing. After searching a bit, I installed PSReadLine, and setup my profile using the following:

Import-Module PSReadLine

Set-PSReadLineOption -PredictionSource History

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

Set-PSReadLineKeyHandler -Key Tab -Function Complete

This is working a lot better than earlier, but I'm noticing that I cannot accept autocompletions partially.

For example, this is part of my history:

New-item –type file –force $profile

If I now try to create a file say test-file, and start typing New-Item, I do get the suggestion. I want to accept till the end of -force, or possibly till file. That's not happening, whatever be the key combination I try (tried Ctrl+, Shift+, Alt+, Fn+ in vain).

Of course, I can accept full suggestion and delete what I do not need. While for this example it's reall doesn't matter, but it'll be annoying for long commands with matches present only in initial parts. So, my question is what is the combo to accept suggestions in part, or if that's need to be enabled separately, how do I do it?

yarnabrina
  • 1,561
  • 1
  • 10
  • 30
  • Related search hit https://dev.to/ofhouse/add-a-bash-like-autocomplete-to-your-powershell-4257 – Tomalak Feb 27 '22 at 08:08
  • 2
    Have a look at this `Set-PSReadLineKeyHandler -Chord "Ctrl+f" -Function ForwardWord` https://learn.microsoft.com/en-us/powershell/module/psreadline/about/about_psreadline?view=powershell-7.2#acceptnextwordsuggestion – Daniel Feb 27 '22 at 08:10
  • @Daniel Thanks. I changed it to `Ctrl+RightArrow`, and it seems to be the same behaviour. Do you want to add an answer? I'll accept and close. – yarnabrina Feb 27 '22 at 08:26

1 Answers1

6

From the about_PSReadLine docs

AcceptNextWordSuggestion
Accepts the next word of the inline suggestion from Predictive IntelliSense. This function can be bound with Ctrl+F by running the following command.

Set-PSReadLineKeyHandler -Chord "Ctrl+f" -Function ForwardWord
Daniel
  • 4,792
  • 2
  • 7
  • 20
  • 1
    Nicely done. Note that `ForwardWord` does double duty: it navigates to the next word in the part of the command line that has been _typed explicitly_ and also accepts the next _suggested_ word. `AcceptNextWordSuggestion` does exist as a separate function and can be bound separately, if desired (probably not worth it). On Windows, where Ctrl+RightArrow is by default bound to `NextWord`, which only navigates in _what has been typed_, the following can therefore be used to make it accept the next suggested word too: `Set-PSReadLineKeyHandler -Chord 'Ctrl+RightArrow' -Function ForwardWord` – mklement0 Feb 28 '22 at 21:16