Oh My Posh 是一款可深度定制的全平台终端工具,支持 Windows、GNU/Linux(WSL)、macOS 三个系统上的 PowerShell、bash、zsh 等终端。
安装并不复杂,非常的简单。
下面的安装方式任选其一,适用于 Windows 和 WSL。
Scoop
scoop install https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/oh-my-posh.json
Chocolatey:
cinst oh-my-posh -y
Winget:
winget install JanDeDobbeleer.OhMyPosh
brew tap jandedobbeleer/oh-my-posh && brew install oh-my-posh
#安装
sudo wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/posh-linux-amd64 -O /usr/local/bin/oh-my-posh
sudo chmod +x /usr/local/bin/oh-my-posh
# 下载主题文件
mkdir ~/.poshthemes
wget https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/themes.zip -O ~/.poshthemes/themes.zip
unzip ~/.poshthemes/themes.zip -d ~/.poshthemes
chmod u+rw ~/.poshthemes/*.json
rm ~/.poshthemes/themes.zip
不同的终端配置文件位置不同。可以使用 oh-my-posh --print-shell 命令查看配置文件的所在目录。
PowerShell 配置文件有内置的变量 Profile。键入Profile终端会显示配置文件的路径。编辑此文件,新增如下代码,重启终端即可。
oh-my-posh --init --shell pwsh --config 主题路径 | Invoke-Expression
Bash 的配置文件一般是~/.bashrc 或者~/.profile,同上文一样,编辑对应的文件,若没有,则新建一个,新增下面一行代码并重启终端1:
eval "$(oh-my-posh --init --shell bash --config 主题路径)"
#更改主题
vi ~/.bashrc
-> 添加如下代码
eval "$(oh-my-posh --init --shell bash --config ~/.poshthemes/*.json)"
Zsh 的配置文件为~/.zshrc,需要新增的代码需要将bash改为zsh:
eval "$(oh-my-posh --init --shell zsh --config 主题路径)"
在 Windows 的 WSL 上的 Ubuntu .bashrc 文件应该添加:
eval "$(oh-my-posh-wsl --init --shell bash --config /mnt/c/users/用户名/AppData/Local/Program
不同安装方式得到的主题目录路径不同,分别如下:
#Windows Scoop
~\scoop\apps\oh-my-posh\current\themes\wopian.omp.json
#Windows Choco/Winget
~\AppData\Local\Programs\oh-my-posh\themes\wopian.omp.json
#macOS Brew
~/.poshthemes/wopian.omp.json
#GNU/Linux 命令行
~/.poshthemes/wopian.omp.json
配置1_shell主题
#打开配置文件
notepad $PROFILE
#添加下面的配置
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\1_shell.omp.json" | Invoke-Expression
主题介绍:https://ohmyposh.dev/docs/themes
Oh my posh 主题有一些特殊的字符,例如表示系统类型的徽标、GitHub 标志,这些字符需要特殊的字体支持,所以,需要提前安装配置的。
可以访问 Nerd Fonts 的下载界面(https://www.nerdfonts.com/font-downloads),从中任意选择一个字体包,下载后解压,再安装进系统即可。Oh my posh 官方推荐 Meslo LGM NF。
#安装字体
oh-my-posh font install
Windows Terminal:进入设置 > 配置文件 > 默认值 & 子配置文件 > 外观,设定字体为 Nerd Fonts。这一设置对 WSL 和远程主机都生效。
Windows终端的一些配置:https://learn.microsoft.com/zh-cn/windows/terminal/tutorials/custom-prompt-setup
总的来讲,Oh My Posh 是一款可定制的全平台终端工具。
注:如需转载须保留文首公众号名片,其它一律视为非授权转载行为。