效果图

介绍

  • 对于使用 Windows 的程序员来说,可能对 Windows 最大的愿望就是有一个像 Linux 那样完善美观的 terminal 了,不得不说,Windows 默认的命令提示符和 PowerShell 界面都太丑了,特别是 PowerShell,简直不能看。

  • 对于 Windows 下的 terminal:

    • Maximus5/ConEmu 由于注入 Hook 导致性能不太好,此外还有一些 Bug。

    • zeit/hyper,这是一个基于 Electron 和 Node-pty 的工具, Node-pty 目前是有 Bug 的,具体来说,是 Node-pty 依赖的 winpty 有 Bug,与最新的 Windows 兼容性不好,好在微软在 Win 10 1809 上发布了新的 Conpty API 用于替代 winpty,不过目前 Node-pty 还没有适配,所以在未来 (19H1) hyper 会是一个不错的选择。

使用Windows terminal配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 安装oh-my-posh
scoop install oh-my-posh
# 安装字体
oh-my-posh font install meslo
# Windows Terminal 设置字体
{
"profiles":
{
"defaults":
{
"font":
{
"face": "MesloLGM Nerd Font"
}
}
}
}
# 创建文件 New-Item -Path $PROFILE -Type File -Force
notepad $PROFILE
# 设置utf8编码
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
oh-my-posh init pwsh | Invoke-Expression

## 自定义 --config
### 环境变量方式
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/jandedobbeleer.omp.json" | Invoke-Expression
### 绝对路径方式
oh-my-posh init pwsh --config C:\Users\sfuli\scoop\apps\oh-my-posh\21.26.3\themes\jandedobbeleer.omp.json | Invoke-Expression
1
2
3
4
5
6
7
8
## 安装模块
scoop install psreadline posh-git

notepad $PROFILE

Set-Alias ll Get-ChildItem
Import-Module posh-git
Import-Module PSReadLine

字体

  • be5invis/Sarasa-Gothic (更纱黑体),这款字体的中文使用思源黑体,英文使用 Iosevka,同时还内置了 Powerline,简直完美。

  • 更纱黑体按照用途有很多不同的分支,由于我们是用于 terminal 的,选择安装 Sarasa Term Sc(点我下载),安装好之后,在 PowerShell 的设置中可以看到改字体。

修改配置

  • 打开powershell方式

    1. 从注册表中获取配置

      • shift + 鼠标右键打开
    2. PowerShell 的快捷方式(菜单中查找)

      • 从开始菜单中打开
      • 从 Win + x 菜单中打开。
  • 因为打开的方式多样,需要多处配置。配置完后要到菜单栏 - 右键 - 属性 - 点击确定后才能保存配置

    • shift + 鼠标右键打开后,进入属性 (设置字体配色

    • 从 Win + x 菜单中打开,进入属性 (设置字体配色

配色

  • 我们使用微软推出的 ColorTool 修改配色,这个工具使用十分简单,而且支持 iTerm2 的配色方案文件,我选择的是 Argonaut 配色方案。

    • Argonaut.itermcolors 文件下载后放到 schemes 文件夹中,然后执行以下命令把系统默认配色更改为 Argonaut:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      # 使用scoop 安装
      scoop install ColorTool

      # 查看安装信息
      scoop info ColorTool
      #Installed:
      # D:\scoop\apps\ColorTool\1904.29002
      # 将Argonaut.itermcolors文件放到 D:\scoop\apps\ColorTool\1904.29002\schemes文件夹中

      # 设置默认配色
      ColorTool.exe -b Argonaut.itermcolors

    oh-my-posh

    • Linux 上有 oh-my-zsh,在 Windows 上,我们使用 JanDeDobbeleer/oh-my-posh 增强命令行体验。

    • 安装方法很简单,首先应该安装 Git for Windows,之后执行以下命令进行安装:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      # 首先以管理员身份打开 PowderShell,执行如下命令,卸载已安装的 oh-my-posh,并升级 posh-git 和 PSReadLine:
      Remove-Module oh-my-posh -Force
      Install-Module -Name PSReadLine -Scope AllUsers -Force -SkipPublisherCheck
      Install-Module posh-git -Scope AllUsers -Force

      # 然后根据oh-my-posh官方文档的指引重新安装它。执行如下命令:
      # scoop 安装
      scoop install oh-my-posh
      # 或使用 winget 安装 OhMyPosh
      winget install JanDeDobbeleer.OhMyPosh -s winget
      # (后续需要更新时)使用 winget 更新 OhMyPosh
      winget upgrade JanDeDobbeleer.OhMyPosh -s winget

      应用商店对 Windows Terminal 进行了更新,以下安装命令并没有效果了

      1
      2
      Install-Module posh-git -Scope CurrentUser
      Install-Module oh-my-posh -Scope CurrentUser
    • 遇到确定全部选 Y。之后使用以下命令打开配置文件:

      1
      notepad $PROFILE
    • 填入以下配置:

      1
      2
      3
      4
      5
      6
      Set-Alias ll Get-ChildItem
      Import-Module posh-git
      Import-Module PSReadLine
      # 这里配置的主题为 robbyrussell。可打开如下路径文件夹,查看全部的主题并选择自己喜欢的: $env:POSH_THEMES_PATH
      oh-my-posh init pwsh --config $env:POSH_THEMES_PATH\robbyrussell.omp.json | Invoke-Expression
      # Set-Theme PowerLine
    • Set-Alias ll Get-ChildItem 这句话是设置别名,保证可以使用ll

    • 最后在设置 -> 更新和安全 -> 开发者选项中打开允许执行 PowerShell 脚本。

    最后

    • 经过以上操作,我们已经得到了基本完善的 PowerShell,可以直接在快速启动栏打开,也可以在某一文件夹打开直接进入该文件夹。

    • 在使用过程中,我发现了一个问题,oh-my-zsh 默认主题的右箭头 无法显示,目测是由于更纱黑体不支持该符号导致。经过一番查询,我找到了解决方案。

    • 具体来说,在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink 中新建多字符串值,取名为 Sarasa Term SC,填入以下内容:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      MICROSS.TTF,Microsoft Sans Serif,108,122
      MICROSS.TTF,Microsoft Sans Serif
      MINGLIU.TTC,PMingLiU
      MSMINCHO.TTC,MS PMincho
      BATANG.TTC,Batang
      MSYH.TTC,Microsoft YaHei UI
      MSJH.TTC,Microsoft JhengHei UI
      YUGOTHM.TTC,Yu Gothic UI
      MALGUN.TTF,Malgun Gothic
      SEGUISYM.TTF,Segoe UI Symbol
    • 最后重启电脑。Done!