Windows Terminal 使用

前言

Windows 終端是一個面向命令行工具和 shell(如命令提示符、PowerShell 和適用于 Linux 的 Windows 子系統 (WSL))用戶的新式終端應用程序。 它的主要功能包括多個選項卡、窗格、Unicode 和 UTF-8 字符支持、GPU 加速文本呈現引擎,還可以用于創(chuàng)建你自己的主題并自定義文本、顏色、背景和快捷鍵綁定。

1. 安裝

從 windows store中安裝,搜索terminal 有兩個,一個microsoft terminal一個windows terminal preview , 選擇后者。
如果你無法訪問 Microsoft Store,GitHub 發(fā)布頁上發(fā)布有內部版本。 如果從 GitHub 安裝,終端將不會自動更新為新版本。

2. 配置參考

可參考我的配置:

// This file was initially generated by Windows Terminal Preview 1.2.2022.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.
      "fontFace": "Cascadia Code",
      "fontSize": 10,
      "useAcrylic": true, //使用背景透明
      "acrylicOpacity": 0.8, //背景透明度
      "launchMode": "default", //啟動大小 default , maximized , fullscreen
      "initialPosition": "800,#" //啟動位置

    },
    "list": [
      {
        // Make changes here to the cmd.exe profile.
        "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
        "name": "命令提示符",
        "commandline": "cmd.exe",
        "hidden": false,
        "startingDirectory": null //默認值:"%USERPROFILE%",通過地址欄啟動,輸入 wt 即可打開Windows terminal (類似cmd)
      },
      {
        // Make changes here to the powershell.exe profile.
        "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
        "name": "Windows PowerShell",
        "commandline": "powershell.exe",
        "startingDirectory": null,
        "fontFace": "Cascadia Code PL",
        "hidden": false
      },
      {
        "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
        "hidden": true,
        "name": "Azure Cloud Shell",
        "source": "Windows.Terminal.Azure"
      },
      {
        "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b7}",
        "hidden": false,
        "name": "git bash",
        "icon": "D:\\Program Files\\Git\\favicon.ico",
        "commandline": "D:\\Program Files\\Git\\bin\\bash.exe"
      }
    ]
  },

  // Add custom color schemes to this array.
  // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
  "schemes": [],

  // 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"
    }
  ]
}

效果圖:


windows terminal

3. 添加git-bash 命令

在list中添加一個對象

 {
        "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b7}",
        "hidden": false,
        "name": "git bash",
        "icon": "D:\\Program Files\\Git\\favicon.ico",
        "commandline": "D:\\Program Files\\Git\\bin\\bash.exe"
}

其中guid可用工具生成, icon從官網下載(打開git-bash官網后,在控制臺網絡中可找到favicon.ico),放到適當位置,引用其位置即可。
commandline填寫已安裝的git下的bash.exe路徑

4. 在powershell中使用git并帶有命令行自動補全和漂亮的文本[可選]

[可選內容,喜歡折騰的用戶隨意]
參考:https://docs.microsoft.com/zh-cn/windows/terminal/tutorials/powerline-setup

  • 下載字體并安裝:Cascadia Mono PL , 字體下載:CascadiaPL.ttf, 下載后直接雙擊字體即可安裝。

  • 使用 PowerShell,安裝 Posh-Git 和 Oh-My-Posh:

Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser

注意此處安裝可能會失敗,需要kexuejishu上網

  • 使用powershell 啟動自動加載配置文件
echo $profile
Test-Path $profile
New-Item -Path $profile -Type File –Force

使用echo $profile檢查.ps1文件是否存在,Test-Path $profile進一步測試是否存在,若不存在則用命令新建一個New-Item -Path $profile -Type File –Force

使用文本編輯器打開該 PowerShell 配置文件,該腳本在每次啟動 PowerShell 時運行。 詳細了解 PowerShell 配置文件。
在 PowerShell 配置文件中,將以下內容添加到文件的末尾:

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

現在,每個新實例啟動時都會導入 Posh-Git 和 Oh-My-Posh,然后從 Oh-My-Posh 設置 Paradox 主題。 Oh-My-Posh 附帶了若干內置主題。
效果如下:

帶有命令補全和漂亮窗體的powershell git

  • 注意: 如果啟動powershell時報錯如下:
. : 無法加載文件 C:\Users\wweim\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1,因為在此系統上禁止運行腳
本。有關詳細信息,請參閱 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 3
+ . 'C:\Users\wweim\Documents\WindowsPowerShell\Microsoft.PowerShell_pr ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

則可根據https:/go.microsoft.com/fwlink/?LinkID=135170中的提示進行設置, 簡單的方式可這樣操作:
使用管理員權限打開powershell

Get-ExecutionPolicy
# 如果是Restricted, 則代表不能使用本地加載ps1
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# 設置本地用戶執(zhí)行策略為 RemoteSigned

完成, 重新打開terminal后進入powershell可發(fā)現已經可以加載ps1文件

5. 將本地cmd命令默認使用windows terminal打開

快捷鍵win+r后在打開的運行窗口輸入cmd打開cmd窗口后在窗口頭部右鍵選擇屬性打開屬性對話框,然后選擇終端,在默認終端應用程序中選擇Windows Terminal即可,下次打開cmd時自動使用Windows Terminal終端打開

cmd選擇Windows Terminal為默認終端

或者在打開的Windows Terminal終端中選擇設置
Windows Terminal終端選擇

設置-啟動-默認終端應用程序

6. Windows Terminal啟動快捷方式

方法1: win+s中輸入wt即可啟動Windows Terminal
方法2: win+r中輸入wt即可啟動Windows Terminal
方法3: 在文件夾路徑上輸入wt,此命令根目錄為默認目錄
方法4: (推薦)在文件夾路徑上輸入'cmd',此時命令根目錄為當前目錄
方法5: (推薦)大多數編輯器(如:vs,vscode,webstorm,idea等等)都自帶終端,打開終端后直接輸入start cmd也可打開終端, 而且終端根目錄也是當前自帶終端的根目錄. 方便執(zhí)行帶有當前路徑的命令.

總結

本文從安裝到設置,再到默認切換終端,最后快捷的啟動方式,簡單的帶你進入了Windows Terminal的世界, 從此一款高效、美觀、現代的命令行工具伴隨著我們的日常開發(fā)??

參考:windows terminal官方文檔

本文作者:wwmin
微信公眾號: DotNet技術說
本文鏈接:http://m.itdecent.cn/p/faeb78aed400
版權聲明:轉載請注明出處!
聲援博主:如果您覺得文章對您有幫助,關注點贊, 您的鼓勵是博主的最大動力!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容