配置流程
以下安装命令均已管理员权限打开PowerShell执行
配置 PowerShell
允许运行 Install-Module 脚本
Set-ExecutionPolicy RemoteSigned
# 更新 PSReadLine,以支持自动补全
Install-Module PSReadLine -Force
# 创建 PowerShell 初始化脚本
notepad $profile
# 安装插件
Install-Module posh-git
Install-Module Terminal-Icons
OhMyPosh
安装OhMyPosh
winget install JanDeDobbeleer.OhMyPosh -s winget
安装OhMyPosh主题:
git clone https://github.com/JanDeDobbeleer/oh-my-posh
只需保留 theme 文件夹
测试安装
oh-my-posh init pwsh | Invoke-Expression
如果终端界面变成 Oh My Posh 的默认主题了,就表示安装成功啦
完整配置文件
执行notepad $profile
并添加以下内容:
# 初始化 Oh My Posh 并应用主题
oh-my-posh init pwsh --config C:\oh_my_posh\themes\1_shell.omp | Invoke-Expression
# 导入 posh-git 模块,提供 Git 状态提示
Import-Module posh-git
# 导入 Terminal-Icons 模块,提供文件和目录的图标支持
Import-Module Terminal-Icons
# 设置 PowerShell 的自动补全选项
Set-PSReadLineOption -PredictionSource History
# 设置 Tab 键为菜单补全功能
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
# 设置预测源为历史记录,并将预测视图样式设置为列表视图
Set-PSReadLineOption -PredictionSource History -PredictionViewStyle ListView
# 自定义提示符颜色
# $host.UI.RawUI.ForegroundColor = "Cyan" # 设置前景色为青色
# $host.UI.RawUI.BackgroundColor = "Black" # 设置背景色为黑色
# function prompt {
# $time = Get-Date -Format "HH:mm:ss"
# $user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
# "$time [$user] > "
# }
Set-PSReadLineOption -EditMode Emacs # Emacs 风格的键绑定
Set-PSReadLineOption -HistorySearchCursorMovesToEnd # 历史搜索时光标移动到行尾
function fzf-file {
Get-ChildItem | fzf
}
function fzf-command {
Get-History | fzf
}
其中 <C:\oh_my_posh\themes\1_shell.omp> 是主题文件夹中 JSON 文件的路径,可以查找自己喜欢的主题:Themes | Oh My Posh
重新打开终端,查看效果。