Windows-Terminal 美化以及通过 OhMyPosh 美化 PowerShell

2022-01-12 09:13:43 浏览数 (1)

通过 Powerline 来自定义 PowerShell,以提供 Git 状态提示符,再对 Windows Terminal 美化,得到一个优秀的终端体验

这里先放出微软的官方教程:

https://docs.microsoft.com/zh-cn/windows/terminal/tutorials/powerline-setup#set-up-powerline-in-wsl-ubuntu

但是微软给出的文档有坑,到本文编写的时候文档还没有进行修改,不妨按照我的步骤来设置

安装 OhMyPosh 的字体

如果不安装字体,最后终端可能会出现乱码

这里我们需要安装 Meslo LGM NF 字体,这是 Oh my Posh 官方推荐使用的字体,使用微软推荐的 Cascadia Code PLCascadia Mono PL 会出现图标不显示的问题

Github 链接:

https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/Meslo.zip

网络不好的可以通过 Github 的镜像站来下载

https://hub.fastgit.org/ryanoasis/nerd-fonts/releases/download/v2.1.0/Meslo.zip

下载后解压,进入 ttf 文件夹,选中下图的 ttf 文件,分别是常规、粗体、斜体、粗斜体,右键进行安装

安装完后可以在字体设置中预览

在 PowerShell 中安装 Posh-Git 和 Oh-My-Posh

在执行这一步前,先确保你安装了 git

在这一步中,我们会安装 Posh-Git 和 Oh-My-Posh:

在 PoweShell 窗口中,用 Terminal 打开 PowerShell 也行,执行以下命令

代码语言:javascript复制
Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser

如果想为全部用户安装,使用管理员打开 Terminal 或 PowerShell 执行以下命令

代码语言:javascript复制
Install-Module posh-git
Install-Module oh-my-posh

如果出现不受信任的存储库,就选择 A

如果需要更新这两个工具,执行以下命令

代码语言:javascript复制
Update-Module -Name posh-git
Update-Module -Name oh-my-posh

要注意的是,全局安装的模块需要管理员权限才能进行更新

更新完之后我们要卸载旧版本的模块

先获取模块的版本号,需要记住旧版本的版本号

代码语言:javascript复制
Get-Module -ListAvailable

卸载旧版本的模块

代码语言:javascript复制
Uninstall-Module -Name "posh-git" -RequiredVersion "0.7.3"
Uninstall-Module -Name "oh-my-posh" -RequiredVersion "3.106.7"

最后再确认一下是否卸载成功

自定义 PowerShell 提示符

现在当前 PowerShell 窗口导入 posh-git 和 oh-my-posh

代码语言:javascript复制
Import-Module posh-git
Import-Module oh-my-posh

然后我们输入以下命令来预览效果

代码语言:javascript复制
Get-PoshThemes

选择你喜欢的主题,记住他的名称,我这里选择 honukai 主题

输入这个命令

代码语言:javascript复制
notepad $PROFILE

添加以下内容,在每次打开 PowerShell 的时候,就会执行里面的命令

代码语言:javascript复制
Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt -Theme honukai

保存并退出,重新打开 PowerShell,你就会看到你选择的主题了

这里和微软官方的文档不同,因为 oh-my-posh 的新版本使用 go 来重写了,指令发生了变化,所以旧的命令是无效的

WindowsTerminal 美化

打开 Terminal,点击 Ctrl , 打开配置文件

这里放出我的配置文件,你也可以自己设置,但是 “fontFace” 这一项一定要设置成 MesloLGM NF 字体,否则会乱码

代码语言:javascript复制
"fontFace": "MesloLGM NF",

配置文件:

