安装PowerShell
- windowns10默认情况下,自带的PowerShell为5.1,属于很难用的那种。因此需要自己安装最新版的PowerShell7,微软新一代跨平台版本Shell,好用很多。
1、使用winget安装
使用官方推荐方式进行安装,win+x 打开自带的PowerShell,输入如下命令:
1
2
3
4查找当前可用的PowerShell版本
winget search Microsoft.PowerShell
使用winget安装PowerShell,这里安装stable版本
winget install --id Microsoft.PowerShell安装完毕之后,新版的PowerShell为pwsh.exe 默认已经添加到环境变量中
2、替换PowerShell入口
- 此时需要替换
win+x
和菜单右键中的PowerShell入口,否则使用的还是旧版本的 - 在windows菜单中搜索
Windows PowerShell
右键文件所在位置,可以看到快捷方式文件,选中后右键修改它的属性,目标修改为"C:\Program Files\PowerShell\7\pwsh.exe"
,起始位置可以根据自己习惯修改,为这里改为用户目录:%USERPROFILE%
- 然后是修改按住shift时右键菜单中的
在PowerShell中打开
win+r
运行 regedit,找到HKEY_CLASSES_ROOT\Directory\Background\shell\PowerShell\command
- 默认情况下
command
是归属于TrustedInstaller,无法修改当前用户的控制权,需要将所有者进行修改。右键command
,点击权限,高级,更改所有者为当前登录用户,完成后点击确定。然后在权限-安全窗口中选中登录用户,勾选上完全控制,点击确认关闭权限窗口。 - 然后在注册表编辑器中选中
command
在右侧修改(默认)
的数值数据,将powershell.exe改为pwsh.exe,然后点击确定即可 - 以上步骤完成后win+x和菜单右键中的PowerShell都将被替换为pwsh.exe也就是最新版本
安装windows终端
- 控制台其实还是不好看,建议直接安装windows终端
- 具体教程参考微软的官方说明即可:https://learn.microsoft.com/zh-cn/windows/terminal/install
- 安装完毕后,在设置中将默认配置文件改为 PowerShell 即可
安装ohmyposh
- 在MacOS下,我特别喜欢使用zsh+ohmyzsh,有很多好看的主题。现在使用PowerShell也有了类似的插件叫ohmyposh
- 官方安装指南:https://ohmyposh.dev/docs/installation/windows
1、安装ohmyposh
这里还是推荐使用winget进行安装
1
winget install JanDeDobbeleer.OhMyPosh -s winget
安装完毕后需要重启一下终端,载入环境变量
2、安装Nerd Fonts字体
主题中可以使用unicode等字符,因此需要安装相应的字体
有两种方式,推荐直接去这个网站下载:https://www.nerdfonts.com/font-downloads 可以直接预览字体,下载对应的压缩包后解压,选中字体文件右键安装即可
另一种方式是使用官方说明中的命令安装:https://ohmyposh.dev/docs/installation/fonts
1
oh-my-posh font install
3、配置字体
在windows terminal中
打开windows terminal,在界面中按下
ctrl+shift+,
或者进入设置界面左下角打开JSON文件
编辑其中的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18{
//...
"profiles":
{
// 修改这里的defaults,增加font
"defaults":
{
// 指定字体font.face
"font":
{
// 我安装的是JetBrainMono字体
"face": "JetBrainsMono Nerd Font"
}
},
//...
}
//...
}
在vscode中配置字体
打开vscode设置,搜索
terminal:font family
将使用的字体写入配置中即可
4、配置ohmyposh
打开PowerShell,输入如下命令:
1
notepad $PROFILE
编辑文本内容后保存
1
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" | Invoke-Expression
修改完毕后执行如下命令使得变更生效
1
. $PROFILE
5、配置主题
默认的主题为
jandedobbeleer
我们可以使用如下命令下载并预览所有的可用主题1
Get-PoshThemes
上述命令执行后会在命令行中显示所有的可用主题,按住ctrl和链接可以打开对应的配置文件
在线查看所有主题的地址为:https://ohmyposh.dev/docs/themes
选择想要使用的配置后,将其配置到
$PROFILE
中即可。例如我使用了1_shell.omp.json则修改为如下:1
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\1_shell.omp.json" | Invoke-Expression
默认情况下的主题,可能也不合自己的喜好,可以自己修改它,具体的语法见官网:https://ohmyposh.dev/docs/configuration/general
自定义配置完毕后,可以将当前主题导出到个人目录:
1
oh-my-posh config export --output ~/.mytheme.omp.json
其他配置
git中文乱码
当使用git status显示中文路径为乱码时,执行以下脚本配置一下
1
git config --global core.quotepath false
配置conda,在ohmyposh中显示当前虚拟环境信息
首先需要安装anaconda,请按官网配置或者搜索相关教程即可
在PowerShell中执行
conda init powershell
初始化conda的配置conda的当前虚拟环境信息会保存到环境变量
CONDA_DEFAULT_ENV
中,因此只需要将它配置到主题文件中即可,在template中配置为.Env.CONDA_DEFAULT_ENV
参考如下:1
2
3
4
5
6
7
8
9
10
11
12
13{
//...
{
"type": "session",
"style": "diamond",
"foreground": "#ff9c3f",
"template": "{{ .UserName }}<#faf88e>[{{ .Env.CONDA_DEFAULT_ENV }}] <#ffffff>on</>",
"properties": {
"display_host": true
}
},
//...
}自定义后的主题预览如下:
- 普通目录
- git目录
配置默认PowerShell控制台字体
在菜单右键或者win+x直接打开PowerShell时的控制台程序中,显示的字体也需要进行配置,否则会显示得不正常,但是这个控制台并不能完美支持Nerd Font,因此这里推荐安装一下
更纱黑体
github地址:https://github.com/be5invis/Sarasa-Gothic/releases安装字体之后,win+x打开powershell控制台,点击左上角图标调出菜单点击属性,在字体标签中修改字体为
等距更纱黑体
配置完毕后可以自行调整字体大小为自己喜欢的,预览如下,缺点是无法显示unicode字符:
我当前所使用的自定义主题文件:
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{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"version": 2,
"console_title_template": "{{ .Folder }}",
"blocks": [
{
"type": "prompt",
"alignment": "left",
"segments": [
{
"type": "status",
"style": "plain",
"foreground": "#A9FFB4",
"foreground_templates": [
"{{ if gt .Code 0 }}#ef5350{{ end }}"
],
"template": " \ue286 ",
"properties": {
"always_enabled": true
}
},
{
"type": "executiontime",
"style": "diamond",
"foreground": "#a9ffb4",
"template": " {{ .FormattedMs }}s <#ffffff>\ue601</>",
"properties": {
"style": "dallas",
"threshold": 0
}
},
{
"type": "root",
"style": "diamond",
"template": " \uf0e7 ",
"properties": {
"root_icon": "\uf292 "
}
},
{
"type": "sysinfo",
"style": "diamond",
"foreground": "#94ffa2",
"template": " <#ffffff>CPU:</> {{ round .PhysicalPercentUsed .Precision }}% "
},
{
"type": "sysinfo",
"style": "diamond",
"foreground": "#81ff91",
"template": "<#ffffff>\ue266</> <#ffffff>RAM:</> {{ (div ((sub .PhysicalTotalMemory .PhysicalFreeMemory)|float64) 1073741824.0) }}/{{ (div .PhysicalTotalMemory 1073741824.0) }}GB "
}
]
},
{
"type": "prompt",
"alignment": "left",
"segments": [
{
"type": "shell",
"style": "diamond",
"foreground": "#faf88e",
"template": " "
},
{
"type": "session",
"style": "diamond",
"foreground": "#ff9c3f",
"template": "{{ .UserName }}<#faf88e>[{{ .Env.CONDA_DEFAULT_ENV }}] <#ffffff>on</>",
"properties": {
"display_host": true
}
},
{
"type": "time",
"style": "diamond",
"foreground": "#bc93ff",
"template": " {{ .CurrentDate | date .Format }} ",
"properties": {
"time_format": "Monday <#ffffff>at</> 3:04 PM"
}
},
{
"type": "git",
"style": "diamond",
"foreground": "#ee79d1",
"template": " {{ .UpstreamIcon }}{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uf046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ",
"properties": {
"branch_icon": "\ue725 ",
"fetch_stash_count": true,
"fetch_status": true,
"fetch_upstream_icon": true,
"fetch_worktree_count": true
}
}
],
"newline": true
},
{
"type": "prompt",
"alignment": "left",
"segments": [
{
"type": "path",
"style": "diamond",
"foreground": "#8edbfa",
"trailing_diamond": "<#8edbfa> > </>",
"template": "{{ .Path }}",
"properties": {
"folder_icon": "\uf07b",
"folder_separator_icon": "/",
"home_icon": "home",
"style": "agnoster_full"
}
}
],
"newline": true
}
],
"transient_prompt": {
"foreground": "#FEF5ED",
"background": "transparent",
"template": " "
}
}