Windows Terminal 与 PowerShell 配置记录

发布于:2023-01-21 ⋅ 阅读:(946) ⋅ 点赞:(0)

https://img-home.csdnimg.cn/images/20220524100510.png

写在前面

Windows Terminal 是微软推出的一款美观易用的终端工具。解决了 Unicode 和 UTF-8 字符渲染问题,使用 GPU 加速文本呈现引擎,并提供了足够的可定制化空间。

需要注意的是 Windows Terminal 并不是一个 Shell,我们仍需要通过 PowerShell 或 Command Prompt 来与系统进行交互。

安装 Windows Terminal

Windows 11 自带 Windows Terminal。如果没有,也可以在 Microsoft Stroe 中安装。

应用商店: Windows Terminal

安装 Windows PowerShell 7

Windows 自带的 PowerShell 版本为 5,可以在 Microsoft Stroe 中安装 PowerShell 7 来获得更多新特性。

参考:Installing PowerShell on Windows

# View powershell version
$PSVersionTable

安装 Nerd Font

Oh My Posh 的大部分主题中都包含的特殊字符,需要使用 Nerd Font 显示。

建议使用是 Consolas Nerd Font。(Consolas 是 Visual Studio Code 的默认字体)

Windows Terminal 默认使用的字体是 Cascadia Code 字体,但是这并不是一个 Nerd Font,而是 PowerLine Font,特殊字符会少一点。

安装 Oh My Posh

使用 Oh My Posh 自定义 PowerShell Prompt。

参考:Oh My Posh

安装完成后重启 Windows Terminal 以刷新环境变量。

# View oh-my-posh version
oh-my-posh --version

配置 PowerShell Profile

Profile 文件的作用相当于于 Bash 中的 .bashrc 文件,PowerShell 启动时会加载 Profile。

# View profile path
echo $Profile

# Edit profile with vscode
code $Profile

# Set prompt theme as ys
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\ys.omp.json" | Invoke-Expression

# Autocompletion for arrow keys
Set-PSReadLineOption -PredictionSource History -ShowToolTips
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

Git Bash 配置

想要把 Oh My Posh 应用在 Git Bash 中,可以执行以下命令。

# Edit .bashrc with vscode
code ~/.bashrc

# Set prompt theme as ys
eval "$(oh-my-posh init bash --config $POSH_THEMES_PATH'/ys.omp.json')"

配置 Windows Terminal

Ctrl + ~ 快捷键呼出设置,点击左下角即可直接编辑 Windows Terminal 配置的 Json 文件。

我们可以通过配置文件来自定义 Windows Terminal 的配色、快捷键、标签页等属性。

Galaxy 主题 配置文件

{
    "$help": "https://aka.ms/terminal-documentation",
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "actions": [
        {
            "command": {
                "action": "copy",
                "singleLine": false
            },
            "keys": "ctrl+c"
        },
        {
            "command": "paste",
            "keys": "ctrl+v"
        },
        {
            "command": "find",
            "keys": "ctrl+shift+f"
        },
        {
            "command": {
                "action": "splitPane",
                "split": "auto",
                "splitMode": "duplicate"
            },
            "keys": "alt+shift+d"
        }
    ],
    "copyFormatting": "none",
    "copyOnSelect": false,
    "defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    "profiles": {
        "defaults": {
            "colorScheme": "Galaxy",
            "font": {
                "face": "Consolas Nerd Font",
                "size": 14
            },
            "suppressApplicationTitle": true
        },
        "list": [
            {
                "commandline": "pwsh.exe -Nologo",
                "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
                "hidden": false,
                "name": "Windows PowerShell 7",
                "source": "Windows.Terminal.PowershellCore"
            },
            {
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "hidden": false,
                "name": "Windows PowerShell"
            },
            {
                "guid": "{2ece5bfe-50ed-5f3a-ab87-5cd4baafed2b}",
                "hidden": false,
                "name": "Git Bash",
                "source": "Git"
            },
            {
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "hidden": false,
                "name": "Command Prompt"
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": true,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "guid": "{57dc719c-57e5-5d17-9989-34312885e023}",
                "hidden": true,
                "name": "Developer Command Prompt for VS 2022",
                "source": "Windows.Terminal.VisualStudio"
            },
            {
                "guid": "{e518cc62-f44d-57fc-8dea-b296953a4c12}",
                "hidden": true,
                "name": "Developer PowerShell for VS 2022",
                "source": "Windows.Terminal.VisualStudio"
            },
            {
                "guid": "{16208362-94fc-5b1f-a491-5b2624d5ab56}",
                "hidden": true,
                "name": "Visual Studio Debug Console",
                "source": "VSDebugConsole"
            }
        ]
    },
    "schemes": [
        {
            "background": "#222324",
            "black": "#000000",
            "blue": "#589DF6",
            "brightBlack": "#555555",
            "brightBlue": "#589DF6",
            "brightCyan": "#3979BC",
            "brightGreen": "#35BB9A",
            "brightPurple": "#E75699",
            "brightRed": "#FA8C8F",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#FFFF55",
            "cursorColor": "#BBBBBB",
            "cyan": "#1F9EE7",
            "foreground": "#FFFFFF",
            "green": "#21B089",
            "name": "Galaxy",
            "purple": "#944D95",
            "red": "#F9555F",
            "selectionBackground": "#B5D5FF",
            "white": "#BBBBBB",
            "yellow": "#FEF02A"
        }
    ]
}
本文含有隐藏内容,请 开通VIP 后查看