代码语言:javascript复制
// This file was initially generated by Windows Terminal 1.0.1401.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// 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": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

  // You can add more global application settings here.
  // To learn more about global settings, visit https://aka.ms/terminal-global-settings

  // If enabled, selections are automatically copied to your clipboard.
  "copyOnSelect": false,

  // If enabled, formatted data is also copied to your clipboard
  "copyFormatting": false,

  // A profile specifies a command to execute paired with information about how it should look and feel.
  // Each one of them will appear in the 'New Tab' dropdown,
  //   and can be invoked from the commandline with `wt.exe -p xxx`
  // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
  "profiles": {
    "defaults": {
      // Put settings here that you want to apply to all profiles.
    },
    "list": [
      {
        // Make changes here to the powershell.exe profile.
        "backgroundImage": "E://WIP-6th-anniversary-wallpaper-dark.jpg",
        "backgroundImageOpacity": 0.6,
        "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
        "name": "Windows PowerShell",
        "commandline": "powershell.exe",
        "colorScheme": "AdventureTime",
        "useAcrylic": false,
        "closeOnExit": true,
        "acrylicOpacity": 0.25,
        "cursorColor": "#FFFFFF",
        "fontFace": "MesloLGM NF",
        "tabTitle": "Powershell",
        "hidden": false
      },
      {
        // Make changes here to the cmd.exe profile.
        "backgroundImage": "E://WIP-6th-anniversary-wallpaper-dark.jpg",
        "backgroundImageOpacity": 0.6,
        "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
        "name": "命令提示符",
        "commandline": "cmd.exe",
        "colorScheme": "AdventureTime",
        "useAcrylic": false,
        "closeOnExit": true,
        "acrylicOpacity": 0.25,
        "cursorColor": "#FFFFFF",
        "fontFace": "MesloLGM NF",
        "hidden": false
      },
      {
        "backgroundImage": "E://WIP-6th-anniversary-wallpaper-dark.jpg",
        "backgroundImageOpacity": 0.6,
        "guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
        "hidden": false,
        "name": "Ubuntu-20.04",
        "source": "Windows.Terminal.Wsl",
        "startingDirectory": "//wsl$/Ubuntu-20.04/home/hominsu",
        "colorScheme": "AdventureTime",
        "useAcrylic": false,
        "closeOnExit": true,
        "acrylicOpacity": 0.25,
        "cursorColor": "#FFFFFF",
        "fontFace": "MesloLGM NF"
      },
      {
        "backgroundImage": "E://WIP-6th-anniversary-wallpaper-dark.jpg",
        "backgroundImageOpacity": 0.6,
        "guid": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
        "hidden": false,
        "name": "Ubuntu-18.04",
        "source": "Windows.Terminal.Wsl",
        "startingDirectory": "//wsl$/Ubuntu-18.04/home/hominsu",
        "colorScheme": "AdventureTime",
        "useAcrylic": false,
        "closeOnExit": true,
        "acrylicOpacity": 0.25,
        "cursorColor": "#FFFFFF",
        "fontFace": "MesloLGM NF"
      },
      {
        "backgroundImage": "E://WIP-6th-anniversary-wallpaper-dark.jpg",
        "backgroundImageOpacity": 0.6,
        "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
        "colorScheme": "AdventureTime",
        "useAcrylic": false,
        "closeOnExit": true,
        "acrylicOpacity": 0.25,
        "cursorColor": "#FFFFFF",
        "fontFace": "MesloLGM NF",
        "hidden": false,
        "name": "Azure Cloud Shell",
        "source": "Windows.Terminal.Azure"
      }
    ]
  },

  // Add custom color schemes to this array.
  // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
  "schemes": [
    {
      "name": "AdventureTime",
      "black": "#050404",
      "red": "#bd0013",
      "green": "#4ab118",
      "yellow": "#e7741e",
      "blue": "#0f4ac6",
      "purple": "#665993",
      "cyan": "#70a598",
      "white": "#f8dcc0",
      "brightBlack": "#4e7cbf",
      "brightRed": "#fc5f5a",
      "brightGreen": "#9eff6e",
      "brightYellow": "#efc11a",
      "brightBlue": "#1997c6",
      "brightPurple": "#9b5953",
      "brightCyan": "#c8faf4",
      "brightWhite": "#f6f5fb",
      "background": "#000000",
      "foreground": "#f8dcc0"
    }
  ],

  // Add custom keybindings to this array.
  // To unbind a key combination from your defaults.json, set the command to "unbound".
  // To learn more about keybindings, visit https://aka.ms/terminal-keybindings
  "keybindings": [
    // 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"
    }
  ]
}

这是文件中的图片,这张图片是某天微软丢到我的邮箱里的,很好看,就一直用了下去

这里放上链接

https://image.hauhau.cn/WIP-6th-anniversary-wallpaper-dark.jpg

https://image.hauhau.cn/WIP-6th-anniversary-wallpaper-light.jpg

到此,我们的美化就基本完成了

0 人点赞