---
title: "Powershell 美化教程"
date: 2020-11-19T20:11:58.000Z
author: Z.SHINCHVEN
tags: [Oh-My-Posh, Posh-Git, PowerShell, Windows Terminal, Windows]
canonical: https://atlassc.net/2020/11/19/oh-my-posh-lets-go
---
![Windows Terminal with Oh-My-Posh](https://docs.microsoft.com/en-us/windows/terminal/images/overview.png)

macOS 和 Linux 上有令人羡慕的`oh-my-zsh`，它让开发者能在`zsh`定制炫酷的主题皮肤，在 Windows 上也有开发者开发出了`Oh-My-Posh`这个`PowerShell`的module，它配合`posh-git`使用时，也能对 PowerShell 的主题进行定制。

这个美化套装的主要功能是能在命令行中方便地观察到git的仓库的状态，如图所示：

![Oh-My-Posh Example](https://github.com/JanDeDobbeleer/oh-my-posh/raw/master/img/indications.png)


## 预装环境

1. git
2. [Windows Terminal](https://docs.microsoft.com/en-us/windows/terminal/)
3. [PowerShell](https://github.com/PowerShell/PowerShell/releases/tag/) 最新版，（Oh-My-Posh 需要在新版本的 PowerShell 上运行）

## 配置 Windows Terminal 默认使用新版 PowerShell

1. 在 Windows Terminal 的配置文件中添加一个 PowerShell： `profiles` - `lists`
2. 设置使用支持 Powerline 的字体，Oh-My-Posh 的主题像 oh-my-zsh 的一些主题一样，需要 Powerline 的字体支持，否则会看到乱码：
    1. 微软官网上建议使用 [Cascadia Code PL](https://chocolatey.org/packages/cascadiacodepl) 字体
    2. 而我选择了 JetBrains 家族的 [JetBrains Mono](https://chocolatey.org/packages/jetbrainsmono) 字体
    3. 当然你还可以装其他 [Powerline](https://chocolatey.org/packages/dejavufonts) 字体

```json
{
  "profiles": {
    "lists": [
      {
        "guid": "{0f92157d-7dde-493d-aa2c-1f762ebcd6d8}",
        "hidden": false,
        "name": "Windows PowerShell Core",
        "source": "Windows.Terminal.PowershellCore",
        "fontFace": "Cascadia Code PL"
      }
    ]
  }
}
```

## 安装 Oh-My-Posh

打开 PowerShell 运行以下命令安装：

```PowerShell
# 先安装 Posh-Git，-AllowPrerelease 参数表示会安装 beta 版
Install-Module -Name posh-git -AllowPrerelease
# 再安装 Oh-My-Posh，暂时不要安装 beta 版
Install-Module -Name oh-my-posh
```

** 如果 Install-Module 下载不动，请添加 `-Proxy <proxy_uri>` 参数。

## 启用

安装完毕之后需要运行命令来启用`Oh-My-Posh`

```PowerShell
# 启动 Oh-My-Posh
Set-Prompt
```

## 设置主题

请去挑选你喜欢的[主题](https://github.com/JanDeDobbeleer/oh-my-posh#themes)，然后使用 `Set-Theme` 命令进行设置：

```PowerShell
# 设置你想要的皮肤
Set-Theme Paradox
```

## 启动时自动加载主题

如果要保存主题设置，每次启动的时候都自动加载选定的主题，需要在 PowerShell 的 $PROFILE 中添加相应配置：

```PowerShell
# 打开 $PROFILE 文件
code $PROFILE
```

添加相关启动项

```PowerShell
Set-Theme Paradox
```

## 在 IDE 中使用 PowerShell 作为默认 Terminal

### JetBrains 家族 IDE

1. 打开菜单中 `File` - `Settings...`
2. 搜索 Terminal，修改 `Application settings` 中的 `Shell path` 为 `pwsh.exe`

** 单行主题似乎对 JetBrains 的 Console 兼容不好，可以考虑成双行的主题，比如[robbyrussell](https://github.com/JanDeDobbeleer/oh-my-posh#robbyrussell) 。


### 安装 Hack Fonts

```pwsh
choco install hackfont-windows
```
### Visual Studio Code

Edit `settings.json`

```json
{
    "terminal.integrated.shell.windows": "pwsh.exe",
    "terminal.integrated.fontFamily":"Hack Nerd Font Mono"
}
```
