前言

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

image-20210822175133013

开始操作

  1. 下载Windows Terminal

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

    image-20210822153753915

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

    单击此处从 GitHub 下载。

  3. 安装新款 Powershell Core

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

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

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

  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-20210822154316987

    一定要用刚刚安装的新版的powershell(建议使用管理员模式运行),安装过程可能过于慢,请耐心等待。

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

  5. 编辑$Profile文件。

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

    • Windows

      notepad.exe $Profile

    • Linux

      nano $Profile

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

    #------------------------------- Import Modules BEGIN -------------------------------
    # 引入 posh-git
    Import-Module posh-git
    
    # 引入 oh-my-posh
    Import-Module oh-my-posh
    
    # 设置 PowerShell 主题
    Set-PoshPrompt paradox
    #------------------------------- Import Modules END   -------------------------------
    
    #-------------------------------  Set Hot-keys BEGIN  -------------------------------
    # 设置 Tab 键补全
    # Set-PSReadlineKeyHandler -Key Tab -Function Complete
    
    # 设置 Ctrl+d 为菜单补全和 Intellisense
    Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete
    
    # 设置 Ctrl+d 为退出 PowerShell
    Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit
    
    # 设置 Ctrl+z 为撤销
    Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
    
    # 设置向上键为后向搜索历史记录
    Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
    
    # 设置向下键为前向搜索历史纪录
    Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
    #-------------------------------  Set Hot-keys END    -------------------------------
    
    #-------------------------------   Set Alias Begin    -------------------------------
    # 1. 编译函数 make
    function MakeThings {
        nmake.exe $args -nologo
    }
    Set-Alias -Name make -Value MakeThings
    
    # 2. 更新系统 os-update
    Set-Alias -Name os-update -Value Update-Packages
    
    # 3. 查看目录 ls & ll
    function ListDirectory {
        (Get-ChildItem).Name
        Write-Host("")
    }
    Set-Alias -Name ls -Value ListDirectory
    Set-Alias -Name ll -Value Get-ChildItem
    #-------------------------------    Set Alias END     -------------------------------
    

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

  6. 接下来打开Windows Terminal

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

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

    {
        "$schema": "https://aka.ms/terminal-profiles-schema",
        // Add custom actions and keybindings to this array.
        // To unbind a key combination from your defaults.json, set the command to "unbound".
        // To learn more about actions and keybindings, visit https://aka.ms/terminal-keybindings
        "actions": 
        [
            // 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"
            }
        ],
        "copyFormatting": "none",
        "copyOnSelect": false,
        "defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
        "profiles": 
        {
            "defaults": {},
            "list": 
            [
                {
                    "acrylicOpacity": 0.5,
                    "backgroundImage": null,
                    "colorScheme": "idleToes",
                    "commandline": "C:/Program Files/PowerShell/7/pwsh.exe -nologo",
                    "fontFace": "Fira Code",
                    "fontSize": 11,
                    "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
                    "hidden": false,
                    "historySize": 9001,
                    "name": "pwsh",
                    "padding": "5, 5, 20, 25",
                    "snapOnInput": true,
                    "source": "Windows.Terminal.PowershellCore",
                    "startingDirectory": ".",
                    "useAcrylic": true
                },
                {
                    "commandline": "powershell.exe",
                    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                    "hidden": true,
                    "name": "Windows PowerShell"
                },
                {
                    "commandline": "cmd.exe",
                    "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                    "hidden": false,
                    "name": "\u547d\u4ee4\u63d0\u793a\u7b26"
                },
                {
                    "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                    "hidden": true,
                    "name": "Azure Cloud Shell",
                    "source": "Windows.Terminal.Azure"
                }
            ]
        },
        "schemes": 
        [
            {
                "background": "#0C0C0C",
                "black": "#0C0C0C",
                "blue": "#0037DA",
                "brightBlack": "#767676",
                "brightBlue": "#3B78FF",
                "brightCyan": "#61D6D6",
                "brightGreen": "#16C60C",
                "brightPurple": "#B4009E",
                "brightRed": "#E74856",
                "brightWhite": "#F2F2F2",
                "brightYellow": "#F9F1A5",
                "cursorColor": "#FFFFFF",
                "cyan": "#3A96DD",
                "foreground": "#CCCCCC",
                "green": "#13A10E",
                "name": "Campbell",
                "purple": "#881798",
                "red": "#C50F1F",
                "selectionBackground": "#FFFFFF",
                "white": "#CCCCCC",
                "yellow": "#C19C00"
            },
            {
                "background": "#012456",
                "black": "#0C0C0C",
                "blue": "#0037DA",
                "brightBlack": "#767676",
                "brightBlue": "#3B78FF",
                "brightCyan": "#61D6D6",
                "brightGreen": "#16C60C",
                "brightPurple": "#B4009E",
                "brightRed": "#E74856",
                "brightWhite": "#F2F2F2",
                "brightYellow": "#F9F1A5",
                "cursorColor": "#FFFFFF",
                "cyan": "#3A96DD",
                "foreground": "#CCCCCC",
                "green": "#13A10E",
                "name": "Campbell Powershell",
                "purple": "#881798",
                "red": "#C50F1F",
                "selectionBackground": "#FFFFFF",
                "white": "#CCCCCC",
                "yellow": "#C19C00"
            },
            {
                "background": "#283033",
                "black": "#000000",
                "blue": "#6666E9",
                "brightBlack": "#666666",
                "brightBlue": "#0000FF",
                "brightCyan": "#00E5E5",
                "brightGreen": "#00D900",
                "brightPurple": "#E500E5",
                "brightRed": "#E50000",
                "brightWhite": "#E5E5E5",
                "brightYellow": "#E5E500",
                "cursorColor": "#FFFFFF",
                "cyan": "#00A6B2",
                "foreground": "#00FF00",
                "green": "#00A600",
                "name": "Homebrew",
                "purple": "#B200B2",
                "red": "#FC5275",
                "selectionBackground": "#FFFFFF",
                "white": "#BFBFBF",
                "yellow": "#999900"
            },
            {
                "background": "#282C34",
                "black": "#282C34",
                "blue": "#61AFEF",
                "brightBlack": "#5A6374",
                "brightBlue": "#61AFEF",
                "brightCyan": "#56B6C2",
                "brightGreen": "#98C379",
                "brightPurple": "#C678DD",
                "brightRed": "#E06C75",
                "brightWhite": "#DCDFE4",
                "brightYellow": "#E5C07B",
                "cursorColor": "#FFFFFF",
                "cyan": "#56B6C2",
                "foreground": "#DCDFE4",
                "green": "#98C379",
                "name": "One Half Dark",
                "purple": "#C678DD",
                "red": "#E06C75",
                "selectionBackground": "#FFFFFF",
                "white": "#DCDFE4",
                "yellow": "#E5C07B"
            },
            {
                "background": "#FAFAFA",
                "black": "#383A42",
                "blue": "#0184BC",
                "brightBlack": "#4F525D",
                "brightBlue": "#61AFEF",
                "brightCyan": "#56B5C1",
                "brightGreen": "#98C379",
                "brightPurple": "#C577DD",
                "brightRed": "#DF6C75",
                "brightWhite": "#FFFFFF",
                "brightYellow": "#E4C07A",
                "cursorColor": "#4F525D",
                "cyan": "#0997B3",
                "foreground": "#383A42",
                "green": "#50A14F",
                "name": "One Half Light",
                "purple": "#A626A4",
                "red": "#E45649",
                "selectionBackground": "#FFFFFF",
                "white": "#FAFAFA",
                "yellow": "#C18301"
            },
            {
                "background": "#002B36",
                "black": "#002B36",
                "blue": "#268BD2",
                "brightBlack": "#073642",
                "brightBlue": "#839496",
                "brightCyan": "#93A1A1",
                "brightGreen": "#586E75",
                "brightPurple": "#6C71C4",
                "brightRed": "#CB4B16",
                "brightWhite": "#FDF6E3",
                "brightYellow": "#657B83",
                "cursorColor": "#FFFFFF",
                "cyan": "#2AA198",
                "foreground": "#839496",
                "green": "#859900",
                "name": "Solarized Dark",
                "purple": "#D33682",
                "red": "#DC322F",
                "selectionBackground": "#FFFFFF",
                "white": "#EEE8D5",
                "yellow": "#B58900"
            },
            {
                "background": "#FDF6E3",
                "black": "#002B36",
                "blue": "#268BD2",
                "brightBlack": "#073642",
                "brightBlue": "#839496",
                "brightCyan": "#93A1A1",
                "brightGreen": "#586E75",
                "brightPurple": "#6C71C4",
                "brightRed": "#CB4B16",
                "brightWhite": "#FDF6E3",
                "brightYellow": "#657B83",
                "cursorColor": "#002B36",
                "cyan": "#2AA198",
                "foreground": "#657B83",
                "green": "#859900",
                "name": "Solarized Light",
                "purple": "#D33682",
                "red": "#DC322F",
                "selectionBackground": "#FFFFFF",
                "white": "#EEE8D5",
                "yellow": "#B58900"
            },
            {
                "background": "#000000",
                "black": "#000000",
                "blue": "#3465A4",
                "brightBlack": "#555753",
                "brightBlue": "#729FCF",
                "brightCyan": "#34E2E2",
                "brightGreen": "#8AE234",
                "brightPurple": "#AD7FA8",
                "brightRed": "#EF2929",
                "brightWhite": "#EEEEEC",
                "brightYellow": "#FCE94F",
                "cursorColor": "#FFFFFF",
                "cyan": "#06989A",
                "foreground": "#D3D7CF",
                "green": "#4E9A06",
                "name": "Tango Dark",
                "purple": "#75507B",
                "red": "#CC0000",
                "selectionBackground": "#FFFFFF",
                "white": "#D3D7CF",
                "yellow": "#C4A000"
            },
            {
                "background": "#FFFFFF",
                "black": "#000000",
                "blue": "#3465A4",
                "brightBlack": "#555753",
                "brightBlue": "#729FCF",
                "brightCyan": "#34E2E2",
                "brightGreen": "#8AE234",
                "brightPurple": "#AD7FA8",
                "brightRed": "#EF2929",
                "brightWhite": "#EEEEEC",
                "brightYellow": "#FCE94F",
                "cursorColor": "#000000",
                "cyan": "#06989A",
                "foreground": "#555753",
                "green": "#4E9A06",
                "name": "Tango Light",
                "purple": "#75507B",
                "red": "#CC0000",
                "selectionBackground": "#FFFFFF",
                "white": "#D3D7CF",
                "yellow": "#C4A000"
            },
            {
                "background": "#000000",
                "black": "#000000",
                "blue": "#000080",
                "brightBlack": "#808080",
                "brightBlue": "#0000FF",
                "brightCyan": "#00FFFF",
                "brightGreen": "#00FF00",
                "brightPurple": "#FF00FF",
                "brightRed": "#FF0000",
                "brightWhite": "#FFFFFF",
                "brightYellow": "#FFFF00",
                "cursorColor": "#FFFFFF",
                "cyan": "#008080",
                "foreground": "#C0C0C0",
                "green": "#008000",
                "name": "Vintage",
                "purple": "#800080",
                "red": "#800000",
                "selectionBackground": "#FFFFFF",
                "white": "#C0C0C0",
                "yellow": "#808000"
            },
            {
                "background": "#323232",
                "black": "#323232",
                "blue": "#4099FF",
                "brightBlack": "#535353",
                "brightBlue": "#5EB7F7",
                "brightCyan": "#DCF4FF",
                "brightGreen": "#9DFF91",
                "brightPurple": "#FF9DFF",
                "brightRed": "#F07070",
                "brightWhite": "#FFFFFF",
                "brightYellow": "#FFE48B",
                "cursorColor": "#FFFFFF",
                "cyan": "#BED6FF",
                "foreground": "#00FF00",
                "green": "#7FE173",
                "name": "idleToes",
                "purple": "#F680FF",
                "red": "#D25252",
                "selectionBackground": "#FFFFFF",
                "white": "#EEEEEC",
                "yellow": "#FFC66D"
            }
        ]
    }
    • 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/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. 配置鼠标右键

    新版的Terminal安装后右键默认带有了菜单,无需在手动配置。

问题说明

  1. 实验环境

    VMware下的Windows2004版本