windows使用oh-my-posh美化powershell命令提示符
需要提前安装的软件:windows terminal、git。
- 安装oh-my-posh,需要保证git命令可用,这里默认git已经安装好,之后用下面命令安装oh-my-posh:
Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser
如果安装过程oh-my-posh无法下载下来,则采取手动下载,添加 -Verbose 来显示安装过程的log
Install-Module oh-my-posh -Scope CurrentUser -Verbose
然后观察下面的输出,找到要安装的 oh-my-posh 的下载地址,例如我现在安装的就是3.164.0版本的,地址是
https://www.powershellgallery.com/api/v2/package/oh-my-posh/3.164.0
然后直接浏览器下载即可,下载的文件是 .nupkg 格式的,修改后缀为 .zip 解压。然后将解压后的文件夹放到 powershell 的 modules 文件夹下,比如说,我这里要为powershell7安装oh-my-posh,由于这里是对当前用户安装的,powershell的实际安装路径是这个:
C:\Users\name\Documents\WindowsPowerShell
(如果对所有用户安装需要找到powershell的系统目录,一般是C:\Program Files\WindowsPowerShell)
解压的文件夹放置的路径就为:
C:\Users\name\Documents\WindowsPowerShell\Modules\oh-my-posh\3.164.0
(注意一定是 \Modules\模块名\模块的版本号)。
之后输入下面的命令可以看到oh-my-posh安装完毕。
Import-Module posh-git
Import-Module oh-my-posh
module
- 在profile文件中写入下面内容即可启用oh-my-posh主题
Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt Paradox //paradox为主题名称
- 下载Nerd Fonts字体
通过上面的步骤启用oh-my-posh后会有很多乱码,很难看,因为没有安装专用的字体。许多网上教程是让用下面的方式下载powerline字体,但是powerline字体是不适用于oh-my-posh的。
# 下载
git clone https://github.com/powerline/fonts.git --depth=1
# 安装
cd fonts
./install.ps1
# 安装完毕删除 fonts 文件夹即可
用上面的方法下载字体并设置使用对应的字体后乱码有所改善,但是任然有部分乱码,并且无论切换什么powerline字体都无法继续改善,如下图:
通过查看oh-my-posh的官方文档得知oh-my-posh是使用Nerd Fonts进行设计的,文档地址如下:
https://ohmyposh.dev/docs/fonts
文档中给出了Nerd Fonts的下载链接,但是好像需要翻墙才能打开,链接地址如下:
https://www.nerdfonts.com/
这里下载了字体SourceCodePro,并在windows terminal的settings.json中设置使用该字体,即设置"fontFace": “SauceCodePro Nerd Font”,下面为windows terminal的配置文档。
{
"$schema": "https://aka.ms/terminal-profiles-schema",
// Add custom actions and keybindings to this array.
// To unbind a key combination from your defaults.json, set the command to "unbound".
// To learn more about actions and keybindings, visit https://aka.ms/terminal-keybindings
"actions":
[
// Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
// These two lines additionally bind them to Ctrl+C and Ctrl+V.
// To learn more about selection, visit https://aka.ms/terminal-selection
{
"command":
{
"action": "copy",
"singleLine": false
},
"keys": "ctrl+c"
},
{
"command": "paste",
"keys": "ctrl+v"
},
// Press Ctrl+Shift+F to open the search box
{
"command": "find",
"keys": "ctrl+shift+f"
},
// Press Alt+Shift+D to open a new pane.
// - "split": "auto" makes this pane open in the direction that provides the most surface area.
// - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
// To learn more about panes, visit https://aka.ms/terminal-panes
{
"command":
{
"action": "splitPane",
"split": "auto",
"splitMode": "duplicate"
},
"keys": "alt+shift+d"
}
],
"copyFormatting": "none",
"copyOnSelect": false,
"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"profiles":
{
"defaults": {
//适用所有配置文件的设置
"startingDirectory": "%USERPROFILE%", //起始目录
//"icon": "C:\\Users\\DustinWinFiles\\Pictures\\Java.png", // 图标,建议每一类在list中单独设置
//"tabTitle": "XXX", //自定义选项卡标题,设置了所有的tab页都会是这个名字
"suppressApplicationTitle": true, //禁止shell更改标题
"fontFace": "Fira Code", //字体
"fontSize": 12, //字体大小
"fontWeight": "normal", // 字体粗细
"padding": "8, 8, 8, 8", // 填充(内容距离界面的内部距离)
"antialiasingMode": "cleartype", // 消除文本锯齿
"cursorShape": "vintage", // 光标形状
"cursorColor": "#11EE11", // 光标颜色
"cursorHeight": 100, // 光标高度("cursorShape"设置为"vintage"时可用)
"altGrAliasing": true, // AltGr别名
//"colorScheme": "Ubuntu", // 配色方案 在单个中定义
"colorScheme": "One Half Dark", // 配色方案
//"selectionBackground": "#0000e6", //所选内容的背景色(选用"colorScheme"内的)
"useAcrylic": true, //启用 acrylic(亚克力特效)
"acrylicOpacity": 0.7, //Acrylic 不透明度
//"backgroundImage": "C:\\Users\\DustinWinFiles\\Pictures\\colorful.jpg", //背景图像设置
//"backgroundImageStretchMode": "uniformToFill", // 背景图像拉伸模式
// "backgroundImageAlignment": "center", //背景图像对齐
//"backgroundImageOpacity": 0.5, //背景图像不透明度
"scrollbarState": "visible", //滚动条可见性
"snapOnInput": true, //键入时滚动到输入行
"historySize": 9001, //历史记录大小
"closeOnExit": "graceful", //退出时配置文件的关闭方式(输入exit退出命令窗口)
//"experimental.retroTerminalEffect": false // 怀旧式终端效果
"alwaysOnTop": true, //始终处于顶部模式
},
"list":
[
{
"commandline": "powershell.exe",
//"fontFace": "DejaVu Sans Mono for Powerline",
"fontFace": "SauceCodePro Nerd Font", //设置使用Nerd Fonts
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"hidden": false,
"colorScheme": "Solarized Darcula",
"name": "Windows PowerShell"
},
{
"commandline": "cmd.exe",
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"hidden": false,
"name": "Command Prompt"
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
},
{
"fontFace": "SauceCodePro Nerd Font",
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"hidden": false,
"name": "PowerShell",
"source": "Windows.Terminal.PowershellCore"
}
]
},
"schemes":
[
{
"name": "Solarized Dark",
"black": "#002831",
"red": "#d11c24",
"green": "#738a05",
"yellow": "#a57706",
"blue": "#2176c7",
"purple": "#c61c6f",
"cyan": "#259286",
"white": "#eae3cb",
"brightBlack": "#475b62",
"brightRed": "#bd3613",
"brightGreen": "#475b62",
"brightYellow": "#536870",
"brightBlue": "#708284",
"brightPurple": "#5956ba",
"brightCyan": "#819090",
"brightWhite": "#fcf4dc",
"background": "#001e27",
"foreground": "#708284"
},
{
"name": "One Half Dark",
"black": "#2e3436",
"red": "#cc0000",
"green": "#4e9a06",
"yellow": "#c4a000",
"blue": "#3465a4",
"purple": "#75507b",
"cyan": "#06989a",
"white": "#d3d7cf",
"brightBlack": "#555753",
"brightRed": "#ef2929",
"brightGreen": "#8ae234",
"brightYellow": "#fce94f",
"brightBlue": "#729fcf",
"brightPurple": "#ad7fa8",
"brightCyan": "#34e2e2",
"brightWhite": "#eeeeec",
"background": "#300a24", //背景色
"foreground": "#eeeeec", //前景色
},
{
"name": "Solarized Darcula",
"black": "#25292a",
"red": "#f24840",
"green": "#629655",
"yellow": "#b68800",
"blue": "#2075c7",
"purple": "#797fd4",
"cyan": "#15968d",
"white": "#d2d8d9",
"brightBlack": "#25292a",
"brightRed": "#f24840",
"brightGreen": "#629655",
"brightYellow": "#b68800",
"brightBlue": "#2075c7",
"brightPurple": "#797fd4",
"brightCyan": "#15968d",
"brightWhite": "#d2d8d9",
"background": "#3d3f41",
"foreground": "#d2d8d9"
},
{
"background": "#0C0C0C",
"black": "#0C0C0C",
"blue": "#0037DA",
"brightBlack": "#767676",
"brightBlue": "#3B78FF",
"brightCyan": "#61D6D6",
"brightGreen": "#16C60C",
"brightPurple": "#B4009E",
"brightRed": "#E74856",
"brightWhite": "#F2F2F2",
"brightYellow": "#F9F1A5",
"cursorColor": "#FFFFFF",
"cyan": "#3A96DD",
"foreground": "#CCCCCC",
"green": "#13A10E",
"name": "Campbell",
"purple": "#881798",
"red": "#C50F1F",
"selectionBackground": "#FFFFFF",
"white": "#CCCCCC",
"yellow": "#C19C00"
},
{
"background": "#012456",
"black": "#0C0C0C",
"blue": "#0037DA",
"brightBlack": "#767676",
"brightBlue": "#3B78FF",
"brightCyan": "#61D6D6",
"brightGreen": "#16C60C",
"brightPurple": "#B4009E",
"brightRed": "#E74856",
"brightWhite": "#F2F2F2",
"brightYellow": "#F9F1A5",
"cursorColor": "#FFFFFF",
"cyan": "#3A96DD",
"foreground": "#CCCCCC",
"green": "#13A10E",
"name": "Campbell Powershell",
"purple": "#881798",
"red": "#C50F1F",
"selectionBackground": "#FFFFFF",
"white": "#CCCCCC",
"yellow": "#C19C00"
},
{
"background": "#282C34",
"black": "#282C34",
"blue": "#61AFEF",
"brightBlack": "#5A6374",
"brightBlue": "#61AFEF",
"brightCyan": "#56B6C2",
"brightGreen": "#98C379",
"brightPurple": "#C678DD",
"brightRed": "#E06C75",
"brightWhite": "#DCDFE4",
"brightYellow": "#E5C07B",
"cursorColor": "#FFFFFF",
"cyan": "#56B6C2",
"foreground": "#DCDFE4",
"green": "#98C379",
"name": "One Half Dark",
"purple": "#C678DD",
"red": "#E06C75",
"selectionBackground": "#FFFFFF",
"white": "#DCDFE4",
"yellow": "#E5C07B"
},
{
"background": "#FAFAFA",
"black": "#383A42",
"blue": "#0184BC",
"brightBlack": "#4F525D",
"brightBlue": "#61AFEF",
"brightCyan": "#56B5C1",
"brightGreen": "#98C379",
"brightPurple": "#C577DD",
"brightRed": "#DF6C75",
"brightWhite": "#FFFFFF",
"brightYellow": "#E4C07A",
"cursorColor": "#4F525D",
"cyan": "#0997B3",
"foreground": "#383A42",
"green": "#50A14F",
"name": "One Half Light",
"purple": "#A626A4",
"red": "#E45649",
"selectionBackground": "#FFFFFF",
"white": "#FAFAFA",
"yellow": "#C18301"
},
{
"background": "#002B36",
"black": "#002B36",
"blue": "#268BD2",
"brightBlack": "#073642",
"brightBlue": "#839496",
"brightCyan": "#93A1A1",
"brightGreen": "#586E75",
"brightPurple": "#6C71C4",
"brightRed": "#CB4B16",
"brightWhite": "#FDF6E3",
"brightYellow": "#657B83",
"cursorColor": "#FFFFFF",
"cyan": "#2AA198",
"foreground": "#839496",
"green": "#859900",
"name": "Solarized Dark",
"purple": "#D33682",
"red": "#DC322F",
"selectionBackground": "#FFFFFF",
"white": "#EEE8D5",
"yellow": "#B58900"
},
{
"background": "#FDF6E3",
"black": "#002B36",
"blue": "#268BD2",
"brightBlack": "#073642",
"brightBlue": "#839496",
"brightCyan": "#93A1A1",
"brightGreen": "#586E75",
"brightPurple": "#6C71C4",
"brightRed": "#CB4B16",
"brightWhite": "#FDF6E3",
"brightYellow": "#657B83",
"cursorColor": "#002B36",
"cyan": "#2AA198",
"foreground": "#657B83",
"green": "#859900",
"name": "Solarized Light",
"purple": "#D33682",
"red": "#DC322F",
"selectionBackground": "#FFFFFF",
"white": "#EEE8D5",
"yellow": "#B58900"
},
{
"background": "#000000",
"black": "#000000",
"blue": "#3465A4",
"brightBlack": "#555753",
"brightBlue": "#729FCF",
"brightCyan": "#34E2E2",
"brightGreen": "#8AE234",
"brightPurple": "#AD7FA8",
"brightRed": "#EF2929",
"brightWhite": "#EEEEEC",
"brightYellow": "#FCE94F",
"cursorColor": "#FFFFFF",
"cyan": "#06989A",
"foreground": "#D3D7CF",
"green": "#4E9A06",
"name": "Tango Dark",
"purple": "#75507B",
"red": "#CC0000",
"selectionBackground": "#FFFFFF",
"white": "#D3D7CF",
"yellow": "#C4A000"
},
{
"background": "#FFFFFF",
"black": "#000000",
"blue": "#3465A4",
"brightBlack": "#555753",
"brightBlue": "#729FCF",
"brightCyan": "#34E2E2",
"brightGreen": "#8AE234",
"brightPurple": "#AD7FA8",
"brightRed": "#EF2929",
"brightWhite": "#EEEEEC",
"brightYellow": "#FCE94F",
"cursorColor": "#000000",
"cyan": "#06989A",
"foreground": "#555753",
"green": "#4E9A06",
"name": "Tango Light",
"purple": "#75507B",
"red": "#CC0000",
"selectionBackground": "#FFFFFF",
"white": "#D3D7CF",
"yellow": "#C4A000"
},
{
"background": "#000000",
"black": "#000000",
"blue": "#000080",
"brightBlack": "#808080",
"brightBlue": "#0000FF",
"brightCyan": "#00FFFF",
"brightGreen": "#00FF00",
"brightPurple": "#FF00FF",
"brightRed": "#FF0000",
"brightWhite": "#FFFFFF",
"brightYellow": "#FFFF00",
"cursorColor": "#FFFFFF",
"cyan": "#008080",
"foreground": "#C0C0C0",
"green": "#008000",
"name": "Vintage",
"purple": "#800080",
"red": "#800000",
"selectionBackground": "#FFFFFF",
"white": "#C0C0C0",
"yellow": "#808000"
}
]
}
设置完成后字符显示正常,效果如下: