powershell 是否有与 zsh 的 vared 类似的功能?

powershell 是否有与 zsh 的 vared 类似的功能?

假设你进入 PowerShell 并添加了一条路径:

$env:path += 'C:\banal\cranial\inversion'

哎呀,你忘了;它应该是

$env:path += ';C:\banal\cranial\inversion'

-- 但您的路径已经搞砸了。在 zsh 中,我们只需vared PATH;那么 PowerShell 呢,有没有办法编辑变量而不是重新重置它?如果重置,如果它环绕,您如何复制并粘贴其中的一部分?

答案1

事实上不行,但是你可以这样做:

    function Edit-Variable {
#.Parameter name
#    The name (or path) to the variable to edit.
#.Parameter Environment
#    Optional switch to force evaluating the name as an environment variable. You don't need this if you specify the path as env:Path instead of just "Path"
#.Example
#     Edit-Variable -env path
#.Example
#     Edit-Variable profile
#.Example
#     Edit-Variable env:\path
#.Example
#     Edit-Variable variable:profile

param(
    [Parameter(Position=0,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
    [string]$name
,
    [switch]$Environment
)
process {
    $path = Resolve-Path $name -ErrorAction SilentlyContinue
    if($Environment) {
        ## Force Env: if they said -Env
        if(!$path -or $Path.Provider.Name -ne "Environment") {
            $path = Resolve-Path "Env:$name"
        }
    } else {
        if($Path -and $Path.Provider.Name -eq "Environment") {
            $Environment = $true
        } elseif(!$path -or $Path.Provider.Name -ne "Variable") {
            $path = Resolve-Path "Variable:$name" -ErrorAction SilentlyContinue
        }
    }

    $temp = [IO.Path]::GetTempFileName()
    if($path) {
        if(!$Environment) {
            $value = (Get-Variable $path.ProviderPath).Value
            $string = $value -is [String]
            if(!$string) {
                Write-Warning "Variable $name is not a string variable, editing as CliXml"
                Export-Clixml $temp -InputObject $Value 
            } else {
                Set-Content $temp $Value
            }
        } else {
            Get-Content $path | Set-Content $temp
        }
    } else {
        $Environment = $false
        New-Variable $Name
        $path = Resolve-Path Variable:$name -ErrorAction SilentlyContinue
    }
    if(!$path) {
        Write-Error "Cannot find variable '$name' because it does not exist."
    } else {
        # Let the user edit it in notepad, and see if they save it
        $pre = Get-ChildItem $temp
        (Start-Process notepad $temp -passthru).WaitForExit()
        $post = Get-ChildItem $temp
        if($post.LastWriteTime -gt $pre.LastWriteTime) {
            if(!$Environment) {
                if(!$string) {
                    Import-CliXml $temp | Set-Variable $path.ProviderPath
                } else {
                    Get-Content $temp | Set-Variable $path.ProviderPath
                }
            } else {
                Get-Content $temp | Set-Content $path
            }
        }
    }
    Remove-Item $temp -ErrorAction SilentlyContinue
}
}

Set-Alias vared Edit-Variable

我知道 zsh 的工作方式不是这样的,但是记事本很方便......

答案2

您可以使用字符串方法来完成更正

$indexError = $env:Path.LastIndexOf("C:")
$env:Path = $env:Path.remove($indexError, 1)
$env:Path = $env.Insert($indexError, ";C")

不要忘记,在 powershell 中一切都是对象,您可以使用任何编程策略来实现您的需要。

相关内容