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?
New versions of PowerShell include PSReadline, which can be used to do this:
Set-PSReadlineKeyHandler -Key Tab -Function Complete
or, to make it even more like bash where you can use arrow-keys to navigate available options:
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
To make it permanent, put this command into C:\Users\[User]\Documents\WindowsPowerShell\profile.ps1.
It is now possible to get PowerShell to do Bash-style completion, using PSReadline.
Check out blog post Bash-like tab completion in 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
Take a look here, not really your desiderata:
but I think is the best tab expansion feature for PowerShell console!!!
# 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
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
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
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
Success story sharing
MenuComplete
instead ofComplete
is more like bash, it lets you use the arrow keys to choose from the available optionsnew-item $profile -itemtype file -force
C:\Users\[User]\Documents\WindowsPowerShell\profile.ps1.
You actually need to runnew-item $profile -itemtype file -force
touch
:-(notepad $profile
and save it? It'll complain about it not existing but you'll be good once you save the file.