Windows 终端配置

Insider Lv3

系列目录:Windows 系列目录。

以简体中文 Windows 11(x64)为例,配置命令补全、历史搜索、目录跳转和 Git 状态提示。

一、安装终端工具

确认 winget --version 能正常输出版本号后,在下方勾选所需工具,复制安装命令,在 PowerShell 中执行。清单每页显示 10 项,翻页会保留勾选。

前五项是基础软件;eza、ripgrep、fzf、fastfetch、zoxide、Starship 和 btop4win 按需安装。需要完整使用本文的快捷键和命令时,安装全部工具。

应用名称WinGet ID版本号
Windows 终端Microsoft.WindowsTerminal最新版
PowerShell 7Microsoft.PowerShell最新版
GitGit.Git最新版
Visual Studio Code(VSCode)Microsoft.VisualStudioCode最新版
JetBrainsMono Nerd FontDEVCOM.JetBrainsMonoNerdFont最新版
eza(文件列表)eza-community.eza最新版
ripgrep(文本搜索)BurntSushi.ripgrep.MSVC最新版
fzf(模糊搜索)junegunn.fzf最新版
fastfetch(系统信息)Fastfetch-cli.Fastfetch最新版
zoxide(目录跳转)ajeetdsouza.zoxide最新版
Starship(命令提示符)Starship.Starship最新版
btop4win(系统监控)aristocratos.btop4win最新版

安装完成后关闭终端,重新打开 Windows 终端,选择 PowerShell 标签页。后续配置均在 PowerShell 7 中完成,可运行 $PSVersionTable.PSVersion 查看版本。

二、设置终端和字体

打开 Windows 终端的“设置”:

  1. 在“启动”中,将“默认配置文件”设为 PowerShell,将“默认终端应用程序”设为 Windows 终端。
  2. 在“配置文件 → 默认值 → 外观”中,将“字体”设为 JetBrainsMono Nerd Font。
  3. 将“字号”设为 11 或 12,保存设置。

VSCode 中打开设置,搜索 terminal.integrated.fontFamily,同样填写 JetBrainsMono Nerd Font;在命令面板中选择“终端: 选择默认配置文件”,再选择 PowerShell。

三、配置 PowerShell

安装模糊搜索模块

安装 fzf 后,在 PowerShell 7 中安装 PSFzf :

1
Install-PSResource -Name PSFzf -Repository PSGallery -Scope CurrentUser

如果终端提示禁止加载个人配置文件,执行:

1
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

编辑个人配置文件

创建并打开当前用户的 PowerShell 配置文件:

1
2
3
4
5
New-Item -ItemType Directory -Path (Split-Path $PROFILE) -Force | Out-Null
if (-not (Test-Path -LiteralPath $PROFILE)) {
New-Item -ItemType File -Path $PROFILE | Out-Null
}
notepad $PROFILE

将下面的配置写入文件并以 UTF-8 保存。已有配置时,先备份,再合并需要的部分。

配置包含 UTF-8 编码、历史命令预测、Tab 补全、模糊搜索快捷键,以及 Starship、zoxide 和 eza 的集成;未安装的可选工具会跳过。

PowerShell 配置内容(展开复制)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 使用 UTF-8 编码(不带 BOM)。
$Utf8NoBom = [System.Text.UTF8Encoding]::new()
[Console]::InputEncoding = $Utf8NoBom
[Console]::OutputEncoding = $Utf8NoBom
$OutputEncoding = $Utf8NoBom

$IsInteractiveSession = $Host.Name -eq "ConsoleHost" `
-and $Host.UI `
-and $Host.UI.SupportsVirtualTerminal `
-and -not [Console]::IsInputRedirected `
-and -not [Console]::IsOutputRedirected

