ChatGPT解决这个技术问题 Extra ChatGPT

如何使 PowerShell 选项卡完成像 Bash 一样工作

假设我的当前目录中有以下文件:

buildBar.bat
buildFoo.bat
buildHouse.bat

我在命令提示符处输入以下内容,./bu,然后是 TAB

在 Bash 中,它被扩展为 ./build

在 PowerShell 中,它被扩展为 ./buildBar.bat - 列表中的第一项。

在 Cmd 中,行为与 PowerShell 相同。

我更喜欢 Bash 行为 - 有没有办法让 PowerShell 表现得像 Bash?

是的——这就是我过去十年左右一直在做的事情,但我正在尝试过渡到 PowerShell,因为我希望能够在我自己的系统以外的系统上运行命令行,而 Cygwin 不是安装。

M
Mark Meuer

新版本的 PowerShell 包括 PSReadline,可用于执行此操作:

Set-PSReadlineKeyHandler -Key Tab -Function Complete

或者,让它更像 bash,您可以使用箭头键来导航可用选项:

Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete

要使其永久化,请将此命令放入 C:\Users\[User]\Documents\WindowsPowerShell\profile.ps1。


MenuComplete 而不是 Complete 更像 bash,它允许您使用箭头键从可用选项中进行选择
顺便说一句,如果您的机器上不存在 profile.ps1 文件,您可以使用命令 new-item $profile -itemtype file -force 生成一个
您的机器很可能没有 C:\Users\[User]\Documents\WindowsPowerShell\profile.ps1. 您实际上需要运行 new-item $profile -itemtype file -force
touch 的拼写真的很不方便 :-(
不需要生成新的配置文件,您不能只执行 notepad $profile 并保存它吗?它会抱怨它不存在,但保存文件后你会很好。
P
Peter Mortensen

现在可以使用 PSReadline 让 PowerShell 执行 Bash 样式的补全。

查看博文Bash-like tab completion in PowerShell


博文中的链接已损坏,但最终它会将您带到 PsReadline (last commit in 2012),即 forked (last commit in 2013)
如今,您可以使用单线做到这一点,如下所述:stackoverflow.com/a/37715242/24874
G
GorvGoyl

tab 仅完成命令名称而不是其先前的参数/参数。

要使用历史记录中的参数自动完成完整的命令,请设置以下键绑定。

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

现在,键入命令名称的几个字符并使用向上/向下箭头从历史记录中自动完成此命令(带参数)。

实时节省。

查看更多:Power up your PowerShell


这个解决方案最适合我
A
Animesh

看看这里,不是你真正需要的:

PowerTab

但我认为这是 PowerShell 控制台的最佳选项卡扩展功能!!!


有趣的。如果有办法做这样的事情,那么似乎很有可能使扩展像在 bash 中一样工作。不过,我远不是 PowerShell 方面的专家,所以可能会遗漏一些东西。
当然!开始研究 PowerTab 模块的代码以尝试满足您的扩展需求。但 PowerTab 以简单的选择方式为几乎任何命令、wmi、comobject、程序集提供扩展!
徐志鹏
# 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

修改 TabExpansion 函数来实现你想要的。请记住,如果您再次按 Tab 键,新建议可能会从您最初按键的位置修改,可能会完成到最后。我非常喜欢实际的行为,我希望尽可能快地写出这行代码。最后别忘了通配符扩展,例如:bu*h[Tab]自动补全到buildHouse.bat


修改 TabExpansion 函数可能是要走的路,但仍然比我想要的要复杂得多。我认为在我能够解决这个问题之前,我需要更流利地使用 powershell。
N
Nilesh Gule

使用 Powershell Core,我们可以将 PSReadLine 的 PredictionSource 属性设置为 History 以获取自动建议。有关详细信息,请参阅 YouTube 视频https://youtu.be/I0iIZe0dUNw


A
Anubioz

实际上,bash 行为由 /etc/inputrc 控制,它因发行版而异。

因此,这里是如何使 PowerShell 的行为更像是具有健全默认值的 bash(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