安装Oh My Posh
1.打开 PowerShell 提示符并运行以下命令:
Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://ohmyposh.dev/install.ps1'))
//或者
Install-Module oh-my-posh
2.安装字体 (必须否则主题会乱码)
oh-my-posh font install
3.配置终端启动初始化oh-my-posh
#用记事本编辑 profile 文件
notepad $PROFILE
#当上述命令出错时,请确保先创建配置文件。创建profile文件
New-Item -Path $PROFILE -Type File -Force
#终端启动初始化oh-my-posh
oh-my-posh init pwsh | Invoke-Expression
4.设置主题
#显示当前已有主题
Get-PoshThemes
# 修改 profile 文件 配置使用主题初始化启动
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\unicorn.omp.json" | Invoke-Expression
#c重新加载 profile 文件
.$PROFILE
4.提示自动补全
# profile 文件中添加
# 自动建议 并设置预测文本来自历史记录
# Import-Module PSReadLine 默认已包含
# Set-PSReadLineOption -ShowToolTips 默认开启
# Tab 当按Tab键时,显示所有选项的导航菜单
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
# 设置列表视图模式 默认InlineView
Set-PSReadLineOption -PredictionViewStyle ListView
# 设置预测文本来源为历史记录 默认设置 HistoryAndPlugin 7.2 之前 History
#Set-PSReadLineOption -PredictionSource History
# 每次回溯输入历史,光标定位于输入内容末尾
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
# 提示文本颜色
Set-PSReadLineOption -Colors @{ InlinePrediction = '#8A0303'}
# Set-PSReadLineOption -Colors @{ InlinePrediction = '#2F7004'}
# Set-PSReadLineOption -Colors @{ InlinePrediction = "$([char]0x1b)[36;7;238m"}
# Set-PSReadLineOption -Colors @{ InlinePrediction = "$([char]0x1b)[38;5;238m"} 恢复默认
# 方向键自动补全
# Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
# Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
# 下一条自动补全 History记录 line模式中相反
Set-PSReadlineKeyHandler -Key Ctrl+n -Function HistorySearchForward
# 上一条自动补全 History记录
Set-PSReadlineKeyHandler -Key Ctrl+p -Function HistorySearchBackward
####-----相关命令
# 获取所有键映射
# Get-PSReadLineKeyHandler
# 获取 PSReadLineOption配置
# Get-PSReadLineOption
4.git自动补全posh-git: posh-git 包含一组强大的 PowerShell 脚本,提供了 Git 和 PowerShell 的集成
#请以管理员身份运行 PowerShell 并执行
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm
#安装 直接 Install-Module posh-git 没有影响
Install-Module posh-git -Scope CurrentUser -Force
# profile 文件中添加 posh-git 模块
Add-PoshGitToProfile #或者直接 编辑 profile 文件