玩转 Windows Terminal

2020-07-13 10:17:25 浏览数 (1)

今天给大家分享一下Windows Terminal的使用及个性化定制。

一、安装

该项目的开源地址为https://github.com/microsoft/terminal,如果想折腾,可以按照上面的说明自己编译。不想折腾的直接在microsoft store直接搜索Windows Terminal,会搜到一个Windows Terminal 和一个Windows Terminal Preview版本。

本文以Preview版为例。

安装好Windows Terminal Preview之后,默认打开了PowerShell,发现和原来的没多大区别,颜色像CMD一样漆黑一片。完全不像介绍的那么酷。

图 1

不同的是它是多Tab的,在标题右侧有个下拉菜单,可以打开其他工具

图 2

如上图可以再打开一个CMD标签。

同时我们看到了设置选项,下面就开始倒腾一下相关的配置吧。

二、配置

点击【设置】,会打开一个名为settings.json的文件,大概如下面代码所示:

代码语言:javascript复制
 1 {
 2   "$schema": "https://aka.ms/terminal-profiles-schema",
 3 
 4   "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", //默认打开下面的profiles.List中的哪个profile,
 5 
 6   // 是否将选中内容自动复制到剪切板,默认是false,如果需要复制,选中内容中点击鼠标右键就实现了复制。如果是true,当选中的时候,就执行复制
 7   "copyOnSelect": false,
 8 
 9   // 复制的时候是否复制文本的样式,例如颜色、字体等
10   "copyFormatting": false,
11 
12   //profiles配置
13   "profiles": {
14     "defaults": {
15       // 设置通用配置
16     },
17     "list": [
18       {
19         // 针对 powershell.exe 这个profile进行配置.
20         "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
21         "name": "Windows PowerShell",
22         "commandline": "powershell.exe",
23         "hidden": false
24       },
25       {
26         // 针对 cmd.exe 这个profile进行配置.
27         "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
28         "name": "命令提示符",
29         "commandline": "cmd.exe",
30         "hidden": false
31       },
32       {
33         "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
34         "hidden": false,
35         "name": "Azure Cloud Shell",
36         "source": "Windows.Terminal.Azure"
37       }
38     ]
39   },
40 
41   //自定义的color schemes放在这里
42   "schemes": [
43 
44   ],
45 
46   // 在下面的集合中添加自定义的按键命令绑定
47   // 取消组合键,请将命令设置为“ unbound”。
48   "keybindings": [
49     // 在defaults.json中,复制和粘贴已经被绑定到了 Ctrl Shift C and Ctrl Shift V .
50     // 正两行又将它们绑定到了Ctrl C and Ctrl V.
51     {
52       "command": {
53         "action": "copy",
54         "singleLine": false
55       },
56       "keys": "ctrl c"
57     },
58     {
59       "command": "paste",
60       "keys": "ctrl v"
61     },
62 
63     // 按下 Ctrl Shift F 打开搜索窗口
64     {
65       "command": "find",
66       "keys": "ctrl shift f"
67     },
68 
69     // 按组合键 Alt Shift D 打开一个新的窗格,注意不是标签
70     //-“ split”:“ auto”使此窗格向着能提供最大面积的方向打开。
71     //-“ splitMode”:“ duplicate”新窗格使用当前窗格同样的配置文件。
72     {
73       "command": {
74         "action": "splitPane",
75         "split": "auto",
76         "splitMode": "duplicate"
77       },
78       "keys": "alt shift d"
79     }
80   ]
81 }

1. Profile设置:

【图2】所示的下拉菜单中,列出了"Windows PowerShell"、"命令提示符"和"Azure Cloud Shell"三个选项,为什么是这三项呢?是在profiles的List集合中配置的。可以看到现有三项正好与图2中的3项一一对应。

既然是可配置的,那么我们是否可以自定义添加呢?当然是可以的,下一小节我们来实验一下,本节先讲现有配置。 第4行的defaultProfile属性,设置了默认的Profile的guid,可以看出该guid正是Windows PowerShell的,所以我们打开Windows Terminal的时候,默认打开的是Windows PowerShell。