$env:STARSHIP_CONFIG = Join-Path $HOME ".config\starship.toml"
$FzfPrompt = [char]0x276F
$FzfPointer = [char]0x203A
$FzfMarker = [char]0x2713
$env:FZF_DEFAULT_OPTS = "--height=45% --layout=reverse --border=rounded --info=inline --prompt=`"$FzfPrompt `" --pointer=`"$FzfPointer`" --marker=`"$FzfMarker`""

if ($IsInteractiveSession) {
Import-Module PSReadLine -ErrorAction SilentlyContinue

if (Get-Module PSReadLine) {
Set-PSReadLineOption -EditMode Windows
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineOption -HistoryNoDuplicates
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineOption -MaximumHistoryCount 10000
Set-PSReadLineOption -BellStyle None
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
}

if ((Get-Command fzf -ErrorAction SilentlyContinue) -and (Get-Module -ListAvailable -Name PSFzf)) {
Import-Module PSFzf
Set-PsFzfOption `
-PSReadlineChordReverseHistory "Ctrl+r" `
-PSReadlineChordProvider "Ctrl+f" `
-PSReadlineChordSetLocation "Alt+c"
}
}

if ($IsInteractiveSession -and (Get-Command starship -ErrorAction SilentlyContinue)) {
Invoke-Expression (&starship init powershell)
}

if ($IsInteractiveSession -and (Get-Command zoxide -ErrorAction SilentlyContinue)) {
Remove-Variable -Name __zoxide_hooked -Scope Global -ErrorAction SilentlyContinue
Invoke-Expression (& { (zoxide init powershell --cmd cd | Out-String) })
Set-Alias -Name zi -Value cdi -Scope Global -Force
}

if (Get-Command eza -ErrorAction SilentlyContinue) {
Remove-Item Alias:ls -ErrorAction SilentlyContinue
function global:ls { eza --icons=auto --hyperlink=auto --group-directories-first @args }
function global:ll { eza -lah --git --icons=auto --hyperlink=auto --group-directories-first @args }
function global:tree { eza --tree --icons=auto --hyperlink=auto @args }
}

Remove-Item Alias:ii -Force -ErrorAction SilentlyContinue
function global:ii([string]$Path = ".") { Invoke-Item -LiteralPath $Path }
Set-Alias -Name open -Value ii -Scope Global -Force

function global:reload { . $PROFILE }
function global:Edit-Profile {
if (Get-Command code -ErrorAction SilentlyContinue) { code $PROFILE }
else { notepad $PROFILE }
}
Set-Alias -Name ep -Value Edit-Profile -Scope Global -Force
function global:which([Parameter(Mandatory)][string]$Name) {
Get-Command $Name | Select-Object CommandType, Name, Version, Source, Definition
}

保存后重新打开一个 PowerShell 标签页,加载配置。此后修改配置,可运行 reload 重新加载。

四、配置 Starship 提示符

使用 Starship 配置 显示当前目录、Git 状态、开发语言版本、命令耗时和时间。

创建配置目录,并打开配置文件:

1
2
New-Item -ItemType Directory -Path "$HOME\.config" -Force | Out-Null
notepad "$HOME\.config\starship.toml"

将下面内容保存到 starship.toml;已有配置时先备份。上一步的 PowerShell 配置会自动加载它。

Starship 配置内容(展开复制)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"$schema" = "https://starship.rs/config-schema.json"

add_newline = true
command_timeout = 1200
scan_timeout = 30
continuation_prompt = "[\u2219](bright-black) "

format = """
$directory\
$git_branch\
$git_status\
$git_state\
$fill\
$python\
$nodejs\
$rust\
$cmd_duration\
$status\
$time\
$line_break\
$character"""

[fill]
symbol = " "

[directory]
format = "[$path]($style)[$read_only]($read_only_style) "
style = "bold cyan"
read_only = " \U000F033E"
read_only_style = "bold red"
truncation_length = 4
truncate_to_repo = false

[git_branch]
format = "[$symbol$branch]($style) "
symbol = "\uE0A0 "
style = "bold purple"

[git_status]
format = "([$all_status$ahead_behind]($style) )"
style = "bold yellow"
conflicted = "="
ahead = "\u21E1"
behind = "\u21E3"
diverged = "\u21D5"
untracked = "?"
stashed = "$"
modified = "!"
staged = "+"
renamed = "\u00BB"
deleted = "\u2718"

[git_state]
format = '[\($state( $progress_current/$progress_total)\)]($style) '
style = "bold yellow"

[python]
format = '[$symbol$version( \($virtualenv\))]($style) '
symbol = "\uE235 "
style = "yellow"
detect_extensions = []
detect_files = ["pyproject.toml", "requirements.txt", ".python-version", "uv.lock"]

[nodejs]
format = "[$symbol$version]($style) "
symbol = "\uE718 "
style = "green"
detect_extensions = []
detect_files = ["package.json", "pnpm-lock.yaml", "yarn.lock", "package-lock.json"]

[rust]
format = "[$symbol$version]($style) "
symbol = "\uE7A8 "
style = "red"

[cmd_duration]
format = "[\U000F199F $duration]($style) "
style = "yellow"
min_time = 1000
show_milliseconds = false

[status]
format = "[\u2718 $status]($style) "
symbol = "\u2718"
style = "bold red"
disabled = false

[time]
format = "[\uF017 $time]($style)"
style = "bright-black"
time_format = "%H:%M"
disabled = false

[character]
success_symbol = "[\u276F](bold green)"
error_symbol = "[\u276F](bold red)"
vimcmd_symbol = "[\u276E](bold green)"
vimcmd_replace_one_symbol = "[\u276E](bold purple)"
vimcmd_replace_symbol = "[\u276E](bold purple)"
vimcmd_visual_symbol = "[\u276E](bold yellow)"

保存后在终端按一次回车查看效果。语言版本会在对应项目目录中显示,Python、Node.js、Rust 等运行环境按项目需要另行安装。

五、配置 Git

将姓名和邮箱替换为自己的信息,再执行:

1
2
3
4
5
git config --global user.name "你的名字"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.autocrlf true
git config --global fetch.prune true

新仓库默认使用 main 分支;文本文件检出时使用 CRLF、提交时转为 LF;获取远端更新时自动清理已删除的远端跟踪分支。项目已有 .gitattributes 时,以项目约定为准。

六、常用操作

以下快捷键和命令在安装对应工具、加载上述配置后可用。

操作用途
Tab显示命令、参数或路径补全菜单
↑ / ↓按已输入的前缀查找历史命令
Ctrl + R模糊搜索历史命令,选中后填入命令行
Ctrl + F搜索文件或目录,将选中路径填入命令行
Alt + C搜索当前目录下的子目录并进入
cd 关键词跳转到 zoxide 记录的常用目录
zi从访问过的目录中交互选择并跳转
ls / ll查看文件列表 / 包含隐藏文件的详细列表
tree -L 2查看两层目录树
rg "关键词" .搜索当前目录下文件中的文本
open .在文件资源管理器中打开当前目录
code .在 VSCode 中打开当前目录
ep / reload编辑 / 重新加载 PowerShell 配置
which git查看 Git 命令的位置和信息
fastfetch查看系统信息
btop查看处理器、内存、磁盘、网络和进程状态

先用完整路径进入常用项目目录,让 zoxide 记录它们,再使用 cd 关键词 或 zi。

七、检查是否完成

重新打开 Windows 终端,运行以下命令,确认版本号和模块信息能正常显示:

1
2
3
4
pwsh --version
git --version
fzf --version
Get-Module -ListAvailable PSFzf

再试用 ll、zi 和 Ctrl + R,进入一个 Git 仓库确认提示符能显示分支与文件状态。

  • Title: Windows 终端配置
  • Author: Insider
  • Created at : 2024-06-17 03:45:14
  • Updated at : 2026-09-29 09:41:32
  • Link: https://blog.insider.baby//2024/06/17/Windows开发环境搭建指南/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments