Windows终端美化

501 阅读2分钟

Windows终端美化

总所周知,Windows默认的终端非常难看且不好用,本文介绍使用Windows Terminal+nerd-fonts+Oh my posh+PSReadLine 实现终端的美化字体样式和自动补全。

1. 安装Chocolatey

Chocolatey Software | Installing Chocolatey网页下,复制安装脚本

安装脚本

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

复制到终端中

即可自动安装Chocolatey

2. 安装字体

推荐使用等宽连体带图标(终端显示的花样多一点)的字体

我使用Nerd Fonts

安装使用Chocolatey即可

choco install nerd-fonts-hack

3. 编辑windows terminal 配置使用Nerd Fonts字体

这里很多人都会去下载一个powershell,其实没有必要,Windows自带的powershell的版本和功能已经可以满足要求。

当然也可以直接使用配置文件,"defaults""profiles"属性下

"defaults": 
{
    "backgroundImage": null,
    "colorScheme": "Campbell",
    "cursorShape": "filledBox",
    "experimental.retroTerminalEffect": false,
    "font": 
    {
        "face": "CodeNewRoman Nerd Font Mono",
        "size": 14.0,
        "weight": "normal"
    },
    "opacity": 90,
    "padding": "0",
    "scrollbarState": "hidden",
    "useAcrylic": false
},

其余的配置文件内容

"copyFormatting": "none",
"copyOnSelect": false,
"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"alwaysOnTop": false,
"alwaysShowTabs": true,
"autoHideWindow": false,
"disableAnimations": false,
"firstWindowPreference": "defaultProfile",
"focusFollowMouse": false,
"initialCols": 88,
"initialPosition": "750,350",
"initialRows": 24,
"launchMode": "focus",

大概在这个行数

4. 设置管理员权限启动

没有这个选项的更新即可

5. 使用oh-my-posh

  1. 安装,还是使用Chocolatey
choco install oh-my-posh
  1. 创建powershell配置文件
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
  1. 打开配置文件
notepad $PROFILE
  1. 写入指令,这里是让终端在启动时加载oh-my-posh配置文件,其中~/.omp.theme.json是配置文件的所在路径,windows的话就是在C:\Users\Administrator下创建.omp.theme.json文件并写入配置即可
oh-my-posh init pwsh --config ~/.omp.theme.json | Invoke-Expression
  1. 使用kushal主题,其他主题在这里选择Themes | Oh My Posh

  2. 复制内容粘贴到配置文件中即可

  1. 关闭启动时的banner,添加-nologo项即可

6. 使用自动补全

  1. 先安装最新的PowerShellGet
Install-Module -Name PowerShellGet -Force
Exit
  1. 再安装PSReadLine
Install-Module PSReadLine -AllowPrerelease -Force
  1. powsershell输入
notepad.exe $PROFILE
  1. 在打开的界面输入下面内容
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete #Tab键会出现自动补全菜单
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
# 上下方向键箭头,搜索历史中进行自动补全
  1. 保存重启powershell即可