简 述: 美化 Windows-Terminal + OhMyPosh + PowerShell 7 来作为 windows 下的主力终端。

[TOC]


本文初发于 “偕臧的小站“,同步转载于此。


背景

  半年前,在 上一篇 中详细介绍了 Windows-Terminal + OhMyPosh + GitBash 的美化 ,win10 下终端算是勉强可以使用了,颜值关勉强过了。

  但是每次启动速度感觉很慢,使用 git 命令亦明显很是卡顿的感觉。自己的笔记本启动需 1-2 s,而公司电脑通常 5-6s,前面了解到 pwoshell 后,今天决定不能忍;遂切 PowerShell 7 + Windows-Terminal,最后效果来看,颜值和速度都有了✨。前提:若下载网络不畅,需自行解决下。


介绍

  win10 21H2 默认配置的是 PowerShell 5,约 2016 年;推荐用 PowerShell 7 ,对应 2022 年。此两版本差异很大,疑惑自行翻阅资料。对于未曾接触过此工具的,可参考之前写的这一篇 shell、bash、cmd、PowerShell的区别


正文

TIME:更新 2024.07.12

  1. 下载 Windows TerminalPowerShell 7, Fira Code Nerd Font 就这个 v2.1 tag 看着很顺眼,v2.2 字体有点不习惯

  2. 打开 pwsh7 ,命令安装 scoopohmyposh

    # 官网方式安装 scoop
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
    Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression
    
    scoop install winfetch neofetch
    
    # 安装 OhMyPosh
    winget install JanDeDobbeleer.OhMyPosh -s winget
    
    # 此条是以前的经验,保留提供参考 iwr -useb get.scoop.sh | iex     // 挂代理有时候依旧,安装 scoop 网络问题,操!!!;则使
    用 https://gitee.com/RubyKids/scoop-cn ,亲测可用
    Set-ExecutionPolicy RemoteSigned -scope CurrentUser
    iwr -useb https://gitee.com/RubyKids/scoop-cn/raw/main/install.ps1 | iex    // 可用
    
    
  3. pwsh 执行

    Set-ExecutionPolicy RemoteSigned -scope CurrentUser  # 开启扩展
    Install-Module -Name PSReadLine  -Scope CurrentUser  # 安装PSReadline包,让命令行很好用
    Install-Module posh-git  -Scope CurrentUser          # Git 命令补全,遇安装失败看这个 https://github.com/dahlbyk/posh-git#installation,试一下其它命令
    #Install-Module oh-my-posh -Scope CurrentUser         # oh-my-posh 不再支持此种
  4. 选择主题 “jandedobbeleer.omp.json”

    "C:\Users\用户名\AppData\Local\Programs\oh-my-posh\themes\jandedobbeleer.omp.json" 文件拷贝到 $HOME 目录下;

    再在 ~/.profile 文件(没有则创建)添加命令,后执行 . ~/.profile 刷新,重启终端即可生效。

    eval "$(oh-my-posh --init --shell bash --config ~/jandedobbeleer.omp.json)"
  5. 安装 Meslo LGM NF 字体,避免乱码。

    打开终端,惊喜的发现乱码,符号显示不出来;安装字体 Fira Code Nerd Font 就这个 v2.1 tag

  6. 修改配置文件 settings.json,将如下代码添加到对应的地方

    // ---------------------添加新的标签页,添加到 "profiles - list" 下---------------------
    {
        "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
        "hidden": false,
        "name": "pwsh7",
        "source": "Windows.Terminal.PowershellCore",
        "backgroundImageOpacity": 0.72,
        "closeOnExit": "graceful",
        "colorScheme": "HomebrewCus",
        "cursorColor": "#FFFFFF",
        "cursorShape": "bar",
        "font": {
            "face": "FiraCode Nerd Font"
        }, // Fira Code、FiraCode Nerd Font、MesloLGM  NF
        "historySize": 9001,
        "opacity": 89,
        "padding": "25",
        "snapOnInput": true,
        "startingDirectory": ".",
        "useAcrylic": true
    },
      
    // ps: 先终端-设置-选定默认主题,默认的启动名称。且安装 pwsh7 后好像会自动生成一个这个项,对其进行扩写即可;其安装pa路径为 "commandline": "C:/Program Files/PowerShell/7/pwsh.exe",
    
    // ---------------------新增主题配色,添加到 "schemes" 下--------------------- 
    {
        "background": "#283033",
        "black": "#000000",
        "blue": "#6666E9",
        "brightBlack": "#666666",
        "brightBlue": "#0E5CEE",
        "brightCyan": "#00E5E5",
        "brightGreen": "#00D900",
        "brightPurple": "#E500E5",
        "brightRed": "#E50000",
        "brightWhite": "#E5E5E5",
        "brightYellow": "#E5E500",
        "cursorColor": "#FFFFFF",
        "cyan": "#00A6B2",
        "foreground": "#00FF00",
        "green": "#00A600",
        "name": "HomebrewCus",
        "purple": "#B200B2",
        "red": "#FC5275",
        "selectionBackground": "#FFFFFF",
        "white": "#BFBFBF",
        "yellow": "#999900"
    },
  7. 添加 PowerShell 的启动参数 notepad.exe $Profile

    提示文件不存在则新建,然后将此脚本拷贝后保存文本 FileName: Microsoft.PowerShell_profile.ps1 即可;其默认的路径 $Profile 可查看到

    # 引入 posh-git
    Import-Module posh-git
    
    # 引入 ps-read-line
    Import-Module PSReadLine
    
    # 设置主题
    oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\paradox.omp.json" | Invoke-Expression
    
    #-------------------------------  Set Hot-keys BEGIN  -------------------------------
    # 设置预测文本来源为历史记录
    Set-PSReadLineOption -PredictionSource History
    
    # 每次回溯输入历史,光标定位于输入内容末尾
    Set-PSReadLineOption -HistorySearchCursorMovesToEnd
    
    # 设置 Tab 为菜单补全和 Intellisense
    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
    #-------------------------------  Set Hot-keys END    -------------------------------
    
    #-------------------------------   Set Alias BEGIN    -------------------------------
    # 1. 编译函数 make
    function MakeThings {
        nmake.exe $args -nologo
    }
    Set-Alias -Name make -Value MakeThings
    
    # 2. 更新系统 os-update
    Set-Alias -Name os-update -Value Update-Packages
    
    # 3. 查看目录 ls & ll
    function ListDirectory {
        (Get-ChildItem).Name
        Write-Host("")
    }
    #Set-Alias -Name ls -Value ListDirectory
    #Set-Alias -Name ll -Value Get-ChildItem
    
    # 4. 打开当前工作目录
    
    function OpenCurrentFolder {
        param
        (
            # 输入要打开的路径
            # 用法示例:open C:\
            # 默认路径:当前工作文件夹
            $Path = '.'
        )
        Invoke-Item $Path
    }
    Set-Alias -Name open -Value OpenCurrentFolder
    #-------------------------------    Set Alias END     -------------------------------
  8. 创建快捷方式后,属性栏中的 “Shortcut Key” 添加快捷键 “Ctrl + Alt + T”

  9. pwsh 提示更新

    今天一打开终端,提示更新,可没有顺滑的更新命令;见 #8663 ; 故不更新解决方案:

    You can set environment variable POWERSHELL_UPDATECHECK=LTS (or =Off).