前言
Windows 下的终端使用体验确实说不上好,尝试了许多组合也感觉都不顺手。这次试试微软亲自出的 Windows terminal,再加上 oh-my-posh 插件。
正文
安装 Windows terminal
Windows terminal 提供多种安装方式,你可以在 Microsoft Store 中安装;可以自行在 Github[1] 上下载安装程序;虽然官方文档未提及,但是你也可以用 Scoop 安装:
scoop install windows-terminal
安装 oh-my-posh
oh-my-posh[2] 是针对 Windows PowerShell 设计的主题引擎,类似于 zsh 和 Oh My Zsh 的关系。它可以让你的命令行更强大。
打开 PowerShell,输入以下命令安装:
Install-Module posh-git -Scope CurrentUser Install-Module oh-my-posh -Scope CurrentUser
安装完成之后还更改 PowerShell 的用户配置,让它每次启动的时候自动加载 oh-my-posh 插件;执行以下命令:
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force } notepad $PROFILE
不出意外的话接下来会出现记事本,你需要在里面写上以下内容:
Import-Module posh-git Import-Module oh-my-posh Set-Theme Paradox
保存该文件,重新打开 PowerShell 你就能看到一个发生改变了的 PowerShell 。
oh-my-posh 提供了几组预设的主题供你选择,你可以通过Set-Theme
命令来更改当前主题,可选的值有 Agnoster/Paradox/Sorin/Darkblood/Avit/Honukai/Fish/Robbyrussell
。
这个主题设置只在当前会话生效,为了每次启动 PowerShell 的时候都能加载你想要的主题,你还需要修改以下用户配置文件,就像上一个步骤中做的一样,只需要修改Set-Theme
命令的部分即可。
用上微软的 Cascadia Code 字体
或许你已经发现了有些字符显示不正常,那是因为默认的字体没有内嵌这些字符的形状,你还需要换一个字体。
微软的另一个开源项目,Cascadia Code[3] 就是一款适用于终端和代码展示的字体,你可以使用 scoop 来安装它;安装字体的时候需要使用到管理员权限,你可以使用sudo
命令,或者用管理员权限重新打开 PowerShell,再执行安装。
在安装之前,你可能需要添加一下nerd-fonts
仓库,因为字体软件的元信息都存放于这个仓库中。
scoop bucket add nerd-fonts
在 Scoop 的仓库中存在着好几个版本的 Cascadia Code,其中:
Cascadia-Code
是原版不带 Powerline 字符的;Cascadia-PL
是微软出品的,自带 Powerline 字符的;CascadiaCode-NF
是 Nerd Fonts[4] 项目出品的,后期补丁了大量 Powerline 字符。
没有特殊需求的话可以选择官方出的轻量级的Cascadia-PL
,执行以下命令:
sudo scoop install Cascadia-PL
安装完成之后还需设置以下,指定 Windows terminal 使用该字体。
目前 Windows terminal 没有专门的设置界面,所有的配置项都在profiles.json
配置文件里;在配置文件的对应地方设置fontFace
的值为Cascadia Code PL
:
// ... "defaults": { // Put settings here that you want to apply to all profiles "fontFace": "Cascadia Code PL" }, // ...
保存配置文件后就能使配置文件生效;此时所有字符应该能正常显示了。
集成 Git Bash
不知道有啥用,就想折腾一下。
首先将 Git 安装目录下的bin
目录加入Path
环境变量;然后修改 Windows terminal 的配置文件,在profiles
->list
中新增一条配置:
// ... "list": [ { "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff4411}", "name": "Git Bash", "commandline": "bash.exe", "hidden": false, "startingDirectory" : "%USERPROFILE%" } ] // ...
注意 JSON 的语法格式,在列表中不要少加或者多加逗号。保存配置文件之后就能在 Windows terminal 中快捷启动 Git Bash 了。
后记
新的 Windows terminal 使用体验还是挺不错的,颜值也 OK,折腾一下也挺好用的。
参考资料
- microsoft/terminal: The new Windows Terminal and the original Windows console host, all in the same place!
- JanDeDobbeleer/oh-my-posh: A prompt theming engine for Powershell
- microsoft/cascadia-code: This is a fun, new monospaced font that includes programming ligatures and is designed to enhance the modern look and feel of the Windows Terminal.
- ryanoasis/nerd-fonts: Iconic font aggregator, collection, & patcher. 3,600+ icons, 50+ patched fonts: Hack, Source Code Pro, more. Glyph collections: Font Awesome, Material Design Icons, Octicons, & more