Windows Terminal+PowerShell7打造Windows10下好用的终端

本文最后更新于:2021年2月24日 上午

前言

一般来说,Linux和macOS由于都具有一点Unix血统,使得它们的用户平时使用Terminal/Shell的频率要远高于Windows用户。作为Windows老用户,你是否也早已厌倦了Windows下的CMD的“大黑框”以及Powershell的“小蓝屏”?本文将详细的告诉你如何打造自己的个性化且好用的终端。

🎈本文在描述过程中,提供的样式和配置项仅供参考,因个人审美不同,您可以修改为自己喜欢的样式、启用/注释自己按需选择的注释项。除此外,本文尽可能将碰到的需要注意的问题详细介绍,希望您在阅读本文后也能打造出自己喜欢的Windows终端!

安装

安装字体

系统自带的字体并不能满足接下来个性化配置的需求,并且可能出现字形显示不出来的问题,你可能在一些类似的教程里看到推荐使用PowerLine的字体,但是经博主实践,似乎在PowerLine字体里,一些字形仍然无法显示,所以推荐使用同样开源但是能完整在Windows Terminal里显示所有字形和符号的Nerd Font,同样的它也开源在Github:

有关于其介绍你可以查看Nerd Font官网,那么多字体如何选择呢?在官网的下载页面不仅提供了字体的下载还提供了字体在线预览:

点击Preview即可预览字体

下载喜欢的字体文件安装即可,博主使用的字体为FiraMono Nerd Font

Windows Terminal

Windows Terminal是微软在Build2019大会上发布的面向Windows的终端程序,它开源在Github:

不久之前已经发布正式版,所以本文推荐直接在Microsoft应用商店下载安装,可以看到有正式版和预览版,此处都以正式版为例,在应用商店下载安装完成即可~

Windows Terminal在UWP应用商店

安装PowerShell7

系统自带的PowerShell和这里介绍的几乎是两个不同的东西,它同样开源在Github:

打开项目地址后,我们只需要在Releases页面下载适合系统平台的最新版安装包即可:

此处以最新Preview版为例,目前看起来PowerShell7的预览版还是很稳定的👍

以Win x64为例

下载后执行安装程序:

开始安装PowerShell7

默认的安装路径在C盘,如果你想要修改到其他磁盘当然也是可以的,自己记住即可!

安装路径

配置项如图勾选即可:

配置

安装PowerShell插件

打开PowerShell7,分别执行下面命令安装这几个必需的插件:

1
2
3
4
5
6
# 1. 安装 PSReadline 包,该插件可以让命令行很好用,类似 zsh
Install-Module -Name PSReadLine -AllowPrerelease -Force -Verbose
# 2. 安装 posh-git 包,让你的 git 更好用.出现提示输入 Y 或者 A 同意
Install-Module posh-git -Scope CurrentUser -Verbose
# 3. 安装 oh-my-posh 包,让你的命令行更酷炫、优雅
Install-Module oh-my-posh -Scope CurrentUser -Verbose

安装插件速度很慢,进度条不动?

这三个插件在安装的时候需要下载一些文件,由于国内网络的问题,可能会下载很慢……怎么解决呢?

首先,你肯定需要一个可以快速访问国际互联网的代理,此处以Clash For Windows为例:(本地HTTP代理端口7890),打开PowerShell,输入命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 设置本地代理的端口号 7890 为你实际的端口号
set http_proxy=http://127.0.0.1:7890
set https_proxy=http://127.0.0.1:7890

# 设置代理的用户名和密码(Clash For Windows不需要)
# HTTP
set http_proxy_user=username
set http_proxy_pass=password
# HTTPS
set https_proxy_user=username
set https_proxy_pass=password

# 如果需要恢复本地代理(清除代理)
set http_proxy=
set https_proxy=

此外,对于Clash For Windows,其默认的Rule(规则模式),可能让下载插件走直连,所以建议在此过程中将其代理模式设置为Global(全局模式),设置好后,继续执行上面的安装模块。安装完成后在PowerShell7中使用“module”命令检查安装插件是否成功:

验证安装完成

配置

配置Windows Terminal

开始菜单打开Windows Terminal,按照图示步骤打开Windows Terminal配置文件(*.json格式)

往下拉,我们可以看到默认安装到C盘PowerShell7已经在Windows Terminal的配置文件里了

复制PowerShell7的guid粘贴到默认配置的guid里:

配置文件的"schemes"包含终端里面文本的配色,注意要使用*.json格式书写

这个样式可以去哪儿获取呢?这里有一个在线网站:https://windowsterminalthemes.dev/ ,按照图中所示步骤获取自己喜欢的配色即可

博主的Windows Terminal配置参考:(根据需求选择/修改)

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// This file was initially generated by Windows Terminal 1.5.10411.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":
{
"fontFace": "FiraCode NF", //字体名称
"fontSize": 12, //字体大小
"colorScheme": "ayu", //文本主题名称
"padding": "5, 5, 20, 25",
//"useAcrylic": true, //使用亚克力玻璃效果:true or false
//"acrylicOpacity": 0.90, //亚克力效果,范围:0~1
//"background": "#000000", //背景颜色
"backgroundImage": "C:/Program Files/PowerShell/bg.jpg", //背景图片
//"backgroundImageOpacity": 0.9, //背景图像透明度
"launchMod": "default", //默认模式
//"launchMod": "focus", //聚焦模式
"alwaysOnTop": false,
"snapOnInput": true,
"supportApplicationTitle": true
// 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",
"commandline": "C:/Program Files/PowerShell/7-preview/pwsh.exe -nologo", //关闭启动时一段提示
"startingDirectory": ".", //启动菜单
"historySize": 9001, //历史记录
}
]
},

// Add custom color schemes to this array.
// To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
"schemes": [
{
"name": "ayu",
"black": "#000000",
"red": "#ff3333",
"green": "#b8cc52",
"yellow": "#e7c547",
"blue": "#36a3d9",
"purple": "#f07178",
"cyan": "#95e6cb",
"white": "#ffffff",
"brightBlack": "#323232",
"brightRed": "#ff6565",
"brightGreen": "#eafe84",
"brightYellow": "#fff779",
"brightBlue": "#68d5ff",
"brightPurple": "#ffa3aa",
"brightCyan": "#c7fffd",
"brightWhite": "#ffffff",
"background": "#0f1419",
"foreground": "#e6e1cf"
},
{
"name": "Blue Matrix",
"black": "#101116",
"red": "#ff5680",
"green": "#00ff9c",
"yellow": "#fffc58",
"blue": "#00b0ff",
"purple": "#d57bff",
"cyan": "#76c1ff",
"white": "#c7c7c7",
"brightBlack": "#686868",
"brightRed": "#ff6e67",
"brightGreen": "#5ffa68",
"brightYellow": "#fffc67",
"brightBlue": "#6871ff",
"brightPurple": "#d682ec",
"brightCyan": "#60fdff",
"brightWhite": "#ffffff",
"background": "#101116",
"foreground": "#00a2ff"
}
],

// 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" }
]
}

配置PowerShell

打开PowerShell7,输入命令:

1
2
3
4
5
# 设置执行策略
Set-ExecutionPolicy RemoteSigned
# 使用记事本打开 PowerShell 配置文件(没有则会新建该文件该文件)
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
notepad $PROFILE

之前可能没有这个文件,点击,新建这个文件

编辑配置文件,需要的配置项取消命令前面的注释,不需要的配置项使用 “#” 注释:

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
# 导入 posh-git 模块
Import-Module posh-git

# 导入oh-my-posh 模块
Import-Module oh-my-posh

#设置主题
Set-PoshPrompt -Theme jandedobbeleer

# 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo

# 设置 Ctrl+d 为退出 PowerShell
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit

# 设置 Tab 键菜单补全
Set-PSReadlineKeyHandler -Key Tab -Function Complete
# 和上面的菜单补全二选一
# 设置 Tab 键自动补全
# Set-PSReadlineKeyHandler -Key Tab -Function Complete

# 设置 Ctrl+d 为退出 PowerShell
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit

# 使用 ls 和 ll 查看目录
function ListDirectory {
(Get-ChildItem).Name
Write-Host("")
}
Set-Alias -Name ls -Value ListDirectory
Set-Alias -Name ll -Value Get-ChildItem

# 设置向上键为后向搜索历史记录
#Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

# 设置向下键为前向搜索历史纪录
#Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

查看oh-my-posh默认主题,在终端里面输入命令Get-PoshThemes预览内置主题,选择喜欢的主题名,填入上面的配置项即可。

更多主题可以在oh-my-posh文档中获取👈

文档

授人以鱼不如授人以渔,更多高级配置可以查看各个项目的官方文档:

积分

本文在此过程了,摸了一些坑,也拜读了一些大佬的文章,在此表示感谢!以下排列不分先后: