假设我的当前目录中有以下文件:
buildBar.bat
buildFoo.bat
buildHouse.bat
我在命令提示符下键入以下内容,
./bu
,然后按TAB。
在 Bash 中,它扩展为
./build
在 PowerShell 中,它扩展为
./buildBar.bat
——列表中的第一项。在 Cmd 中,行为与 PowerShell 相同。
我更喜欢 Bash 行为 - 有没有办法让 PowerShell 表现得像 Bash?
新版本的 PowerShell 包括 PSReadline,可用于执行此操作:
Set-PSReadlineKeyHandler -Key Tab -Function Complete
或者,让它更像 bash,您可以使用箭头键来导航可用选项:
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
要使其永久化,请将此命令放入由
$PROFILE
定义的 powershell 配置文件中(对于 Windows PowerShell 5.x 通常为 %UserProfile%\Documents\WindowsPowerShell\profile.ps1
,对于 PowerShell 6+ 为 %UserProfile%\Documents\PowerShell\profile.ps1
)。
tab
仅完成命令名称,而不是其之前的参数。
要使用历史记录中的参数自动完成完整命令,请设置以下键绑定。
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
现在,输入命令名称的几个字符,然后使用向上/向下箭头从历史记录中自动完成此命令(带参数)。
实时节省时间。
查看更多:启动您的 PowerShell
现在可以使用 PSReadline 让 PowerShell 执行 Bash 风格的补全。
查看博客文章 PowerShell 中类似 Bash 的选项卡补全。
# 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
修改TabExpansion函数来实现你想要的。请记住,如果您再次按 Tab 键,新的建议可能会从您最初按键的位置进行修改,直到最后为止。我非常喜欢实际的行为,我希望尽快写出该行。最后别忘了通配符扩展,例如:bu*h[Tab]自动补全为buildHouse.bat
实际上,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
使用 Powershell Core,我们可以将 PSReadLine 的 PredictionSource 属性设置为 History 以获得自动建议。请参阅 YouTube 视频了解更多详情 https://youtu.be/I0iIZe0dUNw
受到Anubioz的回答的启发和激发,以下是可用完成预设的差异:
选项 | 建议 | 骑自行车 | 扩张 | 类似于 |
---|---|---|---|---|
|
静态打印 | 不 | 不 | msys2 默认 |
|
静态打印 | 不 | 是的 | |
|
互动 | 视觉 | 是的 | shell 默认 |
|
不 | 盲行 | 是的 | 默认 |
|
不 | 盲行 | 是的 | 默认 |
$env:USERPROFILE
)将变量扩展(如
'C:\Users\<name>'
为其值,即
lsd $env:userprofile/.
vitabcomplete*
,它看起来与普通的制表符完整相同..也许有人可以纠正我MenuPossibleCompletions
或MenuCompletions
这样的选项;但似乎最古老的答案建议使用 tabexpansion 函数自行定义它是实现这一目标的唯一方法,哈哈为了检查这是否就是全部,我在 msys2 bash 中做了以下操作。看起来这些确实是所有可用的补全预设。
命令(每行打印得很漂亮)
powershell -c Set-PSReadlineKeyHandler -Key Tab -Function MenuCompletions 2>&1 |
tr -d \\r\\n |
tr , \\n |
grep -i complet
结果:
Set-PSReadLineKeyHandler : Cannot validate argument on parameter 'Function'. The argument "MenuCompletions" does not belong to the set "Abort
Complete
MenuComplete
PossibleCompletions
TabCompleteNext
TabCompletePrevious
ViTabCompleteNext
ViTabCompletePrevious
YankPop" specified by the ValidateSet attribute. 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 ]