【问题标题】:Change the default "Edit" action for .ps1 from ISE to VS Code将 .ps1 的默认“编辑”操作从 ISE 更改为 VS Code
【发布时间】:2022-09-28 13:16:32
【问题描述】:

就像this question 的人一样,令人沮丧的是,如此明显的事情没有明确的答案(我看了 20 页,还没有找到)。

Windows 中.ps1 文件的默认右键单击“编辑”操作是使用 PowerShell_ISE 打开。 ISE是一个古老而臃肿的旧时代遗物,而且打开的速度非常慢,所以当你不小心点击右键>编辑时,看着ISE慢慢打开是很曲折的。

我们如何以编程方式更改 .ps1 / .psm1 等的默认“编辑”操作,以便它们指向 VS Code 而不是 ISE?

或者,如果我们不能更改它,我们可以完全删除它吗(这样“使用 VS Code 编辑”是唯一剩下的选项)?

    标签: powershell visual-studio-code registry powershell-ise


    【解决方案1】:

    这实际上很容易在 PowerShell 中完成,或者通过手动调整注册表来完成。

    这是一个可以改变事情的脚本。

    因为您要更改 HKEY_CLASSES_ROOT,所以您需要以管理员身份运行:

    # This a "provider qualified" path to the edit command
    $editRegistryPath = 'Microsoft.PowerShell.Core\Registry::HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\Edit\Command'
    
    # This is how we will get the default value
    $defaultEdit = Get-ItemProperty $editRegistryPath  -Name '(default)'
    
    # If we couldn't get it, something's wrong, so return
    if (-not $defaultEdit) {
        return
    }
    
    # Get the path to code.cmd (this must be in $env:PATH)
    $codePath = Get-Command code -CommandType Application | Select-Object -First 1 | Select-Object -ExpandProperty Source
    
    # Change the property.  Make sure we quote our command name and our arguments.
    Set-ItemProperty -Path $editRegistryPath -Name '(default)' -Value "`"$($codePath)`" `"%1`""
    

    【讨论】:

    • 这非常好,谢谢,并且可以立即工作(即使我的 VS Code 位于非标准安装位置),但我很好奇 - 你为什么要针对 code.cmd 而不是 code.exe
    • 两个原因:1. 它是用来从命令行运行它的 2. .exe 在 Windows 11 上不起作用(它一直给我选择对话框)
    【解决方案2】:

    您只需在以下注册表项中将 exe 路径更改为 vs code

    Computer\HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\Edit\Command
    

    双击默认属性并将 ISE 路径替换为 vscode.exe 的路径

    【讨论】:

    • 谢谢。我不能让它工作。 reg add "HKCR\SystemFileAssociations\.ps1\Shell\Edit\Command" /VE /T REG_SZ /D "C:\Program Files\Microsoft VS Code\Code.exe" "%1" 错误:语法无效。我猜想与" 字符有关。
    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2023-01-22
    相关资源
    最近更新 更多