玩转 Windows Terminal

今天给大家分享一下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的文件,大概如下面代码所示:

 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

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

3. 配色方案设置:

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

三、自定义样式

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

代码如下,有兴趣的可以看一看。

// 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文件中做如下配置:

  //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"的配置方式。我们可以像配置配色方案一样配置其他属性。例如可以通过如下代码将默认的背景颜色设置成红色(挺丑的)。

    "defaults": {
      // 设置通用配置
      "background": "#ff0000"
    },

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

3. 自定义colorscheme

本节我们自定义一个colorscheme。

  //自定义的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的背景和毛玻璃效果:

      {
        // 针对 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中添加如下代码:

{
    "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/

本文链接:https://2i3i.com/windowsterminal.html ,转载请注明来源地址。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