软件专栏
Windows Terminal 美化及通过右键菜单启动
4 年前
6516
配置 软件推荐 Windows

Windows Terminal 已经推出一段时间了,支持多选项卡并适用于 Powershell、WSL 等,还有更好的字符支持和渲染效果。工欲善其事必先利其器,本文通过配置来美化 Windows 终端,并让它支持通过右键菜单启动。

本文内容在 Windows Terminal 1.0.1401.0 版本下可用,由于软件版本时常更新,请注意本文时效性。

获取 Windows Terminal

Windows Terminal 是一款由微软开发的开源终端程序,Windows 10 用户可以通过 Windows Store 来安装。

美化

默认情况下,我们通过 Windows Terminal 使用命令提示符和 PowerShell 时,从视觉上看好像和直接使用它们没有什么不同,一个黑色一个蓝色。但我们可以配置 Windows Terminal 的样式效果。

首先认识一下 Windows Terminal 的配置文件

打开 Windows Terminal 配置

通过展开标签栏最右侧的菜单-设置,便会打开 settings.json 配置文件,我们涉及到:

  • profiles 终端支持的配置(即命令提示符、PowerShell 等安装后会自动生成一个配置)
  • schemes 配色方案

选择喜欢的配色方案

iTerm Color Schemes 项目中提供了很多配色方案,找到自己喜欢的配色方案后,复制下它的名字,并进入该项目的 windowsterminal 文件夹 下进行搜索。

包子喜欢黄色背景,个人感觉和黑色、白色比起来,更柔和一点,所以选择了 Violet Light,将 Violet Light.json 的内容粘贴到 settings.jsonschemes 节点中:

"schemes": [
    {
        "name": "Violet Light",
        "black": "#56595c",
        "red": "#c94c22",
        "green": "#85981c",
        "yellow": "#b4881d",
        "blue": "#2e8bce",
        "purple": "#d13a82",
        "cyan": "#32a198",
        "white": "#d3d0c9",
        "brightBlack": "#45484b",
        "brightRed": "#bd3613",
        "brightGreen": "#738a04",
        "brightYellow": "#a57705",
        "brightBlue": "#2176c7",
        "brightPurple": "#c61c6f",
        "brightCyan": "#259286",
        "brightWhite": "#c9c6bd",
        "background": "#fcf4dc",
        "foreground": "#536870"
    }
]

然后在 profiles 节点中,通过 name 找到想要应用该配色方案的程序,将 colorScheme 节点设置为配色方案的名称(对应 schemesname)。

值得注意的是,background 背景颜色和 cursorColor 光标颜色还需要手动配置,将配色方案的 background 设为背景色、black (或其他喜欢的颜色)设为光标颜色,否则会出现光标看不见的问题:

{
    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
    "name": "Windows PowerShell ",
    "commandline": "powershell.exe",

    // 以下为新增修改的内容
    "colorScheme": "Violet Light",
    "background": "#fcf4dc",
    "cursorColor": "#56595c"
}

设置 Cascadia Code 字体

Cascadia Code 字体

Windows Terminal 推荐使用 Cascadia Code 字体,安装 Windows Terminal 时会同时安装。它的字体名称为 Cascadia Mono,但默认似乎没有使用该字体,通过修改 profiles 里的 fontFace 节点来应用:

{
    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
    "name": "Windows PowerShell ",
    "commandline": "powershell.exe",    
    "colorScheme": "Violet Light",
    "background": "#fcf4dc",
    "cursorColor": "#56595c",

    // 以下为新增修改的内容
    "fontFace": "Cascadia Mono"
}

设置亚克力显示效果

亚克力效果 Acrylic material

Windows 10 引入了亚克力显示效果(Acrylic material),Windows Terminal 也支持使用亚克力来达到透明模糊的效果。通过 useAcrylic 启用亚克力显示效果,以及 acrylicOpacity 设置不透明度(0~1之间):

{
    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
    "name": "Windows PowerShell ",
    "commandline": "powershell.exe",    
    "colorScheme": "Violet Light",
    "background": "#fcf4dc",
    "cursorColor": "#56595c",
    "fontFace": "Cascadia Mono",

    // 以下为新增修改的内容
    "useAcrylic": true,
    "acrylicOpacity": 0.6
}

其它设置内容

  • fontSize 字体大小
  • cursorShape 光标样式
  • 还支持设置背景图片等

其他配置可以参考微软官方文档 Windows 终端中的配置文件设置

添加到右键快捷菜单

Open with Windows Terminal

通过资源管理器的右键快捷菜单快速启动 Windows Terminal,免去每次都通过 cd 命令输入路径的繁琐操作,我们需要修改注册表实现。

配置注册表

首先把 Windows Terminal 的图标保存到本地:右键另存(防止 GitHub 部分资源被墙无法下载所以提供的是本站的备份,原始文件在这里

接着打开文本编辑器,创建一个注册表文件(后缀名为 .reg),内容如下:

Windows Registry Editor Version 5.00
​
[HKEY_CLASSES_ROOT\Directory\Background\shell\wt]
@="Open with Windows Terminal"
"Icon"="E:\\icons\\terminal.ico"
​
[HKEY_CLASSES_ROOT\Directory\Background\shell\wt\command]
@="C:\\Users\\修改为你的用户名\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe"

注意将第5行 "Icon" 后面的内容替换为保存图标的完整路径,路径以\\分割文件夹;以及最后一行写入当前用户的用户名。

保存后双击该文件,即可将 Windows Terminal 添加到右键菜单。

从当前目录启动

回到 Windows Terminal 配置文件 settings.json,找到 defaultProfile 节点指向的 profiles 中对应的配置,Windows Terminal 启动时将会使用该程序。我们需要配置 startingDirectory 节点,把它设为从当前目录启动:

{
    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
    "name": "Windows PowerShell ",
    "commandline": "powershell.exe",    
    "colorScheme": "Violet Light",
    "background": "#fcf4dc",
    "cursorColor": "#56595c",
    "fontFace": "Cascadia Mono",
    "useAcrylic": true,
    "acrylicOpacity": 0.6,

    // 以下为新增修改的内容
    "startingDirectory": "."
}

这样通过右键快捷菜单启动 Windows Terminal 并进入当前目录就设置好了。

Windows Terminal 还支持很多其他设置,包括 Powerline 都可以在官方文档上找到。 还有颜色配置实用工具 ColorTool 可以自动设置 iTerm Color Schemes 的配色方案(当然,背景色和光标还是需要手动调整的),都可以尝试一下。

WSL 同样也支持通过 Windows Terminal 来进行调整,本文就不多赘述了。

安装 PowerShell 7

PowerShell 目前最新版本为 7,可以用它替代 Windows 10 内置的 Powershell,获得更好的体验。

如果机器上安装了 .NET Core,可以方便的将 PowerShell 7 安装为全局工具:

dotnet tool install --global PowerShell 

或是通过安装包等方式安装,参见官方文档 在 Windows 上安装 PowerShell

安装好 PowerShell 7 后,打开 Windows Terminal 的配置文件,会发现 profiles 中已经自动生成好,修改 defaultProfile 为对应的 guid,将 PowerShell 7 设为默认,确保启动 Windows Terminal 时使用的是 PowerShell 7 以体验最新功能。