8

Recently I updated my PowerShell to version 7.3.0 and when typing it shows suggestions. But when I press the Tab key it doesn't autocomplete the suggestion. How to set the Tab as the autocompletion key?

NirmalCode
  • 2,140
  • 1
  • 14
  • 19

1 Answers1

11

So after doing some research I found out,

  • The default autocompletion key is the RightArrow key.
  • You can accept the suggestions word by word
  • You can change the suggestion view type between InlineView and ListView by pressing F2

Source: Using predictors in PSReadLine

So here's how to change key bindings

Set Tab key as the keybinding for auto complete (AcceptSuggestion)

Set-PSReadLineKeyHandler -Chord "Tab" -Function AcceptSuggestion

Set RightArrow key as the keybinding for accepting the next word in the suggestion (ForwardWord)

Set-PSReadLineKeyHandler -Chord "RightArrow" -Function ForwardWord

Note :

You need to run these each time you open a new session. To avoid that add these to profile.ps1 file. More on Profiles

  • To change these just for the current user,

    Open a PowerShell window and run,

    notepad $profile.CurrentUserAllHosts
    

OR

  • To change these for all users,

    Open a PowerShell window with Run as Administrator and run,

    Notepad $profile.AllUsersAllHosts
    

If the file doesn't exist, create a new one.

Add these lines and save.

Set-PSReadLineKeyHandler -Chord "Tab" -Function AcceptSuggestion
Set-PSReadLineKeyHandler -Chord "RightArrow" -Function ForwardWord
NirmalCode
  • 2,140
  • 1
  • 14
  • 19
  • My F2 has no effect, how to fix it? I don't see F2 bound. – Konstantin Glukhov Feb 10 '23 at 06:26
  • @KonstantinGlukhov did you fix it? I tried it on Jetbrains Rider's terminal and F2 did nothing, but I ran ```Get-PSReadLineOption | Select-Object -Property PredictionSource``` and it says ```WARNING: The prediction 'ListView' is temporarily disabled because the current window size of the console is too small. To use the 'ListView', please make sure the 'WindowWidth' is not less than '54' and the 'WindowHeight' is not less than '15'. PredictionSource ---------------- HistoryAndPlugin``` After resizing the terminal's height, it worked. – Seiko Santana May 03 '23 at 14:30
  • Yes, I got it working. Just had to RFM. My problem was that I was using Vi mode and all the help pages are about emacs by default. To make it working in Vi mode you need to use the following "Set-PSReadLineKeyHandler F2 -Function SwitchPredictionView" – Konstantin Glukhov May 04 '23 at 16:21
  • Want to throw out there for anyone reading that MS PowerToys has a keyboard manager, which allows you to rebind keys. For use in both Visual Studio (where right-arrow also does intellisense autocomplete) and Powershell 7, I mapped CapsLock to right-arrow to make life easier. Added side bonus of never having to worry about caps lock being on. – mlibby May 28 '23 at 23:38
  • FWIW I get an error with the first command: "Set-PSReadLineKeyHandler : Cannot validate argument on parameter 'Function'. The argument "AcceptSuggestion" does not belong to the set [...]" where "[...]" is a list of actions "specified by the ValidateSet attribute". – bhalperin Jul 18 '23 at 15:15