Windows 11 and vscode terminal beautification
0. After beautification
1. Preparations
-
Allow powershell to execute scripts, if not, subsequent execution of the installation command will report an error
- Settings->Privacy and Security->Developer Options->Powershell, click Apply
-
A Nerd Font . The Nerd Font contains many special icons. If Nerd Font is not used, it will be garbled after setting the theme of the terminal.
-
Here I take the
Hasklig
font as an example, the download link . After downloading, you will get aHasklig.zip
file. After unzipping, you can see that it contains a lot of fonts. Directlyctrl + A
, then right-click and select install all fonts -
After the installation is complete, we will get the following 3 fonts
- Hasklug Nerd Font
- Hasklug Nerd Font Mono
- Hasklug NF
-
-
vscode
-
Windows Terminal
- Set the display font of Windows Terminal. If you don't set it, garbled characters will appear after setting the theme later.
- Open Windows Terminal settings, Windows PowerShell -> Appearance
- Find the font you installed earlier, modify it to
Hasklug NF
, and click Save. Restart Windows Terminal to apply the new font
2. Installationoh-my-posh
Execute the following command in Windows Terminal
Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://ohmyposh.dev/install.ps1'))
复制代码
oh-my-posh
3. Apply and customize the theme in the terminal
Execute the following command in Windows Terminal
oh-my-posh init pwsh | Invoke-Expression
复制代码
At this time, oh-my-posh will set a default theme (as long as you see colored fonts, it should be set successfully)
If you want to set other themes, you can execute
Get-PoshThemes
复制代码
View all configurable themes
After executing Get-PoshThemes
the command to output the styles of all themes, it will tell us the paths of all theme files and how to set the theme at the end;
Take my machine as an example, you can see it in the picture above
-
Paths to all theme files:
C:\Users\aifuxi\AppData\Local\Programs\oh-my-posh\themes
-
Command to set theme:
oh-my-posh init pwsh --config C:\Users\aifuxi\AppData\Local\Programs\oh-my-posh\themes/jandedobbeleer.omp.json | Invoke-Expression 复制代码
jandedobbeleer.omp.json
就是主题的配置文件,jandedobbeleer
是主题名。比如我想设置ys
这个主题,只需要把上面命令中的jandedobbeleer.omp.json
改成ys.omp.json
就可以了。在Windows Terminal里执行设置主题的命令,只是临时改变主题,要想每次打开都自动设置主题我们就得编辑个配置文件了。
3.1 编辑配置文件
在Windows Terminal里执行下面命令编辑或新建一个配置文件
notepad $PROFILE
# 如果在path里安装了vscode也可以用下面命令打开
code $PROFILE
复制代码
以我自己为例,我想设置主题为1_shell
这个主题,那么就可以在刚刚打开的配置文件里加上这句话然后保存并重启Windows Terminal
oh-my-posh init pwsh --config C:\Users\aifuxi\AppData\Local\Programs\oh-my-posh\themes/1_shell.omp.json | Invoke-Expression
复制代码
注意:这里的C:\Users\aifuxi\AppData\Local\Programs\oh-my-posh\themes/1_shell.omp.json
这个路径是我本机的路径,每个人的电脑的配置文件路径都是不一样的,请根据实际情况进行修改,不要盲目复制。 这是设置主题为1_shell
的效果,还是挺好看的。
3.2 vscode的设置
修改vscode配置文件settings.json
{
// 代码字体,可根据实际情况进行设置
"editor.fontFamily": "'Hasklug Nerd Font Mono',Menlo, Monaco, 'Courier New', monospace",
// 终端字体,我这里是设置的Hasklug Nerd Font Mono,可根据实际安装的Nerd Font进行设置
"terminal.integrated.fontFamily": "Hasklug Nerd Font Mono",
}
复制代码
4. 安装PSReadLine
PSReadLine:github.com/PowerShell/…
PSReadLine模块取代了 PowerShell 版本 3 及更高版本的命令行编辑体验。它提供:
- 语法着色
- 简单语法错误通知
- 良好的多线体验(编辑和历史)
- 可定制的键绑定
- Cmd 和 emacs 模式(都没有完全实现,但都可以使用)
- 许多配置选项
- Bash 样式完成(在 Cmd 模式下可选,在 Emacs 模式下默认)
- Bash/zsh 风格的交互式历史搜索 (CTRL-R)
- Emacs yank/kill ring
- 基于 PowerShell 令牌的“单词”移动和杀死
- 撤销重做
- 自动保存历史记录,包括跨实时会话共享历史记录
- 通过 Ctrl+Space 完成“菜单”完成(有点像 Intellisense,用箭头选择完成)
- “开箱即用”的体验意味着 PowerShell 用户非常熟悉 - 不需要学习任何新的击键。
上面是github里的介绍,但其实我们主要用到PSReadLine的功能就是
- 自动保存历史记录,敲过一个命令后,后面只需要敲前几个字母就能提示出命令,按【→】键就可以自动补全命令
- 语法着色 PSReadLine的作用就和oh-my-zsh里面的那个autocomplete的那个插件差不多,用来提示和补全命令的 比如这里我敲
gi
,就能显示出以前敲过的git log
命令,然后按【→】键就可以补全命令了,对于经常敲命令的人来说还是非常有用的,可以提高开发效率。
4.1 安装PSReadLine
- 以管理员身份运行Windows Terminal,执行下面命令:
Install-Module PSReadLine -Force
复制代码
没有以管理员身份运行Windows Terminal时会报错
4.2 添加配置项到配置文件
- 在Windows Terminal里执行下面命令编辑配置文件
notepad $PROFILE
# 如果在path里安装了vscode也可以用下面命令打开
code $PROFILE
复制代码
- 添加下面几行配置
Set-PSReadLineOption -PredictionSource History # 设置预测文本来源为历史记录
Set-PSReadlineKeyHandler -Key Tab -Function Complete # 设置 Tab 键补全
Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function MenuComplete # 设置 Ctrl+d 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward # 设置向下键为前向搜索历史纪录
复制代码
- 保存后,关闭Windows Terminal后再重新打开,验证配置是否生效
安装时遇到的问题
- 为什么不是执行
Install-Module -Name PSReadLine -AllowPrerelease
来安装PSReadLine? 其实开始的时候我是用这条命令来安装的,是根据 @i树 兄弟提供的链接来的, 但是报错了 然后我去看了下github的安装文档找到了原因。 Install-Module -Name PSReadLine -AllowPrerelease
中-AllowPrerelease
是PowerShellGet
这个模块提供的能力,首先得安装PowerShellGet
# 先安装PowerShellGet
Install-Module -Name PowerShellGet -Force
# 然后再这条命令安装PSReadLine
Install-Module PSReadLine -AllowPrerelease -Force
复制代码
5. 隐藏烦人的copyright
每次打开Windows Terminal都会出现烦人的copyright
Windows PowerShell 版权所有(C) Microsoft Corporation。保留所有权利。
安装最新的 PowerShell,了解新功能和改进!aka.ms/PSWindows
We can add -nologo
parameter to hide this text
5.1 Windows Terminal
Open Windows Terminal settings, Windows PowerShell -> Command Line, add on the path-nologo
, then save and restart Windons Terminal to see the annoying copyright prompt.
5.2 vscode
Modify the vscode configuration file settings.json
and add terminal.integrated.profiles.windows
this field
{
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"args": ["-noLogo"]
}
},
}
复制代码
6. OK, you're done
7. Reference Links
- stackoverflow.com/questions/4… , hides the copyright prompt every time powershell starts
- ohmyposh.dev/docs/instal… , installation of oh-my-posh
- ohmyposh.dev/docs/instal… , applying oh-my-posh
- ohmyposh.dev/docs/themes , oh-my-posh theme