2. 复制设置:

第7行: 的"copyOnSelect"指定了是否将选中内容自动复制到剪切板,默认是false。这时如果需要复制,选中内容中点击鼠标右键就实现了复制。如果是true,当选中的时候,就执行复制。 第10行:“copyFormatting”指定了在复制的时候是否复制文本的样式,例如颜色、字体等。

3. 快捷键设置:

第48行,"keybindings"数组设置了如何通过一些组合键触发操作命令,例如复制、粘贴、查找等,代码中已经做了注释,就不一一说明了。 这里要说一下通过 "alt shift d"打开新窗格,这个不是新tab,是在原窗口中拆分出来的,如下图

图3

代码语言:javascript复制
新窗格会采用当前窗格的配置方案。可以按住 alt 键,然后使用箭头键在窗格之间移动焦点。
打开了这么多不知道怎么关闭?可以键入 ctrl shift w 来关闭焦点窗格。 如果只有一个窗格,ctrl shift w 将关闭该选项卡。与往常一样,关闭最后一个选项卡将关闭该窗口。     
如果想要打开新的tab可以使用"ctrl shift d"。  

3. 配色方案设置:

第42行,"schemes"提供了一个空的数组,用于放置自定义的配色方案。 具体介绍见下一节。

三、自定义样式

setting.json 只列出了一些方便我们配置的选项, 其实系统中已经为我们做了许多默认配置。按住alt键, 再去点击设置选项,这时不会打开setting.json, 而是会打开一个名为default.json的文件,这就是默认的配置文件。

代码如下,做了折叠有兴趣的可以看一看。

代码语言:javascript复制
// THIS IS AN AUTO-GENERATED FILE! Changes to this file will be ignored.
{
    "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

    // Launch Settings
    "initialCols": 120,
    "initialRows": 30,
    "launchMode": "default",

    // Selection
    "copyOnSelect": false,
    "copyFormatting": true,
    "wordDelimiters": " /\()"'-.,:;<>~!@#$%^&*| =[]{}~?u2502",

    // Tab UI
    "alwaysShowTabs": true,
    "showTabsInTitlebar": true,
    "showTerminalTitleInTitlebar": true,
    "tabWidthMode": "equal",

    // Miscellaneous
    "confirmCloseAllTabs": true,
    "startOnUserLogin":  false,
    "theme": "system",
    "rowsToScroll": "system",
    "snapToGridOnResize": true,

    "profiles":
    [
        {
            "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
            "name": "Windows PowerShell",
            "commandline": "powershell.exe",
            "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
            "colorScheme": "Campbell",
            "antialiasingMode": "grayscale",
            "closeOnExit": "graceful",
            "cursorShape": "bar",
            "fontFace": "Cascadia Mono",
            "fontSize": 12,
            "hidden": false,
            "historySize": 9001,
            "padding": "8, 8, 8, 8",
            "snapOnInput": true,
            "altGrAliasing": true,
            "startingDirectory": "%USERPROFILE%",
            "useAcrylic": false
        },
        {
            "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
            "name": "Command Prompt",
            "commandline": "cmd.exe",
            "icon": "ms-appx:///ProfileIcons/{0caa0dad-35be-5f56-a8ff-afceeeaa6101}.png",
            "colorScheme": "Campbell",
            "antialiasingMode": "grayscale",
            "closeOnExit": "graceful",
            "cursorShape": "bar",
            "fontFace": "Cascadia Mono",
            "fontSize": 12,
            "hidden": false,
            "historySize": 9001,
            "padding": "8, 8, 8, 8",
            "snapOnInput": true,
            "altGrAliasing": true,
            "startingDirectory": "%USERPROFILE%",
            "useAcrylic": false
        }
    ],
    "schemes":
    [
        // A profile can override the following color scheme values:
        //   - "foreground"
        //   - "background"
        //   - "cursorColor"
        {
            "name": "Campbell",
            "foreground": "#CCCCCC",
            "background": "#0C0C0C",
            "cursorColor": "#FFFFFF",
            "black": "#0C0C0C",
            "red": "#C50F1F",
            "green": "#13A10E",
            "yellow": "#C19C00",
            "blue": "#0037DA",
            "purple": "#881798",
            "cyan": "#3A96DD",
            "white": "#CCCCCC",
            "brightBlack": "#767676",
            "brightRed": "#E74856",
            "brightGreen": "#16C60C",
            "brightYellow": "#F9F1A5",
            "brightBlue": "#3B78FF",
            "brightPurple": "#B4009E",
            "brightCyan": "#61D6D6",
            "brightWhite": "#F2F2F2"
        },
        {
            "name": "Campbell Powershell",
            "foreground": "#CCCCCC",
            "background": "#012456",
            "cursorColor": "#FFFFFF",
            "black": "#0C0C0C",
            "red": "#C50F1F",
            "green": "#13A10E",
            "yellow": "#C19C00",
            "blue": "#0037DA",
            "purple": "#881798",
            "cyan": "#3A96DD",
            "white": "#CCCCCC",
            "brightBlack": "#767676",
            "brightRed": "#E74856",
            "brightGreen": "#16C60C",
            "brightYellow": "#F9F1A5",
            "brightBlue": "#3B78FF",
            "brightPurple": "#B4009E",
            "brightCyan": "#61D6D6",
            "brightWhite": "#F2F2F2"
        },
        {
            "name": "Vintage",
            "foreground": "#C0C0C0",
            "background": "#000000",
            "cursorColor": "#FFFFFF",
            "black": "#000000",
            "red": "#800000",
            "green": "#008000",
            "yellow": "#808000",
            "blue": "#000080",
            "purple": "#800080",
            "cyan": "#008080",
            "white": "#C0C0C0",
            "brightBlack": "#808080",
            "brightRed": "#FF0000",
            "brightGreen": "#00FF00",
            "brightYellow": "#FFFF00",
            "brightBlue": "#0000FF",
            "brightPurple": "#FF00FF",
            "brightCyan": "#00FFFF",
            "brightWhite": "#FFFFFF"
        },
        {
            "name": "One Half Dark",
            "foreground": "#DCDFE4",
            "background": "#282C34",
            "cursorColor": "#FFFFFF",
            "black": "#282C34",
            "red": "#E06C75",
            "green": "#98C379",
            "yellow": "#E5C07B",
            "blue": "#61AFEF",
            "purple": "#C678DD",
            "cyan": "#56B6C2",
            "white": "#DCDFE4",
            "brightBlack": "#5A6374",
            "brightRed": "#E06C75",
            "brightGreen": "#98C379",
            "brightYellow": "#E5C07B",
            "brightBlue": "#61AFEF",
            "brightPurple": "#C678DD",
            "brightCyan": "#56B6C2",
            "brightWhite": "#DCDFE4"
        },
        {
            "name": "One Half Light",
            "foreground": "#383A42",
            "background": "#FAFAFA",
            "cursorColor": "#4F525D",
            "black": "#383A42",
            "red": "#E45649",
            "green": "#50A14F",
            "yellow": "#C18301",
            "blue": "#0184BC",
            "purple": "#A626A4",
            "cyan": "#0997B3",
            "white": "#FAFAFA",
            "brightBlack": "#4F525D",
            "brightRed": "#DF6C75",
            "brightGreen": "#98C379",
            "brightYellow": "#E4C07A",
            "brightBlue": "#61AFEF",
            "brightPurple": "#C577DD",
            "brightCyan": "#56B5C1",
            "brightWhite": "#FFFFFF"
        },
        {
            "name": "Solarized Dark",
            "foreground": "#839496",
            "background": "#002B36",
            "cursorColor": "#FFFFFF",
            "black": "#073642",
            "red": "#DC322F",
            "green": "#859900",
            "yellow": "#B58900",
            "blue": "#268BD2",
            "purple": "#D33682",
            "cyan": "#2AA198",
            "white": "#EEE8D5",
            "brightBlack": "#002B36",
            "brightRed": "#CB4B16",
            "brightGreen": "#586E75",
            "brightYellow": "#657B83",
            "brightBlue": "#839496",
            "brightPurple": "#6C71C4",
            "brightCyan": "#93A1A1",
            "brightWhite": "#FDF6E3"
        },
        {
            "name": "Solarized Light",
            "foreground": "#657B83",
            "background": "#FDF6E3",
            "cursorColor": "#002B36",
            "black": "#073642",
            "red": "#DC322F",
            "green": "#859900",
            "yellow": "#B58900",
            "blue": "#268BD2",
            "purple": "#D33682",
            "cyan": "#2AA198",
            "white": "#EEE8D5",
            "brightBlack": "#002B36",
            "brightRed": "#CB4B16",
            "brightGreen": "#586E75",
            "brightYellow": "#657B83",
            "brightBlue": "#839496",
            "brightPurple": "#6C71C4",
            "brightCyan": "#93A1A1",
            "brightWhite": "#FDF6E3"
        },
        {
            "name": "Tango Dark",
            "foreground": "#D3D7CF",
            "background": "#000000",
            "cursorColor": "#FFFFFF",
            "black": "#000000",
            "red": "#CC0000",
            "green": "#4E9A06",
            "yellow": "#C4A000",
            "blue": "#3465A4",
            "purple": "#75507B",
            "cyan": "#06989A",
            "white": "#D3D7CF",
            "brightBlack": "#555753",
            "brightRed": "#EF2929",
            "brightGreen": "#8AE234",
            "brightYellow": "#FCE94F",
            "brightBlue": "#729FCF",
            "brightPurple": "#AD7FA8",
            "brightCyan": "#34E2E2",
            "brightWhite": "#EEEEEC"
        },
        {
            "name": "Tango Light",
            "foreground": "#555753",
            "background": "#FFFFFF",
            "cursorColor": "#000000",
            "black": "#000000",
            "red": "#CC0000",
            "green": "#4E9A06",
            "yellow": "#C4A000",
            "blue": "#3465A4",
            "purple": "#75507B",
            "cyan": "#06989A",
            "white": "#D3D7CF",
            "brightBlack": "#555753",
            "brightRed": "#EF2929",
            "brightGreen": "#8AE234",
            "brightYellow": "#FCE94F",
            "brightBlue": "#729FCF",
            "brightPurple": "#AD7FA8",
            "brightCyan": "#34E2E2",
            "brightWhite": "#EEEEEC"
        }
    ],
    "keybindings":
    [
        // Application-level Keys
        { "command": "closeWindow", "keys": "alt f4" },
        { "command": "toggleFullscreen", "keys": "alt enter" },
        { "command": "toggleFullscreen", "keys": "f11" },
        { "command": "openNewTabDropdown", "keys": "ctrl shift space" },
        { "command": "openSettings", "keys": "ctrl ," },
        { "command": { "action": "openSettings", "target": "defaultsFile" }, "keys": "ctrl alt ," },
        { "command": "find", "keys": "ctrl shift f" },

        // Tab Management
        // "command": "closeTab" is unbound by default.
        //   The closeTab command closes a tab without confirmation, even if it has multiple panes.
        { "command": "newTab", "keys": "ctrl shift t" },
        { "command": { "action": "newTab", "index": 0 }, "keys": "ctrl shift 1" },
        { "command": { "action": "newTab", "index": 1 }, "keys": "ctrl shift 2" },
        { "command": { "action": "newTab", "index": 2 }, "keys": "ctrl shift 3" },
        { "command": { "action": "newTab", "index": 3 }, "keys": "ctrl shift 4" },
        { "command": { "action": "newTab", "index": 4 }, "keys": "ctrl shift 5" },
        { "command": { "action": "newTab", "index": 5 }, "keys": "ctrl shift 6" },
        { "command": { "action": "newTab", "index": 6 }, "keys": "ctrl shift 7" },
        { "command": { "action": "newTab", "index": 7 }, "keys": "ctrl shift 8" },
        { "command": { "action": "newTab", "index": 8 }, "keys": "ctrl shift 9" },
        { "command": "duplicateTab", "keys": "ctrl shift d" },
        { "command": "nextTab", "keys": "ctrl tab" },
        { "command": "prevTab", "keys": "ctrl shift tab" },
        { "command": { "action": "switchToTab", "index": 0 }, "keys": "ctrl alt 1" },
        { "command": { "action": "switchToTab", "index": 1 }, "keys": "ctrl alt 2" },
        { "command": { "action": "switchToTab", "index": 2 }, "keys": "ctrl alt 3" },
        { "command": { "action": "switchToTab", "index": 3 }, "keys": "ctrl alt 4" },
        { "command": { "action": "switchToTab", "index": 4 }, "keys": "ctrl alt 5" },
        { "command": { "action": "switchToTab", "index": 5 }, "keys": "ctrl alt 6" },
        { "command": { "action": "switchToTab", "index": 6 }, "keys": "ctrl alt 7" },
        { "command": { "action": "switchToTab", "index": 7 }, "keys": "ctrl alt 8" },
        { "command": { "action": "switchToTab", "index": 8 }, "keys": "ctrl alt 9" },

        // Pane Management
        { "command": "closePane", "keys": "ctrl shift w" },
        { "command": { "action": "splitPane", "split": "horizontal" }, "keys": "alt shift -" },
        { "command": { "action": "splitPane", "split": "vertical" }, "keys": "alt shift plus" },
        { "command": { "action": "resizePane", "direction": "down" }, "keys": "alt shift down" },
        { "command": { "action": "resizePane", "direction": "left" }, "keys": "alt shift left" },
        { "command": { "action": "resizePane", "direction": "right" }, "keys": "alt shift right" },
        { "command": { "action": "resizePane", "direction": "up" }, "keys": "alt shift up" },
        { "command": { "action": "moveFocus", "direction": "down" }, "keys": "alt down" },
        { "command": { "action": "moveFocus", "direction": "left" }, "keys": "alt left" },
        { "command": { "action": "moveFocus", "direction": "right" }, "keys": "alt right" },
        { "command": { "action": "moveFocus", "direction": "up" }, "keys": "alt up" },

        // Clipboard Integration
        { "command": { "action": "copy", "singleLine": false }, "keys": "ctrl shift c" },
        { "command": { "action": "copy", "singleLine": false }, "keys": "ctrl insert" },
        { "command": "paste", "keys": "ctrl shift v" },
        { "command": "paste", "keys": "shift insert" },

        // Scrollback
        { "command": "scrollDown", "keys": "ctrl shift down" },
        { "command": "scrollDownPage", "keys": "ctrl shift pgdn" },
        { "command": "scrollUp", "keys": "ctrl shift up" },
        { "command": "scrollUpPage", "keys": "ctrl shift pgup" },

        // Visual Adjustments
        { "command": { "action": "adjustFontSize", "delta": 1 }, "keys": "ctrl =" },
        { "command": { "action": "adjustFontSize", "delta": -1 }, "keys": "ctrl -" },
        { "command": "resetFontSize", "keys": "ctrl 0" }
    ]
}

