1. 程式人生 > >玩轉 Windows Terminal

玩轉 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" }
    ]
}
View Code

 

這個檔案的結構和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/