MOFASHY

Live Is Life

终端美化之 Windows 篇

安装 Windows Terminal

Windows 终端程序是一款新式、快速、高效、强大且高效的终端应用程序,适用于命令行工具和命令提示符,PowerShell 和 WSL 等 Shell 用户。可以通过配置美化样式。打开 Windows 应用商城,安装 Windows Terminal

安装 oh-my-posh

oh-my-posh 是一个让你的命令行提示更美观的工具。oh-my-posh 广泛支持Windows、macOS、Linux等平台。安装好 oh-my-posh 后,会在安装目录下找到下面的文件和目录:

1
2
3
4
5
6
7
# 文件目录结构和官网描述有些出入
.
└─oh-my-posh
├─bin
│ └─oh-my-posh.exe
└─themes
└─M365Princess.omp.json
1
2
3
4
5
6
7
8
9
- `oh-my-posh` - Windows可执行文件或目录
- `themes` - 主题目录

## 安装主题

下载 oh-my-posh 主题,下载成功后的主题文件位于:`C:\Users\<your_name>\AppData\Local\Programs\oh-my-posh\themes`。

```powershell
Get-PoshThemes

创建/编辑PowerShell配置文件指定主题。

1
2
3
notepad $PROFILE
# 当上述命令出现错误时,请确保先创建配置文件
New-Item -Path $PROFILE -Type File -Force

在打开的文件中添加如下内容:

1
2
# 配置你喜欢的主题,如:M365Princess.omp.json
oh-my-posh init pwsh --config 'C:/Users/Posh/M365Princess.omp.json' | Invoke-Expression

安装字体

oh-my-posh 这设计为使用 Nerd 字体,为了图标的正常显示,还需要安装 Nerd 字体,并配置终端以使用它。

1
2
oh-my-posh font install
# 根据提示先择喜欢的 Nerd 字体,博主选择的是 Meslo 字体

安装好 Nerd 字体后,修改 Windows 终端设置(默认快捷方式:CTRL + SHIFT + ,)。在settings.json文件中设置默认字体:

1
2
3
4
5
6
7
8
9
10
11
12
{
"profiles":
{
"defaults":
{
"font":
{
"face": "MesloLGM Nerd Font"
}
}
}
}

使用 Visual Studio Code 时,还需要配置集成终端以使用 Nerd 字体。这可以通过更改终端设置中的值来完成(默认快捷方式:CTRL + ,)。选择Users,搜索Integrated: Font Family

如果使用的是基于 JSON 的设置,则需要更新该值。书字体的例子:terminal.integrated.fontFamilyMesloLGM Nerd Font

1
"terminal.integrated.fontFamily": "MesloLGM Nerd Font"

显示文件和文件夹图标

Terminal-Icons 是一个 PowerShell 模块,它会添加在 Windows 终端中显示文件或文件夹时可能缺少的文件和文件夹图标,并基于名称或扩展名查找相应的图标。 它尝试将图标用于已知文件/文件夹,但如果找不到此内容,则会回滚到通用文件或文件夹图标。

若要使用 PowerShell 安装 Terminal-Icons,请使用以下命令:

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

编辑$profile,添加以下内容:

1
Import-Module -Name Terminal-Icons

智能提示和自动补全

PSReadLine 是一个 PowerShell 模块。借助它可以实现语法高亮和错误提示,以及基于历史输入的智能提示和自动补全等功能,从而获得更好的终端使用体验。

若要使用 PowerShell 安装 PSReadLine,请使用以下命令:

1
Install-Module -Name PSReadLine -AllowClobber -Force

编辑$profile文件,启用智能提示和自动补全,需添加如下内容:

1
2
3
4
5
6
7
8
9
10
# Shows navigable menu of all options when hitting Tab
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete

# Autocompletion for arrow keys
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

# auto suggestions
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History