这个文件的结构和Setting.json文件的结构基本上是一样的,只不过系统提供了比较多的默认配置。 我们如果想写配置也可以用作参考。

1.系统提供的配色方案

可以看到在default.json"schemes"数组中提供了多种配色方案,例如"Campbell"、"Campbell Powershell"、"Vintage"等。

Campbell:

Solarized Light:

2. 配色方案的使用

使用方法就是在"profiles"节点中进行设置,例如Setting文件中做如下配置:

代码语言:javascript复制
  //profiles配置
  "profiles": {
    "defaults": {
      // 设置通用配置
      "colorScheme": "Solarized Light"
    },
    "list": [
      {
        // 针对 cmd.exe 这个profile进行配置.
        "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
        "name": "命令提示符",
        "commandline": "cmd.exe",
        "hidden": false,
        "colorScheme": "Tango Light"
      }
      // 。。。。。。。。。。。。。。
    ]
  },

可以看到代码中分别针对“default"和”cmd.exe“做了"colorScheme": "Solarized Light"和"colorScheme": "Tango Light"的配置,这使每个tab均默认采用名为"Solarized Light"的配色方案,cmd的tab除外,因为专门在cmd的节点中定义的优先级要高于”default“的默认配置。

这也使我们简单了解了"profiles"的配置方式。我们可以像配置配色方案一样配置其他属性。例如可以通过如下代码将默认的背景颜色设置成红色(挺丑的)。

