欢迎光临!
若无相欠,怎会相见

Windows 10 终端美化工程

序言

前段时间在 B 站看到了Windows 10 上 PowerShell 使用 oh-my-posh 美化的相关视频, 我也在自己的办公电脑上使用美化后的终端一段时间,还是比较喜欢的,现在在自己的电脑上进行终端美化工程。

美化过程

Windows Terminal

美化的第一步需要安装 Windows Terminal,可以直接访问这个链接进行下载安装 https://github.com/microsoft/terminal/releases ,或者从 Windows 应用商店里面安装,安装完成后再打开会是这样的界面。

PowerShell 7

直接从这个链接下载安装包 : https://github.com/PowerShell/PowerShell/releases ,Windows 自带的版本不行,换成 PowerShell 7 使用起来更爽,我安装的是预览版本。安装完毕打开之后如下图:

PSReadLine

这里安装 PSReadLine 模块,需要 PowerShell 安装完成 。

以管理员权限启动 PowerShell 后直接执行如下命令,需要下载模块,没有进度提示需静静等待:

Install-Module PSReadLine -Scope AllUsers -AllowPrerelease -Force -Verbose

最后选择 Y (Yes) 确认安装。

oh-my-posh

这一步安装 oh-my-posh 模块, 和 Linux 里面的 oh-my-zsh 类似,接着上一步直接执行下面的命令:

Install-Module oh-my-posh -Scope AllUsers -AllowPrerelease -Verbose

同样选择 Y 进行安装。

posh-git

这里安装 posh-git,会使git 在终端中更好用,接着执行命令:

Install-Module posh-git -Scope AllUsers -AllowPrerelease -Verbose

同样选择 Y 继续安装。

Fonts

安装完模块之后,这一步安装字体,因为Windows自带的字体是不包含某些符号,如果字体用的不好的话,显示出来的终端还不如默认的。

在这里安装 Nerd Font 字体,我经常使用的是 FiraCode Nerd Font Mono 字体,因为用习惯了,这个字体的链接是 https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/FiraCode.zip, 其他字体可通过这个链接下载: https://github.com/ryanoasis/nerd-fonts/releases/tag/v2.1.0 。

解压之后选择一个安装,需要注意一下,字体名称自己需要记录一下,方便下一步使用,字体名称如下:

Terminal Setting

在美化 Terminal 之前, 先设置 Terminal 一下, 在 Terminal 中集成 PowerShell , 以后的主力终端就是 PowerShell 了 。我的配置如下:

// 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": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
  // If enabled, selections are automatically copied to your clipboard.
  "copyOnSelect": false,

  // If enabled, formatted data is also copied to your clipboard
  "copyFormatting": false,
  "profiles": [
      {
          // Make changes here to the cmd.exe profile
          "background": "#000000",
          "backgroundImage": "C://1.jpg",
          "backgroundImageOpacity": 0.5,
          "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
          "name": "cmd",
          "commandline": "cmd.exe",
          "colorScheme": "AdventureTime",
          "useAcrylic": false,
          "closeOnExit": true,
          "acrylicOpacity": 0.25,
          "cursorColor": "#FFFFFF",
          "fontFace": "Fira Code",
          "hidden": false
      },
      {
          // Make changes here to the powershell.exe profile
          "background": "#000000",
          "backgroundImage": "C://1.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": "Fira Code",
          "hidden": false,
          "tabTitle": "Powershell"
      },
      {
          "background": "#000000",
          "backgroundImage": "C://1.jpg",
          "backgroundImageOpacity": 0.6,
          "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
          "colorScheme": "AdventureTime",
          "useAcrylic": false,
          "closeOnExit": true,
          "acrylicOpacity": 0.25,
          "cursorColor": "#FFFFFF",
          "fontFace": "Fira Code",
          "hidden": false,
          "name": "Azure Cloud Shell",
          "source": "Windows.Terminal.Azure"
      },
      {
          "guid": "{46ca431a-3a87-5fb3-83cd-11ececc031d2}",
          "hidden": false,
          "name": "kali-linux",
          "source": "Windows.Terminal.Wsl",
          "fontFace": "Fira Code"
      },
      {
          "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
          "hidden": false,
          "name": "PowerShell",
          "source": "Windows.Terminal.PowershellCore",
          "fontFace": "FiraCode Nerd Font Mono",
          // JetBrainsMono Nerd Font Mono      FiraCode Nerd Font Mono
          "commandline": "C:/Program Files/PowerShell/7-preview/pwsh.exe -nologo",
          "fontSize": 11,
          "historySize": 9001,
          "padding": "5, 5, 20, 25",
          "snapOnInput": true,
          "useAcrylic": false,
          "colorScheme": "Homebrew"

      }
  ],
  // Add custom color schemes to this array
  "schemes": [
      {
          "name": "3024 Day",
          "black": "#090300",
          "red": "#db2d20",
          "green": "#01a252",
          "yellow": "#fded02",
          "blue": "#01a0e4",
          "purple": "#a16a94",
          "cyan": "#b5e4f4",
          "white": "#a5a2a2",
          "brightBlack": "#5c5855",
          "brightRed": "#e8bbd0",
          "brightGreen": "#3a3432",
          "brightYellow": "#4a4543",
          "brightBlue": "#807d7c",
          "brightPurple": "#d6d5d4",
          "brightCyan": "#cdab53",
          "brightWhite": "#f7f7f7",
          "background": "#f7f7f7",
          "foreground": "#4a4543"
      },
      {
          "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": "#1f1d45",
          "foreground": "#f8dcc0"
      },
      {
          "name" : "Raspberry",
          "background" : "#3C0315",
          "black" : "#282A2E",
          "blue" : "#0170C5",
          "brightBlack" : "#676E7A",
          "brightBlue" : "#80c8ff",
          "brightCyan" : "#8ABEB7",
          "brightGreen" : "#B5D680",
          "brightPurple" : "#AC79BB",
          "brightRed" : "#BD6D85",
          "brightWhite" : "#FFFFFD",
          "brightYellow" : "#FFFD76",
          "cyan" : "#3F8D83",
          "foreground" : "#FFFFFD",
          "green" : "#76AB23",
          "purple" : "#7D498F",
          "red" : "#BD0940",
          "white" : "#FFFFFD",
          "yellow" : "#E0DE48"
      },
      {
          "name": "Homebrew",
          "black": "#000000",
          "red": "#FC5275",
          "green": "#00a600",
          "yellow": "#999900",
          "blue": "#6666e9",
          "purple": "#b200b2",
          "cyan": "#00a6b2",
          "white": "#bfbfbf",
          "brightBlack": "#666666",
          "brightRed": "#e50000",
          "brightGreen": "#00d900",
          "brightYellow": "#e5e500",
          "brightBlue": "#0000ff",
          "brightPurple": "#e500e5",
          "brightCyan": "#00e5e5",
          "brightWhite": "#e5e5e5",
          "background": "#283033",
          "foreground": "#00ff00"
      }
  ],
  // Add any keybinding overrides to this array.
  // To unbind a default keybinding, set the command to "unbound"
  "keybindings": [],
  "actions":
  [
      // 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" }
  ]
}

将 PowerShell 的路径修改为自己的实际路径就 OK 了,那么以后打开 Windows Terminal 会默认打开 PowerShell , 如图:

美化过程

安装完上面的3个模块后,就可以设置界面了,oh-my-posh 中附带了几个主题, 可以使用 Get-PoshThemes 预览主题,然后选择自己喜欢的。

但是,自带的几个主题对我来说稍稍有点儿缺陷,因此我自己修改了一点,适合自己才是最好的。废话先不多说,看一下美化过程。

首先设置 PowerShell 的 Profile 文件, 在 PowerShell 中执行命令 : notepad.exe $Profile , 将下面的内容输入到文件中并保存:

# 引入 posh-git
Import-Module posh-git

# 引入 oh-my-posh
Import-Module oh-my-posh

# 设置 PowerShell 主题
Set-PoshPrompt Paradox-plus

# 设置显示命令历史
set-psreadlineoption -predictionsource history

# 设置 Tab 为菜单补全
Set-PSReadLineKeyHandler -Key "Tab" -Function MenuComplete

# 设置 Ctrl+d 为退出 PowerShell
Set-PSReadlineKeyHandler -Key "Ctrl+d" -Function ViExit

# 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo

# 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward

# 设置向下键为前向搜索历史纪录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

# Python 直接执行
$env:PATHEXT += ";.py"

Set-Alias -Name ls -Value Get-ChildItem

这里的主题被我设置为 Paradox-plus ,实际上并不存在这个主题,我只是修改了 Paradox 主题,使其更适合于我。

到这个目录下,C:\Program Files\PowerShell\Modules\oh-my-posh\3.130.0\themes 这里存放的是 oh-my-posh 的主题文件,这里的版本号可能不一样,只需要找到 oh-my-posh 目录就可以找到 themes 目录 , 直接修改或者复制后重命名为 paradox-plus.omp.json ,将里面的内容替换为如下内容:

