ChatGPT解决这个技术问题 Extra ChatGPT

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.

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?

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.

M
Mark Meuer

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.


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.
P
Peter Mortensen

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

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


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
G
GorvGoyl

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


This solution works best for me
A
Animesh

Take a look here, not really your desiderata:

PowerTab

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


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!
徐志鹏
# 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

m
mjsr

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


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.
N
Nilesh Gule

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


A
Anubioz

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