如何更改默认的Powershell CLI行为以忽略$Env:Path
环境变量中存在的文件类型的文件扩展名?
如果我只是键入python
,它将正常工作,并按我期望的那样将我放入Python解释器中,因为扩展名是环境变量的一部分。
问题是,当我pyth
在PowerShell中键入并按Tab键完成时,它总是会附加.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
}
}
我在以开头的那一行之后添加了几行$field
;它会通过默认的制表符完成选项,并使扩展名与您的扩展名失去联系PATH
。我使用以下命令得到了原始源代码:
(Get-Command 'TabExpansion2').ScriptBlock
如果将新函数放在.ps1
文件中并点执行该脚本(例如. .\tabnoext.ps1
),它将成为当前会话的制表符完成处理程序。要在每次打开PowerShell窗口时加载它,只需将所有代码粘贴到PowerShell配置文件脚本中。
如果您使用的是旧版PowerShell,则需要覆盖该TabExpansion
函数,该函数仅返回字符串数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句