文本最后编辑时间:2020-06-13

前言

在这之前,我一直使用的都是cmder,但是这东西说真的,有点难用。不过好在微软推出了Windows Terminal,经过我的实测,感觉还是不错的。毕竟颜值才是第一生产力。

image-20200604173746487

开始操作

  1. 下载Windows Terminal

    下载很简单,在微软商店下载即可。

    image-20200604172452842

  2. 安装一个字体,比较推荐的是Fira Code

    单击此处从 GitHub 下载。

  3. 安装新款 Powershell Core

    首先声明,我们这儿用的 Powershell 与 Windows 自带的 Powershell 是完全不同的两个东西,除了功能相似和名字相同,两者内在已经天差地别。

    Powershell Core 是什么呢?这是伟大的 .Net Core 跨平台战略的一个重要组成部分,微软设想,要让强大的 .Net 在所有平台上通用,让这么强大的 Powershell 在所有平台上都能用,古老的 bash 可以退休了!

    下载地址(可下载最新版本,不一定要下载图中所示)

    image-20200604172952110

  4. 安装Powershell插件

    • PSReadLine

      PSReadLine 是一个由微软发布的用于 PowerShell 的行读取实现,提供了以下功能:

      • 语法着色
      • 简单语法错误通知
      • 良好的多行体验
      • 可自定义的键绑定
      • Cmd和Emacs模式
      • 许多配置选项
      • Bash 样式的补全
      • Bash/zsh 样式的交互式历史记录搜索
      • Emacs yank/kill ring
      • 基于 PowerShell Token 的单词移动和删除
      • 撤销/重做
      • 自动保存历史记录,包括在实时会话中共享历史记录
      • 菜单补全、Intellisense

      GitHub地址:https://github.com/PowerShell/PSReadLine

    • oh-my-posh&posh-git

      类似于 oh-my-zsh,oh-my-posh 为 PowerShell 提供了很多自定义主题和配色,而 posh-git 为 PowerShell 提供了 git 状态显示和命令补全等。

      GitHub地址:https://github.com/JanDeDobbeleer/oh-my-posh

    看完上边的三个插件介绍,我们来安装这三个插件。

    用管理员模式打开刚装好的新版 powershell,逐行输入命令。

    # 1. 安装 PSReadline 包,该插件可以让命令行很好用,类似 zsh
    Install-Module -Name PSReadLine -AllowPrerelease -Force
    
    # 2. 安装 posh-git 包,让你的 git 更好用
    Install-Module posh-git -Scope CurrentUser
    
    # 3. 安装 oh-my-posh 包,让你的命令行更酷炫、优雅
    Install-Module oh-my-posh -Scope CurrentUser

    image-20200604174115515

    image-20200604174201074

    安装完成

    一定要用刚刚安装的新版的powershell,安装过程可能过于慢,请耐心等待。

    后面两个包的来源可能不受系统信任,不用管它,如果让你选择是否信任,直接输入 Y 即可。

  5. 编辑$Profile文件。

    这个文件类似于 ~/.zshrc,会在 PowerShell 启动的时候自动执行,因此我们在这个文件中加载我们所需的模块。

    • Windows

      notepad.exe $Profile

    • Linux

      nano $Profile

    然后在这个文件中添加如下内容:

    Import-Module posh-git # 引入 posh-git
    Import-Module oh-my-posh # 引入 oh-my-posh
    
    Set-Theme Paradox # 设置主题为 Paradox
    
    Set-PSReadlineKeyHandler -Key Tab -Function Complete # 设置 Tab 键补全
    Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function MenuComplete # 设置 Ctrl+d 为菜单补全和 Intellisense
    Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置 Ctrl+z 为撤销
    Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向上键为后向搜索历史记录
    Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward # 设置向下键为前向搜索历史纪录

    oh-my-posh提供了 10 款 漂亮 的主题供我们选择。 Agnoster,Avit,Darkblood,Fish,Honukai,Paradox,PowerLine,robbyrussell,Sorin,tehrob

  6. 接下来打开Windows Terminal

    Windows Terminal默认为旧版本的powershell,我们需要把新版本作为默认。

    image-20200604175634559

    修改的内容有三个,如果你不想自己改可以直接将我的粘贴到配置文件(需要注意的是:你的commandline,如果按照默认路径安装(预览版),那么可以直接复制下边。正式版或者更换了安装位置,那么请手动更改位置。其他信息试自己能力进行修改)。

    // This file was initially generated by Windows Terminal 1.0.1401.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": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    
        // 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.
            },
            "list": [
                {
                    // Make changes here to the powershell.exe profile.
                    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                    "name": "Windows PowerShell",
                    "commandline": "powershell.exe",
                    "hidden": false
                },
                {
                    // Make changes here to the cmd.exe profile.
                    "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                    "name": "命令提示符",
                    "commandline": "cmd.exe",
                    "hidden": false
                },
                {
                    "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                    "hidden": false,
                    "name": "Azure Cloud Shell",
                    "source": "Windows.Terminal.Azure"
                },
                {
                    "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
                    "hidden": false,
                    "name": "PowerShell",
                    "source": "Windows.Terminal.PowershellCore"
                },
                {
                    // Powershell 7 配置
                    "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
                    "hidden": false,
                    "name": "pwsh",
                    // 注意:一定要写上 -nologo,否则开启 powershll 会有一段话输出,很讨厌!
                    // 如果下载的是正是版,那么路径为:C:/Program Files/PowerShell/7/pwsh.exe -nologo
                    "commandline": "C:/Program Files/PowerShell/7-preview/pwsh.exe -nologo",
                    "source": "Windows.Terminal.PowershellCore",
                    // 启动菜单一定要设置为 <.>,否则后面重要的一步将会无效!
                    "startingDirectory": ".",
                    // 字体
                    "fontFace": "Fira Code",
                    "fontSize": 11,
                    "historySize": 9001,
                    "padding": "5, 5, 20, 25",
                    "snapOnInput": true,
                    "useAcrylic": false,
                    // 颜色
                    "colorScheme": "idleToes"
                }    ]
        },
    
        // Add custom color schemes to this array.
        // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
        "schemes": [
            {
                "name": "Zenburn",
                "black": "#4d4d4d",
                "red": "#705050",
                "green": "#60b48a",
                "yellow": "#f0dfaf",
                "blue": "#506070",
                "purple": "#dc8cc3",
                "cyan": "#8cd0d3",
                "white": "#dcdccc",
                "brightBlack": "#709080",
                "brightRed": "#dca3a3",
                "brightGreen": "#c3bf9f",
                "brightYellow": "#e0cf9f",
                "brightBlue": "#94bff3",
                "brightPurple": "#ec93d3",
                "brightCyan": "#93e0e3",
                "brightWhite": "#ffffff",
                "background": "#3f3f3f",
                "foreground": "#dcdccc"
            },
            {
                "name": "PencilDark",
                "black": "#212121",
                "red": "#c30771",
                "green": "#10a778",
                "yellow": "#a89c14",
                "blue": "#008ec4",
                "purple": "#523c79",
                "cyan": "#20a5ba",
                "white": "#d9d9d9",
                "brightBlack": "#424242",
                "brightRed": "#fb007a",
                "brightGreen": "#5fd7af",
                "brightYellow": "#f3e430",
                "brightBlue": "#20bbfc",
                "brightPurple": "#6855de",
                "brightCyan": "#4fb8cc",
                "brightWhite": "#f1f1f1",
                "background": "#212121",
                "foreground": "#f1f1f1"
            },
            {
                "name": "Slate",
                "black": "#222222",
                "red": "#e2a8bf",
                "green": "#81d778",
                "yellow": "#c4c9c0",
                "blue": "#264b49",
                "purple": "#a481d3",
                "cyan": "#15ab9c",
                "white": "#02c5e0",
                "brightBlack": "#ffffff",
                "brightRed": "#ffcdd9",
                "brightGreen": "#beffa8",
                "brightYellow": "#d0ccca",
                "brightBlue": "#7ab0d2",
                "brightPurple": "#c5a7d9",
                "brightCyan": "#8cdfe0",
                "brightWhite": "#e0e0e0",
                "background": "#222222",
                "foreground": "#35b1d2"
            },
            {
                "name": "OneHalfDark",
                "black": "#282c34",
                "red": "#e06c75",
                "green": "#98c379",
                "yellow": "#e5c07b",
                "blue": "#61afef",
                "purple": "#c678dd",
                "cyan": "#56b6c2",
                "white": "#dcdfe4",
                "brightBlack": "#282c34",
                "brightRed": "#e06c75",
                "brightGreen": "#98c379",
                "brightYellow": "#e5c07b",
                "brightBlue": "#61afef",
                "brightPurple": "#c678dd",
                "brightCyan": "#56b6c2",
                "brightWhite": "#dcdfe4",
                "background": "#282c34",
                "foreground": "#dcdfe4"
            },
            {
                "name": "OneHalfLight",
                "black": "#383a42",
                "red": "#e45649",
                "green": "#50a14f",
                "yellow": "#c18401",
                "blue": "#0184bc",
                "purple": "#a626a4",
                "cyan": "#0997b3",
                "white": "#fafafa",
                "brightBlack": "#4f525e",
                "brightRed": "#e06c75",
                "brightGreen": "#98c379",
                "brightYellow": "#e5c07b",
                "brightBlue": "#61afef",
                "brightPurple": "#c678dd",
                "brightCyan": "#56b6c2",
                "brightWhite": "#ffffff",
                "background": "#fafafa",
                "foreground": "#383a42"
            },
            {
                "name": "Material",
                "black": "#212121",
                "red": "#b7141f",
                "green": "#457b24",
                "yellow": "#f6981e",
                "blue": "#134eb2",
                "purple": "#560088",
                "cyan": "#0e717c",
                "white": "#efefef",
                "brightBlack": "#424242",
                "brightRed": "#e83b3f",
                "brightGreen": "#7aba3a",
                "brightYellow": "#ffea2e",
                "brightBlue": "#54a4f3",
                "brightPurple": "#aa4dbc",
                "brightCyan": "#26bbd1",
                "brightWhite": "#d9d9d9",
                "background": "#eaeaea",
                "foreground": "#232322"
            },
            {
                "name": "MaterialDark",
                "black": "#212121",
                "red": "#b7141f",
                "green": "#457b24",
                "yellow": "#f6981e",
                "blue": "#134eb2",
                "purple": "#560088",
                "cyan": "#0e717c",
                "white": "#efefef",
                "brightBlack": "#424242",
                "brightRed": "#e83b3f",
                "brightGreen": "#7aba3a",
                "brightYellow": "#ffea2e",
                "brightBlue": "#54a4f3",
                "brightPurple": "#aa4dbc",
                "brightCyan": "#26bbd1",
                "brightWhite": "#d9d9d9",
                "background": "#232322",
                "foreground": "#e5e5e5"
            },
            {
                "name": "Homebrew",
                "black": "#000000",
                "red": "#FC5275",
                "green": "#00a600",
                "yellow": "#999900",
                "blue": "#6666e9",
                "purple": "#b200b2",
                "cyan": "#00a6b2",
                "white": "#bfbfbf",
                "brightBlack": "#666666",
                "brightRed": "#e50000",
                "brightGreen": "#00d900",
                "brightYellow": "#e5e500",
                "brightBlue": "#0000ff",
                "brightPurple": "#e500e5",
                "brightCyan": "#00e5e5",
                "brightWhite": "#e5e5e5",
                "background": "#283033",
                "foreground": "#00ff00"
            },
            {
                "name": "Breeze",
                "black": "#31363b",
                "red": "#ed1515",
                "green": "#11d116",
                "yellow": "#f67400",
                "blue": "#1d99f3",
                "purple": "#9b59b6",
                "cyan": "#1abc9c",
                "white": "#eff0f1",
                "brightBlack": "#7f8c8d",
                "brightRed": "#c0392b",
                "brightGreen": "#1cdc9a",
                "brightYellow": "#fdbc4b",
                "brightBlue": "#3daee9",
                "brightPurple": "#8e44ad",
                "brightCyan": "#16a085",
                "brightWhite": "#fcfcfc",
                "background": "#31363b",
                "foreground": "#eff0f1"
            },
            {
                "name": "idleToes",
                "black": "#323232",
                "red": "#d25252",
                "green": "#7fe173",
                "yellow": "#ffc66d",
                "blue": "#4099ff",
                "purple": "#f680ff",
                "cyan": "#bed6ff",
                "white": "#eeeeec",
                "brightBlack": "#535353",
                "brightRed": "#f07070",
                "brightGreen": "#9dff91",
                "brightYellow": "#ffe48b",
                "brightBlue": "#5eb7f7",
                "brightPurple": "#ff9dff",
                "brightCyan": "#dcf4ff",
                "brightWhite": "#ffffff",
                "background": "#323232",
                "foreground": "#00ff00"
            },
            {
                "name": "Firewatch",
                "black": "#585f6d",
                "red": "#d95360",
                "green": "#5ab977",
                "yellow": "#dfb563",
                "blue": "#4d89c4",
                "purple": "#d55119",
                "cyan": "#44a8b6",
                "white": "#e6e5ff",
                "brightBlack": "#585f6d",
                "brightRed": "#d95360",
                "brightGreen": "#5ab977",
                "brightYellow": "#dfb563",
                "brightBlue": "#4c89c5",
                "brightPurple": "#d55119",
                "brightCyan": "#44a8b6",
                "brightWhite": "#e6e5ff",
                "background": "#1e2027",
                "foreground": "#9ba2b2"
            },
            {
                "name": "Github",
                "black": "#3e3e3e",
                "red": "#970b16",
                "green": "#07962a",
                "yellow": "#f8eec7",
                "blue": "#003e8a",
                "purple": "#e94691",
                "cyan": "#89d1ec",
                "white": "#ffffff",
                "brightBlack": "#666666",
                "brightRed": "#de0000",
                "brightGreen": "#87d5a2",
                "brightYellow": "#f1d007",
                "brightBlue": "#2e6cba",
                "brightPurple": "#ffa29f",
                "brightCyan": "#1cfafe",
                "brightWhite": "#ffffff",
                "background": "#f4f4f4",
                "foreground": "#3e3e3e"
            }
        ],
    
        // 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"
            }
        ]
    }
    
    
    • defaultProfile

      defaultProfile改为新的powershell。

      "defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    • list

      在list中新增一个配置,也就是我们的新版powershell。

      {
          // Powershell 7.1.0-preview.2 配置
          "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
          "hidden": false,
          "name": "pwsh",
          // 注意:一定要写上 -nologo,否则开启 powershll 会有一段话输出,很讨厌!
          "commandline": "C:/Program Files/PowerShell/7-preview/pwsh.exe -nologo",
          "source": "Windows.Terminal.PowershellCore",
          // 启动菜单一定要设置为 <.>,否则后面重要的一步将会无效!
          "startingDirectory": ".",
          // 字体
          "fontFace": "Fira Code",
          "fontSize": 11,
          "historySize": 9001,
          "padding": "5, 5, 20, 25",
          "snapOnInput": true,
          "useAcrylic": false,
          // 颜色
          "colorScheme": "idleToes"
      },
    • colorScheme

      这个也就是配色。

      {
          "name": "idleToes",
          "black": "#323232",
          "red": "#d25252",
          "green": "#7fe173",
          "yellow": "#ffc66d",
          "blue": "#4099ff",
          "purple": "#f680ff",
          "cyan": "#bed6ff",
          "white": "#eeeeec",
          "brightBlack": "#535353",
          "brightRed": "#f07070",
          "brightGreen": "#9dff91",
          "brightYellow": "#ffe48b",
          "brightBlue": "#5eb7f7",
          "brightPurple": "#ff9dff",
          "brightCyan": "#dcf4ff",
          "brightWhite": "#ffffff",
          "background": "#323232",
          "foreground": "#00ff00"
      },
      
  7. 配置鼠标右键

    完成上面的操作后,重新打开Windows Terminal就会发现模样已经变化了。

    image-20200604180232963

    接下来就是配置鼠标右键了,添加鼠标右键的方式有两种,一种是用脚本,另一种就是改注册表。我本人比较推荐直接使用脚本,毕竟修改注册表有风险。。。

    1. 直接使用脚本

      脚本下载地址:https://github.com/lextm/windowsterminal-shell/

      在 .\install.ps1 后面接参数可以定制你要的效果,该仓库提供了三个效果。如果不加参数,则使用默认效果。该仓库有个好处,那就是可以添加一个右键【以管理员身份在此打开】的选项,简便了不少。

      再次使用管理员模式打开PowerShell7,然后切换到放install.ps1的目录进行安装。我这里使用默认安装。如果你想用其他模式,也可以使用其他模式,看自己心情即可。其他模式请参考项目文档。

      image-20200604180936562

      image-20200604180956425

    2. 修改注册表。

      自行百度

  8. 接下来我们到对应目录点击pwsh即可呼唤出新版的powershell。

问题说明

  1. 实验环境

    VMware下的Windows2004版本

  2. 本文中所使用的文件打包

    https://tzk.lanzous.com/b06lz9sfa
    密码:dkqs