228

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?

2
  • 14
    Yes - that's what I've been doing for the last decade or so, but I'm trying to transition to PowerShell, because I want to be able to fly on the command line on systems other than my own, where Cygwin isn't installed. Commented Nov 28, 2011 at 13:41
  • Does this answer your question? powershell.exe tab completion - list alternatives? Commented Sep 6, 2023 at 14:56

9 Answers 9

351

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 your powershell profile, defined by $PROFILE (usually %UserProfile%\Documents\WindowsPowerShell\profile.ps1 for Windows PowerShell 5.x and %UserProfile%\Documents\PowerShell\profile.ps1 for PowerShell 6+).

Sign up to request clarification or add additional context in comments.

17 Comments

MenuComplete instead of Complete is more like bash, it lets you use the arrow keys to choose from the available options
BTW, if the profile.ps1 file does not exist on your machine, you can generate one with a command new-item $profile -itemtype file -force
It is very likely that your machine will not have C:\Users\[User]\Documents\WindowsPowerShell\profile.ps1. You actually need to run new-item $profile -itemtype file -force
That's a really inconvenient spelling for touch :-(
Instead of needing to generate a new profile, can't you just do notepad $profile and save it? It'll complain about it not existing but you'll be good once you save the file.
|
40

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

1 Comment

This solution works best for me
30

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

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

2 Comments

The link in the blogpost is broken, however at the end of the day it brings you to PsReadline (last commit in 2012) which has been forked (last commit in 2013)
Nowadays you can do this with a one-liner, as described here: stackoverflow.com/a/37715242/24874
15

Take a look here, not really your desiderata:

PowerTab

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

2 Comments

Interesting. If there's a way to do something like that, then it seems quite possible to make the expansion work like in bash. I'm far from being an expert in PowerShell, though, so may be missing something.
Sure! Start studying the PowerTab Module's code to try doing your expansion needs. But PowerTab offers expansions for almost any command, wmi, comobject, assembly with a easy selection way!
13
# 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

Comments

7

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

Comments

6

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

1 Comment

Modifying the TabExpansion function is probably the way to go, still a lot more complicated than what I was looking for though. I think I'll need to get way more fluent in powershell before I'm able to mess around with that.
5

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

Comments

3

inspired and provoked by Anubioz's answer, here's the difference in the available completion presets:

option suggestions cycling var expansion unique completion akin to
PossibleCompletions static print no no no ~bash~
**msys2 default bash static print no no yes
Complete static print no yes yes ~bash~
MenuComplete interactive visual yes yes nushell default
TabComplete* no blind inline yes yes cmd default
ViTabComplete* no blind inline yes yes
  • lack of completion means that it ain't completed even if there exists single unambiguous match
  • expand the variable (like ~ or $env:USERPROFILE to its value i.e. 'C:\Users\<name>') under favourable conditions like say with invoking command lsd ~/.conf
  • i think vitabcomplete* is to enable tabcomplete* in vi mode. maybe someone can correct me.
  • i wish there were an option with "interactive suggestions, visual cycling, yes unique completion, no expansion" i.e. say smth with name like MenuPossibleCompletions or MenuCompletions; but it seems the oldest answer's suggestion to define it perself using the tabexpansion function will be only way to achieve that lol

to check if that's all, i did the foll in msys2 bash. and it indeed seems that these are all the available presets for completions.

command (pretty printed per line)

powershell -c Set-PSReadlineKeyHandler -Key Tab -Function MenuCompletions 2>&1 | 
tr -d '\r\n' | 
tr ',' '\n' | 
sed 's/\. /.\n/g' |
grep -i complet
The argument "MenuCompletions" does not belong to the set "Abort
Complete
MenuComplete
PossibleCompletions
TabCompleteNext
TabCompletePrevious
ViTabCompleteNext
ViTabCompletePrevious
Supply an argument that is in the set and then try the command again.At line:1 char:45+ Set-PSReadlineKeyHandler -Key Tab -Function MenuCompletions+                                             ~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidData: (:) [Set-PSReadLineKeyHandler    ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.