jmr*_*cha 5 windows powershell command-line
如何更改默认的 Powershell CLI 行为以忽略存在于我的$Env:Path环境变量中的文件类型的文件扩展名?
如果我只输入python,它就可以正常工作并将我放入 Python 解释器中,因为扩展是环境变量的一部分。
问题是,当我pyth在 PowerShell 中键入和 tab-complete 时,它总是附加.exe. 我只希望它在没有扩展名的情况下完成命令的第一部分。
这可能吗?也许是脚本?
您可以使用自己的选项卡完成功能覆盖标准。在最新版本的 PowerShell 中,该函数是TabExpansion2. 对它的这种修改似乎可以满足您的需求:
Function TabExpansion2 {
[CmdletBinding(DefaultParameterSetName = 'ScriptInputSet')]
Param(
[Parameter(ParameterSetName = 'ScriptInputSet', Mandatory = $true, Position = 0)]
[string] $inputScript,
[Parameter(ParameterSetName = 'ScriptInputSet', Mandatory = $true, Position = 1)]
[int] $cursorColumn,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 0)]
[System.Management.Automation.Language.Ast] $ast,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 1)]
[System.Management.Automation.Language.Token[]] $tokens,
[Parameter(ParameterSetName = 'AstInputSet', Mandatory = $true, Position = 2)]
[System.Management.Automation.Language.IScriptPosition] $positionOfCursor,
[Parameter(ParameterSetName = 'ScriptInputSet', Position = 2)]
[Parameter(ParameterSetName = 'AstInputSet', Position = 3)]
[Hashtable] $options = $null
)
End
{
$source = $null
if ($psCmdlet.ParameterSetName -eq 'ScriptInputSet')
{
$source = [System.Management.Automation.CommandCompletion]::CompleteInput(
<#inputScript#> $inputScript,
<#cursorColumn#> $cursorColumn,
<#options#> $options)
}
else
{
$source = [System.Management.Automation.CommandCompletion]::CompleteInput(
<#ast#> $ast,
<#tokens#> $tokens,
<#positionOfCursor#> $positionOfCursor,
<#options#> $options)
}
$field = [System.Management.Automation.CompletionResult].GetField('completionText', 'Instance, NonPublic')
$source.CompletionMatches | % {
If ($_.ResultType -eq 'Command' -and [io.file]::Exists($_.ToolTip)) {
$field.SetValue($_, [io.path]::GetFileNameWithoutExtension($_.CompletionText))
}
}
Return $source
}
}
Run Code Online (Sandbox Code Playgroud)
我在以$field;开头的那一行之后添加了这些行 它通过默认的选项卡完成选项,并将扩展名去掉那些似乎来自您的PATH. 我用这个命令得到了原始来源:
(Get-Command 'TabExpansion2').ScriptBlock
Run Code Online (Sandbox Code Playgroud)
如果您将新函数放在.ps1文件中并点执行该脚本(例如. .\tabnoext.ps1),它将成为当前会话的选项卡完成处理程序。要在每次打开 PowerShell 窗口时加载它,只需将所有代码粘贴到PowerShell 配置文件脚本中即可。
如果您使用的是旧版本的 PowerShell,则需要覆盖该TabExpansion函数,该函数仅返回一个字符串数组。
| 归档时间: |
|
| 查看次数: |
439 次 |
| 最近记录: |