Windows Terminal 开发环境美化配置

Windows Terminal + Starship + Terminal-Icons + Vim 完整配置,复刻 Ghostty 风格。

环境概览

组件 作用
Windows Terminal 终端宿主
Starship 提示符美化
Hack Nerd Font 图标字体
Terminal-Icons ls 图标着色
Vim 9.2 命令行编辑器

一、安装字体

下载 Hack Nerd Font,解压后右键 .ttf为所有用户安装


二、安装 Starship

winget install Starship.Starship

三、配置 PowerShell Profile

notepad $PROFILE

写入以下内容(注意每行独立):

Invoke-Expression (&starship init powershell)
Set-ItemProperty HKCU:\Console VirtualTerminalLevel -Value 1
Import-Module Terminal-Icons

或用命令写入(避免编码问题):

$lines = @(
    'Invoke-Expression (&starship init powershell)',
    'Set-ItemProperty HKCU:\Console VirtualTerminalLevel -Value 1',
    'Import-Module Terminal-Icons'
)
[System.IO.File]::WriteAllLines($PROFILE, $lines, [System.Text.Encoding]::UTF8)

四、配置 Starship 主题

New-Item -ItemType Directory -Force -Path "$HOME\.config"

写入配置(强制 UTF-8,避免乱码):

$lines = @(
    'format = """',
    '$directory\',
    '$git_branch\',
    '$git_status\',
    '$golang\',
    '$java\',
    '$line_break\',
    '$character"""',
    '',
    '[directory]',
    'style = "bold #83a598"',
    'format = "[$path]($style) "',
    'truncation_length = 4',
    'truncate_to_repo = true',
    '',
    '[git_branch]',
    'symbol = " "',
    'style = "bold #d3869b"',
    'format = "[$symbol$branch]($style) "',
    '',
    '[git_status]',
    'style = "#fabd2f"',
    'format = "([$all_status$ahead_behind]($style) )"',
    'modified = "!$count"',
    'staged = "+$count"',
    'deleted = "-$count"',
    'untracked = "?$count"',
    '',
    '[golang]',
    'symbol = " "',
    'style = "#83a598"',
    'format = "[$symbol$version]($style) "',
    '',
    '[java]',
    'symbol = " "',
    'style = "#cc241d"',
    'format = "[$symbol$version]($style) "',
    '',
    '[cmd_duration]',
    'min_time = 2000',
    'style = "#a89984"',
    'format = "[ $duration]($style) "',
    '',
    '[character]',
    'success_symbol = "[>](bold #b8bb26)"',
    'error_symbol = "[>](bold #fb4934)"'
)
[System.IO.File]::WriteAllLines(
    "$HOME\.config\starship.toml",
    $lines,
    [System.Text.Encoding]::UTF8
)

五、配置 Windows Terminal 配色

Ctrl+, → 打开 JSON 文件,全部替换为:

