How to make PowerShell tab completion work like Bash

PowershellCmdTab Completion

Powershell Problem Overview


Let's say I have the following files in my current directory:

buildBar.bat
buildFoo.bat
buildHouse.bat

And I type the following at my command prompt, ./bu and then TAB.

  • In Bash, it gets expanded to ./build

  • In PowerShell, it gets expanded to ./buildBar.bat -- the first item in the list.

  • In Cmd, the behavior is the same as PowerShell.

I prefer the Bash behaviour - is there a way to make PowerShell behave like Bash?

Powershell Solutions


Solution 1 - Powershell

New versions of PowerShell include PSReadline, which can be used to do this:

Set-PSReadlineKeyHandler -Key Tab -Function Complete

To make it permanent, put this command into C:\Users\[User]\Documents\WindowsPowerShell\profile.ps1.

Solution 2 - Powershell

It is now possible to get PowerShell to do Bash-style completion, using PSReadline.

Check out blog post Bash-like tab completion in PowerShell.

Solution 3 - Powershell

tab only completes the command name not its previous arguments/parameters.

to also autocomplete the complete command with arguments from history set the below keybinding.

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

Now, type few characters of command name and use up/down arrow to autocomplete this command (with arguments) from history.

real time saver.


See more: Power up your PowerShell

Solution 4 - Powershell

Take a look here, not really your desiderata:

PowerTab

but I think is the best tab expansion feature for PowerShell console!!!

Solution 5 - Powershell

# keep or reset to powershell default
Set-PSReadlineKeyHandler -Key Shift+Tab -Function TabCompletePrevious

# define Ctrl+Tab like default Tab behavior
Set-PSReadlineKeyHandler -Key Ctrl+Tab -Function TabCompleteNext

# define Tab like bash
Set-PSReadlineKeyHandler -Key Tab -Function Complete

Solution 6 - Powershell

Modify the TabExpansion function to achieve what you want. Remember that perhaps it completes till the end if you press tab again the new suggestion modify from where you originally press the key. I strongly prefer the actual behaviour, I want the line writted as fast as possible. Finally don't forget the wildcard expansion, for example: bu*h[Tab] automatically completes to buildHouse.bat

Solution 7 - Powershell

With Powershell Core we can set the PredictionSource property for PSReadLine as History to get auto suggestion. Refer to the YouTube video for more details https://youtu.be/I0iIZe0dUNw

Solution 8 - Powershell

Actually, bash behavior is governed by /etc/inputrc, which varies heavily from distro to distro.

So here's how to make PowerShell behave more like a bash with sane defaults (Gentoo, CentOS)

# Press tab key to get a list of possible completions (also on Ctrl+Space)

Set-PSReadlineKeyHandler -Chord Tab -Function PossibleCompletions


# Search history based on input on PageUp/PageDown

Set-PSReadlineKeyHandler -Key PageUp -Function  HistorySearchBackward
Set-PSReadlineKeyHandler -Key PageDown -Function HistorySearchForward


# If you feel cursor should be at the end of the line after pressing PageUp/PageDown (saving you an End press), you may add:

Set-PSReadLineOption -HistorySearchCursorMovesToEnd

# Set-PSReadLineOption -HistorySearchCursorMovesToEnd:$False to remove

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobSiklosView Question on Stackoverflow
Solution 1 - PowershellsvickView Answer on Stackoverflow
Solution 2 - PowershellJay BazuziView Answer on Stackoverflow
Solution 3 - PowershellGorvGoylView Answer on Stackoverflow
Solution 4 - PowershellCB.View Answer on Stackoverflow
Solution 5 - Powershell徐志鹏View Answer on Stackoverflow
Solution 6 - PowershellmjsrView Answer on Stackoverflow
Solution 7 - PowershellNilesh GuleView Answer on Stackoverflow
Solution 8 - PowershellAnubiozView Answer on Stackoverflow