代码语言:javascript复制
    "defaults": {
      // 设置通用配置
      "background": "#FF0000"
    },

当然这只是例子,关于这样的样式设置还是通过建议自定义colorScheme来实现(见下节)。不只是样式,还可以设置光标、键盘、tab的标题等,这里就不一一介绍了,详见本文底部的官方链接。

3. 自定义colorScheme

本节我们自定义一个colorScheme。

代码语言:javascript复制
  //自定义的color schemes放在这里
  "schemes": [
    {
      "name": "FlyLolo Test",
      "cursorColor": "#5F04B4",
      "background": "#FFFFFF",
      "selectionBackground": "#D8F781",
      "black": "#3C5712",
      "blue": "#17b2ff",
      "brightBlack": "#749B36",
      "brightBlue": "#27B2F6",
      "brightCyan": "#13A8C0",
      "brightGreen": "#89AF50",
      "brightPurple": "#F2A20A",
      "brightRed": "#F49B36",
      "brightWhite": "#741274",
      "brightYellow": "#991070",
      "cyan": "#3C96A6",
      "foreground": "#6A0888",
      "green": "#6AAE08",
      "purple": "#991070",
      "red": "#8D0C0C",
      "white": "#6E386E",
      "yellow": "#991070"
    }
  ]

设置了Windows PowerShell的背景和毛玻璃效果:

