RobSiklos
RobSiklos

Reputation: 8849

How to make PowerShell tab completion work like Bash

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.

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

Upvotes: 225

Views: 90390

Answers (9)

CB.
CB.

Reputation: 60958

Take a look here, not really your desiderata:

PowerTab

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

Upvotes: 15

user8395964
user8395964

Reputation: 144

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    ]

Upvotes: 3

svick
svick

Reputation: 244978

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+).

Upvotes: 348

GorvGoyl
GorvGoyl

Reputation: 49510

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

Upvotes: 39

Anubioz
Anubioz

Reputation: 495

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

Upvotes: 6

徐志鹏
徐志鹏

Reputation: 141

# 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

Upvotes: 13

Nilesh Gule
Nilesh Gule

Reputation: 1621

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

Upvotes: 5

Jay Bazuzi
Jay Bazuzi

Reputation: 46536

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

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

Upvotes: 30

mjsr
mjsr

Reputation: 7590

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

Upvotes: 6

Related Questions