Windows折腾记之Windows Terminal配置

最近闲来无事,开始了对我可怜的Windows电脑一系列的折腾-_-

首当其冲的,就是这个新生代、据说是全网最好用的终端:Windows Terminal。

文章目录

下载与安装

Windows Terminal安装并不需要过于复杂的步骤,只要保证你是Windows10,并且版本不低于1903。

打开Win10自带的应用商店,搜索“Windows Terminal”,安装第一个即可,是不是很简单呢?

Windows Terminal配置

终于到了折腾的环节!Windows Terminal(简称“WT”)亮点之一(也是槽点之一)就是它没有配置界面,取而代之的是一个JSON文件,点击配置后会用你默认的编辑器打开这个文件(注:安装了VS的童鞋要注意了,VS会把JSON的默认打开方式设为VS,我当时就是这样被坑了,点了好几下配置,正想着为什么打不开,突然弹出了VS的启动界面,只要把JSON的默认打开方式设为一些轻量级编辑器即可)。在这里先贴一下我自己的配置代码,需要的小伙伴可以拿走(有删减):

// This file was initially generated by Windows Terminal 1.0.1811.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
    "$schema": "https://aka.ms/terminal-profiles-schema",

    "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

    // You can add more global application settings here.
    // To learn more about global settings, visit https://aka.ms/terminal-global-settings

    // If enabled, selections are automatically copied to your clipboard.
    "copyOnSelect": false,

    // If enabled, formatted data is also copied to your clipboard
    "copyFormatting": false,

    // A profile specifies a command to execute paired with information about how it should look and feel.
    // Each one of them will appear in the 'New Tab' dropdown,
    //   and can be invoked from the commandline with `wt.exe -p xxx`
    // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
    "profiles":
    {
        "defaults":
        {
            // Put settings here that you want to apply to all profiles.
            "useAcrylic": true,
            "acrylicOpacity": 0.8,
            "colorScheme": "Monokai Remastered",
            "fontFace": "Fira Mono"
        },
        "list":
        [
            {
                // Make changes here to the pwsh.exe profile.
                "guid": "{在这里填你的GUID}",
                "name": "Windows PowerShell",
                "commandline": "pwsh.exe",
                "hidden": false
            },
            {
                // Make changes here to the cmd.exe profile.
                "guid": "{在这里填你的GUID}",
                "name": "命令提示符",
                "commandline": "cmd.exe",
                "hidden": false
            },
            {
                "guid": "{在这里填你的GUID}",
                "name": "SSH Server",
                "commandline": "putty.exe xinzhe@XinzheServer",
                "hidden": false
            },
            {
                "guid": "{在这里填你的GUID}",
                "hidden": false,
                "name": "Ubuntu-20.04",
                "source": "Windows.Terminal.Wsl"
            },
            {
                "guid": "{在这里填你的GUID}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            }
        ]
    },

    // Add custom color schemes to this array.
    // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
    "schemes": [
        {
            "name": "Dark+",
            "black": "#000000",
            "red": "#cd3131",
            "green": "#0dbc79",
            "yellow": "#e5e510",
            "blue": "#2472c8",
            "purple": "#bc3fbc",
            "cyan": "#11a8cd",
            "white": "#e5e5e5",
            "brightBlack": "#666666",
            "brightRed": "#f14c4c",
            "brightGreen": "#23d18b",
            "brightYellow": "#f5f543",
            "brightBlue": "#3b8eea",
            "brightPurple": "#d670d6",
            "brightCyan": "#29b8db",
            "brightWhite": "#e5e5e5",
            "background": "#0e0e0e",
            "foreground": "#cccccc"
        },
        {
            "name": "Monokai Remastered",
            "black": "#1a1a1a",
            "red": "#f4005f",
            "green": "#98e024",
            "yellow": "#fd971f",
            "blue": "#9d65ff",
            "purple": "#f4005f",
            "cyan": "#58d1eb",
            "white": "#c4c5b5",
            "brightBlack": "#625e4c",
            "brightRed": "#f4005f",
            "brightGreen": "#98e024",
            "brightYellow": "#e0d561",
            "brightBlue": "#9d65ff",
            "brightPurple": "#f4005f",
            "brightCyan": "#58d1eb",
            "brightWhite": "#f6f6ef",
            "background": "#0c0c0c",
            "foreground": "#d9d9d9"
        }
    ],

    // Add custom keybindings to this array.
    // To unbind a key combination from your defaults.json, set the command to "unbound".
    // To learn more about keybindings, visit https://aka.ms/terminal-keybindings
    "keybindings":
    [
        // Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
        // These two lines additionally bind them to Ctrl+C and Ctrl+V.
        // To learn more about selection, visit https://aka.ms/terminal-selection
        { "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" },

        // Press Alt+Shift+D to open a new pane.
        // - "split": "auto" makes this pane open in the direction that provides the most surface area.
        // - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
        // To learn more about panes, visit https://aka.ms/terminal-panes
        { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
    ]
}

这是一份不完全的配置文件,在后面的文章中还会继续添加内容。下面来讲解一下这些配置的意思。

标签页预设

作为一个好用的终端,标签页是必不可少的东西。那么标签页预设又是什么呢?简单来说,就是不同的标签页种类。通过配置,可以实现新建一个SSH标签页这种功能,有点类似于一个终端可以集成bash、zsh等多种Shell。

那么如何配置呢?在“profile”的“list”中添加如下代码:

"profile":
{
    "list":
    [
        {
            "guid": "{在这里填你的GUID}",
            "name": "吧啦吧啦吧啦",
            "commandline": "吼吼吼",
            "hidden": false
        }
    ]
}

其中“name”项是显示的名称,“commandline”项是要执行的命令,而“hidden”项则是是否隐藏。至于“guid”项,后面会解释。下面举一个普通PowerShell标签页的例子:

"profile":
{
    "list":
    [
        {
            "guid": "{df396da7-8d8c-4344-bab7-9587bbf88fab}",
            "name": "Windows PowerShell",
            "commandline": "pwsh.exe",
            "hidden": false
        }
    ]
}

这段文字的意思是添加一个叫做“Windows PowerShell”的标签页预设,当用户要打开它时,执行“pwsh.exe”。也就是说,打开它时就相当于打开了一个PowerShell。当然,PowerShell在WT中已经默认存在了,下面举一个SSH的例子:

"profile":
{
    "list":
    [
        {
            "guid": "{df396da7-8d8c-4344-bab7-9587bbf88fab}",
            "name": "Windows PowerShell",
            "commandline": "pwsh.exe",
            "hidden": false
        },
        {
            "guid": "{6490914f-3a3f-4825-be48-01f1c8d6ebf9}",
            "name": "SSH Server",
            "commandline": "ssh username@sshserver",
            "hidden": false
        }
    ]
}

打开“SSH Server”后,WT会自动调用ssh使用username作为用户名尝试登录sshserver,正确输入密码后即可登录SSH服务器。怎么样,是不是很方便?

那么GUID到底是什么呢?简单来说,就是一种唯一不重复的标识符(详细信息请参见百度百科)。至于生成GUID,Visual Studio内置有一个GUID生成器,相关内容可自己上网搜索。没有安装Visual Studio的童鞋也不必担心,我推荐一个可以在线生成GUID的网站:Generate GUIDs online,打开网页后直接复制中间的一串类似乱码的文字即可,点击“Generate new GUID”即可生成新的GUID。

配置好后,在WT主界面中最上面一条会看到已打开的标签页、符号“+”和一个类似于“▽”、只不过没有底边的符号,点击“▽”,在下拉栏中可以选择自己配置的标签页预设,直接打开即可。

外观配置

下面来说一下WT的外观配置。作为一个新生代的强大终端,肯定要配得好看才行啊!我们先来看一下我配置文件第一段的内容(defaults一段):

"profile":
{
    "defaults":
    {
        // Put settings here that you want to apply to all profiles.
        "useAcrylic": true,
        "acrylicOpacity": 0.8,
        "colorScheme": "Monokai Remastered",
        "fontFace": "Fira Mono"
    }
    "list":
    [
        {
            "guid": "{df396da7-8d8c-4344-bab7-9587bbf88fab}",
            "name": "Windows PowerShell",
            "commandline": "pwsh.exe",
            "hidden": false
        },
        {
            "guid": "{6490914f-3a3f-4825-be48-01f1c8d6ebf9}",
            "name": "SSH Server",
            "commandline": "ssh username@sshserver",
            "hidden": false
        }
    ]
}

“defaults”代表的是对所有标签页预设都有效,如果想要对某个特定的预设起效果,请在预设设置内添加内容,以刚才的SSH预设举例子:

"profile":
{
    "list":
    [
        {
            "guid": "{df396da7-8d8c-4344-bab7-9587bbf88fab}",
            "name": "Windows PowerShell",
            "commandline": "pwsh.exe",
            "hidden": false
        },
        {
            "guid": "{6490914f-3a3f-4825-be48-01f1c8d6ebf9}",
            "name": "SSH Server",
            "commandline": "ssh username@sshserver",
            "hidden": false
            "useAcrylic": true, // 背景透明
            "acrylicOpacity": 0.8, // 不透明度
            "colorScheme": "Monokai Remastered", // 色彩主题
            "fontFace": "Fira Mono" // 字体
        }
    ]
}

这样子,你打开“SSH Server”预设时会出现的效果为背景有20%的透明、主题为“Monokai Remastered”、使用“Fira Mono”字体,但其他标签页预设却不会出现此类效果(如你打开“Windows PowerShell”标签页时就是默认的样式)。

在标签预设设置内添加样式设置的好处是可以区分不同种类的标签页,比如运行WSL的标签页可以将背景改为Ubuntu红,运行PowerShell的标签页可以将背景改为Win蓝,或者干脆弄一个Windows图标当作背景,避免出现在WSL下运行PowerShell命令这种错误。

主题

可以看见上面的设置代码中有“colorScheme”(色彩主题)一条,我选择的是“Monokai Remastered”,那么主题从哪里选择呢?

事实上,WT的色彩主题是要自己编写的。就在“schemes”里编写,然后在“profile”里调用。

"profile":
{
    "defaults":
    {
        // Put settings here that you want to apply to all profiles.
        "useAcrylic": true,
        "acrylicOpacity": 0.8,
        "colorScheme": "Monokai Remastered",
        "fontFace": "Fira Mono"
    }
    "list":
    [
        {
            "guid": "{df396da7-8d8c-4344-bab7-9587bbf88fab}",
            "name": "Windows PowerShell",
            "commandline": "pwsh.exe",
            "hidden": false
        },
        {
            "guid": "{6490914f-3a3f-4825-be48-01f1c8d6ebf9}",
            "name": "SSH Server",
            "commandline": "ssh username@sshserver",
            "hidden": false
        }
    ]
},

"schemes": [
    {
        "name": "Monokai Remastered",
        "black": "#1a1a1a",
        "red": "#f4005f",
        "green": "#98e024",
        "yellow": "#fd971f",
        "blue": "#9d65ff",
        "purple": "#f4005f",
        "cyan": "#58d1eb",
        "white": "#c4c5b5",
        "brightBlack": "#625e4c",
        "brightRed": "#f4005f",
        "brightGreen": "#98e024",
        "brightYellow": "#e0d561",
        "brightBlue": "#9d65ff",
        "brightPurple": "#f4005f",
        "brightCyan": "#58d1eb",
        "brightWhite": "#f6f6ef",
        "background": "#0c0c0c",
        "foreground": "#d9d9d9"
    }
]

可以看到这段配置相对于上一段配置,新增的是“schemes”。

WT的主题虽说没有给你,但也并不是要完全自己写。GitHub上有位大佬搞了一个色彩主题大全:iTerm2 Color Schemes。虽说名字是iTerm2的主题,但其实内容包罗万象。进入仓库后,打开“windowsterminal”文件夹,从中选择自己喜爱的主题打开(假设我们选择的主题是“Dark+”,就要打开“Dark+.json”),点击代码框右上角的“raw”按钮,复制所有的代码内容,将其黏贴至“schemes”下,如代码:

"profile":
{
    "defaults":
    {
        // Put settings here that you want to apply to all profiles.
        "useAcrylic": true,
        "acrylicOpacity": 0.8,
        "colorScheme": "Monokai Remastered",
        "fontFace": "Fira Mono"
    }
    "list":
    [
        {
            "guid": "{df396da7-8d8c-4344-bab7-9587bbf88fab}",
            "name": "Windows PowerShell",
            "commandline": "pwsh.exe",
            "hidden": false
        },
        {
            "guid": "{6490914f-3a3f-4825-be48-01f1c8d6ebf9}",
            "name": "SSH Server",
            "commandline": "ssh username@sshserver",
            "hidden": false
        }
    ]
},

"schemes": [
    {
        "name": "Monokai Remastered",
        "black": "#1a1a1a",
        "red": "#f4005f",
        "green": "#98e024",
        "yellow": "#fd971f",
        "blue": "#9d65ff",
        "purple": "#f4005f",
        "cyan": "#58d1eb",
        "white": "#c4c5b5",
        "brightBlack": "#625e4c",
        "brightRed": "#f4005f",
        "brightGreen": "#98e024",
        "brightYellow": "#e0d561",
        "brightBlue": "#9d65ff",
        "brightPurple": "#f4005f",
        "brightCyan": "#58d1eb",
        "brightWhite": "#f6f6ef",
        "background": "#0c0c0c",
        "foreground": "#d9d9d9"
    },
    {
        "name": "Dark+",
        "black": "#000000",
        "red": "#cd3131",
        "green": "#0dbc79",
        "yellow": "#e5e510",
        "blue": "#2472c8",
        "purple": "#bc3fbc",
        "cyan": "#11a8cd",
        "white": "#e5e5e5",
        "brightBlack": "#666666",
        "brightRed": "#f14c4c",
        "brightGreen": "#23d18b",
        "brightYellow": "#f5f543",
        "brightBlue": "#3b8eea",
        "brightPurple": "#d670d6",
        "brightCyan": "#29b8db",
        "brightWhite": "#e5e5e5",
        "background": "#0e0e0e",
        "foreground": "#cccccc"
    }
]

我们现在拥有了两个主题,可以随意调用。如何调用呢?在每个主题代码第一行总有一个“name”项,这就是主题的名字。然后在你想要调用的地方(“defaults”内或标签页预设内)输入

"colorScheme": "主题名字"

即可。

搭配WSL

WSL,是一个Windows下内置的Linux系统,可以自己选择装哪些Linux系统(不过得是应用商店里有的,至于如何安装和使用WSL我以后还会写一篇文章,这里先占个坑)。至于搭配WT,实在是再简单不过的事了。在安装好WSL后WSL会在WT下自动创建一个标签页预设,只要在WT中直接打开即可运行WSL。

写在最后

WT的配置文件中内置了PowerShell和命令提示符两个标签页预设,自己还可以添加很多预设,如上文提到的SSH标签页。

可惜的是WT并没有内置什么主题,只好自己复制黏贴。

WT被网友评为“Windows下最好的终端之一”,但我想说没有之一。Windows下之前自带的PowerShell和Cmd终端就不用说了,我也用过Cmder之类的第三方终端,但总有各种各样的问题,而WT作为微软官方的“亲儿子”,自然稳定性不会太差,其易用性和可自定义性甚至不比Cmder等要差,使用也更为简单。总之,赶快用起来吧!

1人评论了“Windows折腾记之Windows Terminal配置”

  1. Pingback: Windows折腾记之PowerShell 7 - 向昕哲的博客

发表评论

滚动至顶部