简 述: 美化 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 ntop
    
    # 安装 OhMyPosh
    winget install JanDeDobbeleer.OhMyPosh --source winget

    Note:

    此条是以前的经验,保留提供参考 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 执行 字体安装和 Git 补全

    oh-my-posh font install meslo firacode  // 安装字体,然后 Windows Terminal 中设置默认字体,透明度 84%,开启亚克力
    
    Set-ExecutionPolicy RemoteSigned -scope CurrentUser  # 开启扩展
    Install-Module -Name PSReadLine  -Scope CurrentUser  # 安装PSReadline包,让命令行很好用
    Install-Module posh-git  -Scope CurrentUser          # Git 命令补全,遇安装失败看这个 

    Note:

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

    // 或 oh-my-posh font install 这将显示 Nerd Fonts 字体库列表,推荐选择Meslo包含 Meslo LGM NF)

  4. 选择 oh-my-posh 主题 “jandedobbeleer.omp.json”

  5. notepad $PROFILE 后,将如下拷贝进去,再执行 . $PROFILE 立即生效

    # ==============================
    # PowerShell Profile (pwsh 7)
    # ==============================
    
    # 隐藏 Profile 加载时间提示
    $global:__SuppressProfileLoadTime = $true
    
    # ==============================
    # 基础模块
    # ==============================
    Import-Module PSReadLine -ErrorAction SilentlyContinue
    Import-Module posh-git  -ErrorAction SilentlyContinue
    
    # ==============================
    # PSReadLine(补全 + 体验)
    # ==============================
    if (Get-Module PSReadLine) {
    
        # 历史预测(类似 zsh)
        Set-PSReadLineOption -PredictionSource History
        Set-PSReadLineOption -PredictionViewStyle ListView
    
        # 编辑模式(Windows 风格)
        Set-PSReadLineOption -EditMode Windows
    
        # Tab:菜单补全(关键)
        Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
    
        # Ctrl + r:历史搜索
        Set-PSReadLineKeyHandler -Key Ctrl+r -Function ReverseSearchHistory
    
        # Ctrl + l:清屏
        Set-PSReadLineKeyHandler -Key Ctrl+l -Function ClearScreen
    
        # 更流畅的体验(可选)
        Set-PSReadLineOption -HistoryNoDuplicates
        Set-PSReadLineOption -BellStyle None
    }
    
    # ==============================
    # oh-my-posh(主题)
    # ==============================
    # 推荐使用完整路径(更稳定) paradox
    # 例如:"$env:POSH_THEMES_PATH\unicorn.omp.json"
    
    oh-my-posh init pwsh --config "unicorn" | Invoke-Expression
    
    # ==============================
    # 常用函数
    # ==============================
    
    function ll {
        Get-ChildItem -Force
    }
    
    function la {
        Get-ChildItem -Force | Format-Table
    }
    
    function grep {
        param($pattern)
        Select-String -Pattern $pattern
    }
    
    
    # ==============================
    # 可选优化(大仓库卡顿时开启)
    # ==============================
    # $env:POSH_GIT_ENABLED = $false
  6. 创建快捷方式后,属性栏中的 “Shortcut Key” 添加快捷键 “Ctrl + Alt + T”

  7. pwsh 提示更新

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

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