{
    "$help": "https://aka.ms/terminal-documentation",
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "actions": [
        {"command": {"action": "copy", "singleLine": false}, "id": "User.copy.644BA8F2"},
        {"command": "paste", "id": "User.paste"},
        {"command": "find", "id": "User.find"},
        {"command": {"action": "splitPane", "split": "auto", "splitMode": "duplicate"}, "id": "User.splitPane.A6751878"}
    ],
    "copyFormatting": "none",
    "copyOnSelect": false,
    "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "keybindings": [
        {"id": "User.copy.644BA8F2", "keys": "ctrl+c"},
        {"id": "User.paste", "keys": "ctrl+v"},
        {"id": "User.find", "keys": "ctrl+shift+f"},
        {"id": "User.splitPane.A6751878", "keys": "alt+shift+d"}
    ],
    "newTabMenu": [{"type": "remainingProfiles"}],
    "profiles": {
        "defaults": {
            "colorScheme": "Ghostty Default",
            "font": {"face": "Hack Nerd Font", "size": 13},
            "opacity": 96,
            "useAcrylic": true
        },
        "list": [
            {
                "commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "hidden": false,
                "name": "Windows PowerShell"
            },
            {
                "commandline": "%SystemRoot%\\System32\\cmd.exe",
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "hidden": false,
                "name": "命令提示符"
            },
            {
                "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                "hidden": true,
                "name": "Ubuntu",
                "source": "Windows.Terminal.Wsl"
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "guid": "{51855cb2-8cce-5362-8f54-464b92b32386}",
                "hidden": false,
                "name": "Ubuntu",
                "source": "CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc"
            },
            {
                "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
                "hidden": false,
                "name": "PowerShell",
                "source": "Windows.Terminal.PowershellCore"
            }
        ]
    },
    "schemes": [
        {
            "name": "Ghostty Default",
            "background": "#1e2029",
            "foreground": "#f8f8f2",
            "black":        "#21222c",
            "red":          "#ff5555",
            "green":        "#50fa7b",
            "yellow":       "#f1fa8c",
            "blue":         "#bd93f9",
            "purple":       "#ff79c6",
            "cyan":         "#8be9fd",
            "white":        "#f8f8f2",
            "brightBlack":  "#6272a4",
            "brightRed":    "#ff6e6e",
            "brightGreen":  "#69ff94",
            "brightYellow": "#ffffa5",
            "brightBlue":   "#d6acff",
            "brightPurple": "#ff92df",
            "brightCyan":   "#a4ffff",
            "brightWhite":  "#ffffff"
        },
        {
            "name": "Gruvbox Dark",
            "background": "#282828",
            "foreground": "#ebdbb2",
            "black":        "#282828",
            "blue":         "#458588",
            "cyan":         "#689d6a",
            "green":        "#98971a",
            "purple":       "#b16286",
            "red":          "#cc241d",
            "white":        "#d5c4a1",
            "yellow":       "#928374",
            "brightBlack":  "#928374",
            "brightBlue":   "#83a598",
            "brightCyan":   "#8ec07c",
            "brightGreen":  "#b8bb26",
            "brightPurple": "#d3869b",
            "brightRed":    "#fb4934",
            "brightWhite":  "#ebdbb2",
            "brightYellow": "#d5c4a1"
        }
    ],
    "themes": []
}

切换主题只需改 colorScheme"Gruvbox Dark""Ghostty Default"


六、安装 Terminal-Icons

Install-Module -Name Terminal-Icons -Repository PSGallery -Force

已在 Profile 中配置,重开终端后 ls 自动显示文件图标和颜色。


七、安装并配置 Vim

winget install vim.vim

安装后加入 PATH:

$vimPath = "C:\Program Files\Vim\vim92"
[Environment]::SetEnvironmentVariable("PATH", $env:PATH + ";$vimPath", "Machine")
$env:PATH += ";$vimPath"

写入 .vimrc

$lines = @(
    'set number',
    'set tabstop=4',
    'set shiftwidth=4',
    'set expandtab',
    'set autoindent',
    'set hlsearch',
    'set incsearch',
    'set ignorecase',
    'set smartcase',
    'set cursorline',
    'set encoding=utf-8',
    'syntax on',
    'colorscheme habamax',
    'set background=dark',
    'nnoremap <C-s> :w<CR>',
    'inoremap <C-s> <Esc>:w<CR>a',
    'nnoremap <C-q> :q<CR>'
)
[System.IO.File]::WriteAllLines(
    "$HOME\.vimrc",
    $lines,
    [System.Text.Encoding]::UTF8
)

Vim 常用操作:

操作 按键
进入编辑 i
退出编辑 Esc
保存 Ctrl+S
保存退出 :wq
不保存退出 :q!
搜索 /关键词
跳到第N行 :N

八、故障排查

Terminal 无法启动

用记事本直接打开 settings.json 修复:

Win+R 输入:
notepad "%LOCALAPPDATA%\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"

Starship 不生效

# 确认 profile 路径
$PROFILE
# 重载
. $PROFILE

配置文件中文乱码

不要用记事本直接粘贴 Unicode 字符,统一用 [System.IO.File]::WriteAllLines 写入。

vim 找不到命令

Get-ChildItem "C:\Program Files\Vim" -Recurse -Filter "vim.exe"
# 找到路径后加入 PATH
$env:PATH += ";C:\Program Files\Vim\vim92"