代码语言:javascript复制
      {
        // 针对 powershell.exe 这个profile进行配置.
        "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
        "name": "Windows PowerShell",
        "commandline": "powershell.exe",
        "hidden": false,
        "colorScheme": "FlyLolo Test",
        "useAcrylic": true,
        "acrylicOpacity": 0.7,
        "backgroundImage": "D://1.png",
        "backgroundImageStretchMode": "none",
        "backgroundImageAlignment": "bottomRight",
        "backgroundImageOpacity": "0.6"
      },

最终效果如下图。 背景图个人建议不要弄全屏的炫图,好看,但打字时就不实用了。

四、连接CentOS

之前连接CentOS习惯使用PuTTY,现在是否可以通过Windows Terminal来实现呢? 在第二节我们知道可以在profiles的List集合中配置新的下拉选项功能。

在List中添加如下代码:

代码语言:javascript复制
{
    "guid": "{439227e1-65b3-4a60-a518-74afeca0c7c0}",
    "hidden": false,
    "name": "CentOS",
    "commandline": "ssh 用户名@域名orIP -p 端口号",
    "icon": "ms-appdata:///Local/Centos.png"
}

icon 需要自己设置,VS右键点击settings.json标签,找到该文件所在的位置,

网上找了个centOS的图标,

, 放在settings.json的相同目录下,配置中就可以通过 "icon": "ms-appdata:///Local/Centos.png" 这样的路径设置了。

这样就配置完了,保存settings.json文件,无需重新启动Windows Terminal就可以在下拉选项中找到CentOS了,点击该选项,会出现如下窗口,输入密码登录成功。

参考地址:https://docs.microsoft.com/zh-cn/windows/terminal/

0 人点赞