{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [
    {
      "type": "prompt",
      "alignment": "left",
      "segments": [
        {
          "type": "root",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#100e23",
          "background": "#ffe9aa"
        },
        {
          "type": "session",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#100e23",
          "background": "#ffffff"
        },
        // {
        //   "type": "path",
        //   "style": "powerline",
        //   "powerline_symbol": "\uE0B0",
        //   "foreground": "#100e23",
        //   "background": "#91ddff",
        //   "properties": {
        //     "folder_icon": "\uF115",
        //     "folder_separator_icon": " \uE0B1 ",
        //     "style": "full"
        //   }
        // },
        {
          "type": "path",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#100e23",
          "background": "#91ddff",
          "properties": {
            "style": "full"
          }
        },
        {
          "type": "git",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#193549",
          "background": "#95ffa4"
        },
        {
          "type": "python",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#100e23",
          "background": "#906cff",
          "properties": {
            "prefix": " \uE235 "
          }
        },
        {
          "type": "exit",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#ffffff",
          "background": "#ff8080",
          "properties": {
            "prefix": " \uE20F"
          }
        },
        {
          "type": "time",
          "style": "plain",
          "foreground": "#007ACC",
          "properties": {
            "time_format": "15:04:05"
          }
        }
      ]
    },
    {
      "type": "newline"
    },
    {
      "type": "prompt",
      "alignment": "left",
      "segments": [
        {
          "type": "text",
          "style": "plain",
          "foreground": "#007ACC",
          "properties": {
            "prefix": "",
            "text": "\u276F"
          }
        }
      ]
    }
  ],
  "final_space": false
}

到此就完成了,看一下效果:

2021-04-15更新:

由于我经常使用 Anaconda 集成 Python ,所以今天才发现 PowerShell 中无法激活 Python 的虚拟环境,解决方法如下:

直接在 PowerShell 中执行 conda init powershell ,然后重启终端。效果如下:

现在可以正常切换 conda 的虚拟环境了,但是我还有一个需求,就是希望可以一直显示当前的 Python 环境,毕竟这个主题默认情况下只在有Python源码的目录里面显示后面的python信息,下面我把我的主题再次更新了一下,以方便一直显示 Python 信息:

{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [
    {
      "type": "prompt",
      "alignment": "left",
      "segments": [
        {
          "type": "root",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#100e23",
          "background": "#ffe9aa"
        },
        {
          "type": "session",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#100e23",
          "background": "#ffffff"
        },
        {
          "type": "python",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#100e23",
          "background": "#906cff",
          "properties": {
            "display_mode": "always",
            "display_default": true,
            "prefix": " \uE235 "
          }
        },
        {
          "type": "path",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#100e23",
          "background": "#91ddff",
          "properties": {
            "style": "full"
          }
        },
        {
          "type": "git",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#193549",
          "background": "#95ffa4"
        },
        {
          "type": "exit",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#ffffff",
          "background": "#ff8080",
          "properties": {
            "prefix": " \uE20F"
          }
        },
        {
          "type": "time",
          "style": "plain",
          "foreground": "#007ACC",
          "properties": {
            "time_format": "15:04:05"
          }
        }
      ]
    },
    {
      "type": "newline"
    },
    {
      "type": "prompt",
      "alignment": "left",
      "segments": [
        {
          "type": "text",
          "style": "plain",
          "foreground": "#007ACC",
          "properties": {
            "prefix": "",
            "text": "\u276F"
          }
        }
      ]
    }
  ],
  "final_space": false
}

Python 段的设置可以参考官方文档 https://ohmyposh.dev/docs/python ,我把 display_mode 设置为 always 就是希望一直显示这个 Python 信息段,但是有些不足的是只能在非家目录里面显示,如图:

另外,这个主题颜色比较刺眼,文字都是绿色的,给一个我比较喜欢的暗色调的配色:

{
  "name": "Arthur",
  "black": "#3d352a",
  "red": "#cd5c5c",
  "green": "#86af80",
  "yellow": "#e8ae5b",
  "blue": "#6495ed",
  "purple": "#deb887",
  "cyan": "#b0c4de",
  "white": "#bbaa99",
  "brightBlack": "#554444",
  "brightRed": "#cc5533",
  "brightGreen": "#88aa22",
  "brightYellow": "#ffa75d",
  "brightBlue": "#87ceeb",
  "brightPurple": "#996600",
  "brightCyan": "#b0c4de",
  "brightWhite": "#ddccbb",
  "background": "#1c1c1c",
  "foreground": "#ddeedd",
  "selectionBackground": "#4d4d4d",
  "cursorColor": "#e2bbef"
}

效果如下图,同时也可以通过 https://windowsterminalthemes.dev/ 网站自己选择适合自己的配色。

这两种配色我都挺喜欢的,都在 Terminal Setting 中保存着。

结语

本次终端就美化成这样,后面可能会有细微的调整,还是那句话,适合自己的才是最好用的。

如有错误,敬请指出,感谢指正!                 — 2021-04-14  23:04:05

赞(0) 打赏
转载请注明:飘零博客 » Windows 10 终端美化工程
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

欢迎光临