此方法在Windows 10 版本1909上通过Microsoft Store中安装的
Windows Terminal
测试通过,其它版本可能有些许区别。
Windows Terminal
从profiles.json
中读取配置,初始状态下配置文件很“简陋”:
// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
"$schema": "https://aka.ms/terminal-profiles-schema",
"defaultProfile": "{XXX}",
"profiles": [
{
// Make changes here to the powershell.exe profile
"guid": "{XXX}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false
},
{
// Make changes here to the cmd.exe profile
"guid": "{XXX}",
"name": "cmd",
"commandline": "cmd.exe",
"hidden": false
},
{
"guid": "{XXX}",
"hidden": false,
"name": "Debian",
"source": "Windows.Terminal.Wsl",
"colorScheme": "Builtin Solarized Light"
},
{
"guid": "{XXX}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
}
],
// Add custom color schemes to this array
"schemes": [],
// Add any keybinding overrides to this array.
// To unbind a default keybinding, set the command to "unbound"
"keybindings": []
}
我的配置文件的路径在
~\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\profiles.json
,或者也可以通过Windows Terminal
页面上点击下拉箭头内的Settings打开配置文件(也可以通过快捷键Ctrl+,
打开配置文件)。
可以看到,如果想要添加主题,只需要在schemes
数组内添加相应主题即可,主题的格式如下:
{
"name": "lwz_solarized_light",
"black": "#073642",
"red": "#dc322f",
"green": "#ffe79e",
"yellow": "#b58900",
"blue": "#268bd2",
"purple": "#d33682",
"cyan": "#2aa198",
"white": "#eee8d5",
"brightBlack": "#002b36",
"brightRed": "#cb4b16",
"brightGreen": "#00bc00",
"brightYellow": "#657b83",
"brightBlue": "#949800",
"brightPurple": "#6c71c4",
"brightCyan": "#93a1a1",
"brightWhite": "#fdf6e3",
"background": "#ffffff",
"foreground": "#000000",
"selectionBackground": "#bfbfbf"
}
上面的主题是我根据solarized_light
主题修改后的我个人比较喜欢的主题配置,当然大家也可以根据自己的喜好进行调整,添加多个主题注意将名字替换一下。
使用主题非常方便,只要在profiles
数组对应项中添加一行colorScheme
,其值是上面添加主题的name
字段值:
"profiles": [
{
"guid": "{XXX}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false,
"colorScheme": "Builtin Solarized Light"
}
]
上面提到如何添加主题,那对于我们这些不懂配色的怎么办呢?我们可以在https://github.com/mbadolato/iTerm2-Color-Schemes/tree/master/windowsterminal中找到超多主题,我们可以将相应的主题文件内容按照上文添加主题所述添加进入配置文件内即可。
当然你也可以通过脚本将这些主题获取到,然后写入配置文件内,如https://andrewpla.dev/Adding-New-Color-Schemes-To-Windows-Terminal/一文就提供了一个PowerShell脚本来做这个工作。大家只需要将脚本保存为ps1文件,然后再PowerShell终端内执行此文件即可,或者将脚本内容直接粘贴到PowerShell终端内执行。
如果在PowerShell内执行脚本时出现不允许执行脚本的错误,可以通过管理员权限打开PowerShell窗口执行:
PS C:\WINDOWS\system32> Set-ExecutionPolicy RemoteSigned
然后在执行策略更改提示询问处输入A即可启用PowerShell执行脚本的能力,然后重开一个PowerShell窗口再次执行上面的PowerShell脚本。
如果发现主题更改之后,光标消失了,这是因为默认光标颜色和主题背景颜色很接近,这时候可以通过修改光标颜色解决:
"profiles": [
{
"guid": "{XXX}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false,
"colorScheme": "Builtin Solarized Light",
"cursorColor": "#073642" // 更改一个和背景色对比度比较高的颜色
}
]