自定义 Windows Terminal 终端界面与配色方案

目录
[隐藏]

什么是 Windows Terminal?官方简介如下:

Windows 终端是一个面向命令行工具和 shell(如命令提示符、PowerShell 和适用于 Linux 的 Windows 子系统 (WSL))用户的新式终端应用程序。 它的主要功能包括多个选项卡、窗格、Unicode 和 UTF-8 字符支持、GPU 加速文本呈现引擎,你还可用它来创建你自己的主题并自定义文本、颜色、背景和快捷方式。

用上了 Windows Terminal,或许你就不再需要 cmder 之类的工具了。本文主要介绍在 windows 10 系统下 Windows Terminal 安装与主题配色相关的配置方式参考。

1. 安装 Windows Terminal

你可以在 windows 10 的应用商店中搜索并安装。也可以从 github 中下载最新的版本安装或更新:

  • https://github.com/microsoft/terminal/releases

自定义 Powershell 终端提示符:安装和配置 Powerline

Powerline 提供自定义的命令提示符体验,提供 Git 状态颜色编码和提示符。在官方文档有详细的介绍,这里仅提供快捷的操作步骤参考:

1.1 下载并安装 cascadia-code 字体

从如下地址下载 microsoft/cascadia-code 字体并安装:

https://github.com/microsoft/cascadia-code/releases

将下载到的 zip 压缩包解压,可以选择 otf 目录下的字体,双击打开各字体文件然后点击“安装”按钮执行安装。

1.2 在 PowerShell 中设置 Powerline

首先需要确保已经安装了 Git 客户端。下载并安装 Git 客户端:https://git-scm.com/downloads

然后从 PowerShell 终端中执行如下命令,安装相关依赖工具模块:

# 安装 Posh-Git
Install-Module posh-git -Scope CurrentUser
# 安装 Oh-My-Posh
Install-Module oh-my-posh -Scope CurrentUser
# 安装 PSReadline
Install-Module -Name PSReadLine -Scope CurrentUser -Force -SkipPublisherCheck

PowerShell 终端中执行如下命令,编辑或创建 PowerShell 配置文件:

notepad $PROFILE

在配置文件末尾添加如下内容,以便在 PowerShell 启动时加载相关工具模块和设置:

Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt -Theme Paradox

Windows Terminal终端配置文件中,设置 PowerShell 的字体为 Cascadia Code PL。示例:

{
    "profiles": {
        "list": {
            {
               "guid": "{eeee4bbd-eeee-5271-eeee-eeee87ff44bf}",
               "name": "Windows PowerShell",
               "commandline": "powershell.exe",
               "fontFace": "Cascadia Code PL"
           },
        }
    }
}

2. 设置默认启动目录

进入 shell 默认的目录为当前登录用户主目录 %USERPROFILE%。可以通过设置 startingDirectory 参数自定义它。示例:

{
    "profiles": {
        "defaults": {
            // 指定所有终端的默认启动目录
            "startingDirectory": "D:\\coding\\work" // "%USERPROFILE%"
        },
        "list": {
            {
               "guid": "{eeee4bbd-eeee-5271-eeee-eeee87ff44bf}",
               "name": "Windows PowerShell",
               "commandline": "powershell.exe",
               "fontFace": "Cascadia Code PL",
               // 指定当前终端的默认启动目录
               "startingDirectory": "D:\\coding\\work",
           },
        }
    }
}

提示:在 defaults 中的设置是公共的配置,会被 list 所有终端类型所应用。

3. 为指定的 shell 环境设置背景图片

在对应的 shell 配置中添加 backgroundImagebackgroundImageOpacitybackgroundImageAlignment 等参数配置即可。示例参考如下:

{
    "profiles": {
        "list": {
            {
               "guid": "{eeee4bbd-eeee-5271-eeee-eeee87ff44bf}",
               "name": "Windows PowerShell",
               "commandline": "powershell.exe",
               "colorScheme": "lzwmeThemeColor",
               "backgroundImage": "C:\\Users\\lzw\\Pictures\\wallpaper\\000706.jpg",
               "backgroundImageOpacity": 0.25,
               "backgroundImageAlignment": "center",
               "useAcrylic": true,
               "acrylicOpacity" : 0.7,
               "fontSize": 16,
               "hidden": false
           },
        }
    }
}

4. 自定义主题配色方案

打开 Windows Terminal 的配置文件,在 schemes 下添加配色方案,例如设置其 name 值为 lzwmeThemeColor,然后修改 PowerShell 的配置,添加 colorScheme": "lzwmeThemeColor", 指定使用 lzwmeThemeColor 配色方案。主要配置代码示例如下:

{
    "schemes": [
        {
            "name": "lzwmeThemeColor",
            "black": "#151515",
            "red": "#ac4142",
            "green": "#0eeb7d",
            "yellow": "#f3a62a",
            "blue": "#6c99bb",
            "purple": "#9f4e85",
            "cyan": "#7dd6cf",
            "white": "#d0d0d0",
            "brightBlack": "#505050",
            "brightRed": "#ac4142",
            "brightGreen": "#7e8e50",
            "brightYellow": "#e5b567",
            "brightBlue": "#6c99bb",
            "brightPurple": "#9f4e85",
            "brightCyan": "#7dd6cf",
            "brightWhite": "#f5f5f5",
            "background": "#212121",
            "foreground": "#d0d0d0"
        }
    ],
    "profiles": {
        "list": {
            {
               "guid": "{eeee4bbd-eeee-5271-eeee-eeee87ff44bf}",
               "name": "Windows PowerShell",
               "commandline": "powershell.exe",
               "colorScheme": "lzwmeThemeColor",
               "hidden": false
           },
        }
    }
}

从上面你的示例可以看到,自定义配色方案主要就是在 schemes 下定义各预定义颜色的具体值。Windows Terminal Themes 这个网站提供了多达 228+ 种不同配色方案,如有兴趣可以自行体验和选用:相中了你喜欢的配色方案后,点击 Get theme 按钮,即可将配色方案复制到剪切板中。

5. 自定义 shell: 新增 Git bash 终端类型

list 字段中可以增加配置任意可启动的终端类型,只需要通过 commandline 指定启动的可执行程序,再辅以其他相关的配置信息即可。

下面为增加一个 Git bash 终端类型的示例:

{
    "profiles": {
        "list": {
            {
                "name": "Git Bash",
                "guid": "{eeeee-4e3d-5e58-b989-0a998ec4abcd}",
                "closeOnExit": true,
                "colorScheme": "lzwmeThemeColor", // Campbell
                "commandline": "D:\\Program Files\\Git\\bin\\bash.exe",
                "icon": "D:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico",
                "historySize": 9999,
                "padding": "0, 0, 0, 0",
                "snapOnInput": true,
                "fontSize": 18,
                "hidden": false
           },
        }
    }
}

注意 guid 的取值,应为 a-f0-9 之间的字符。

6. 总结与完整示例

以上便完成了对 Windows Terminal 基本的个性化配置。其中各主题设置、配色方案等均可按你自己喜欢的方式进行设置。

一个完整的配置参考:

{
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "defaultProfile": "{aaaaaaaa-c2c6-5271-96e7-009a87ff44bf}",
    "copyOnSelect": true,
    "copyFormatting": true,
    // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
    "profiles": {
        "defaults": {
            "startingDirectory": "D:\\coding\\work", // "%USERPROFILE%"
            "cursorShape": "bar",
            "fontFace": "Consolas",
            "backgroundImage": "C:\\Users\\lzw\\Pictures\\wallpaper\\000706.jpg",
            "backgroundImageOpacity": 0.35
        },
        "list":
        [
            {
                "guid": "{aaaaaaaa-c2c6-5271-96e7-009a87ff44bf}",
                "name": "Windows PowerShell",
                "commandline": "powershell.exe",
                // "source" : "Windows.Terminal.PowershellCore",
                "backgroundImage": "C:\\Users\\lzw\\Pictures\\wallpaper\\ysdhwexx.jpg",
                "backgroundImageOpacity": 0.15,
                "backgroundImageAlignment": "center",
                "useAcrylic": true,
                "acrylicOpacity" : 0.7,
                "colorScheme" : "lzwmeThemeColor",
                "cursorColor" : "#FFFFFD",
                "startingDirectory": "D:\\coding\\work",
                "fontSize": 16,
                "fontFace" : "Cascadia Code PL",
                "hidden": false
            },
            {
                "guid": "{aaaaaaaa-35be-5f56-a8ff-afceeeaa6101}",
                "name": "命令提示符",
                "commandline": "cmd.exe",
                "hidden": false
            },
            {
                "name": "Git Bash",
                "guid": "{aaaaaaaa-4e3d-5e58-b989-0a998ec4abcd}",
                "closeOnExit": true,
                "colorScheme": "lzwmeThemeColor", // Campbell
                "commandline": "D:\\Program Files\\Git\\bin\\bash.exe",
                "icon": "D:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico",
                "historySize": 9999,
                "padding": "0, 0, 0, 0",
                "snapOnInput": true,
                "fontSize": 18,
                "hidden": false
            },
            {
                "guid": "{aaaaaaaa-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": true,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            }
        ]
    },
    // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
    "schemes": [
        {
            "name": "lzwmeThemeColor",
            "black": "#151515",
            "red": "#ac4142",
            "green": "#0eeb7d",
            "yellow": "#f3a62a",
            "blue": "#6c99bb",
            "purple": "#9f4e85",
            "cyan": "#7dd6cf",
            "white": "#d0d0d0",
            "brightBlack": "#505050",
            "brightRed": "#ac4142",
            "brightGreen": "#7e8e50",
            "brightYellow": "#e5b567",
            "brightBlue": "#6c99bb",
            "brightPurple": "#9f4e85",
            "brightCyan": "#7dd6cf",
            "brightWhite": "#f5f5f5",
            "background": "#212121",
            "foreground": "#d0d0d0"
        }
    ],
    "actions": [
        { "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
        { "command": "paste", "keys": "ctrl+v" },

        // Press Ctrl+Shift+F to open the search box
        { "command": "find", "keys": "ctrl+shift+f" },
        { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" },
        { "command": { "action": "newTab", "index": 0 }, "keys": "ctrl+n" },
        { "command": "closePane", "keys": "ctrl+w" }
    ]
}

效果图参考:

相关参考

  • https://github.com/microsoft/terminal/releases/
  • https://docs.microsoft.com/zh-cn/windows/terminal/custom-terminal-gallery/custom-schemes
  • https://docs.microsoft.com/zh-cn/windows/terminal/tutorials/powerline-setup
  • https://ohmyposh.dev/docs/configure/
点赞 (1)

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据