美化 PowerShell

2023-05-16

美化 PowerShell


UPDATE 2022.3.4: 本文使用的 oh-my-posh 基于 V2 版本,而更新且功能更强大的新版本已经发布,如需使用请参考其官方文档。


1. 准备工作

Step1. 下载并安装 PowerShell Core(pwsh),可在 Github 页面 进行下载。

PowerShell Core 相比传统 PowerShell 优势很多,此处推荐安装

Step2. 确保 Windows 版本在 v1903 之上(可以运行 winver 进行查看)

Windows10 在 1903 版本之后,带来了全新的 conpty,解决了原本 winpty 的大量 bug

Step3. 安装 VSCode 以及 PowerLine fonts

  • VSCode 下载地址
  • PowerLine Fonts 仓库地址
  • Nerd Fonts 仓库地址

2. 安装 PowerShell 模块

以管理员运行 PowerShell,并执行命令:

Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser

(可选)安装预览版 PSReadLine:

Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck

3. 创建启动配置文件

执行命令:

if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
notepad $PROFILE

编辑并保存如下内容:

chcp 65001
Import-Module posh-git 
Import-Module oh-my-posh 
Set-Theme Agnoster

4. 配置 Pwsh 字体和主题颜色

启动 pwsh,可从标题栏右键 > 属性中配置字体,建议使用 PowerLine 字体。

在 Microsoft/Terminal 仓库 中,可以下载到 ColorTool 工具,其可以应用 iTerm2-Color-Schemes 仓库 中的主题配色。

使用方法为:

./colortool -b $THEME_FILE

接下来在标题栏右键 > 属性,之后什么都不用做,直接点击确定,就可以把当前控制台和默认控制台的配色方案进行切换。


5. VSCode 内置终端

需要配置终端的 shell 和字体
字体和程序

另外,需要启用 Conpty
conpty


6. 自定义主题

定位到如下路径(oh-my-posh 版本号要视情况而定):
路径

创建的自定义主题存放在此处即可,例如我基于 Agnoster.psm1Fish.psm1 修改而成的 AgnosterPower.psm1:

#requires -Version 2 -Modules posh-git

function Write-Theme {

    param(
        [bool]
        $lastCommandFailed,
        [string]
        $with
    )

    $lastColor = $sl.Colors.PromptBackgroundColor

    $prompt = Write-Prompt -Object $sl.PromptSymbols.StartSymbol -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor

    #check the last command state and indicate if failed
    If ($lastCommandFailed) {
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.FailedCommandSymbol) " -ForegroundColor $sl.Colors.CommandFailedIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
    }

    #check for elevated prompt
    If (Test-Administrator) {
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.ElevatedSymbol) " -ForegroundColor $sl.Colors.AdminIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
    }

    $user = [System.Environment]::UserName
    $computer = [System.Environment]::MachineName
    if (Test-NotDefaultUser($user)) {
        $prompt += Write-Prompt -Object "$user@$computer " -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
    }

    if (Test-VirtualEnv) {
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.VirtualEnvSymbol) $(Get-VirtualEnvName) " -ForegroundColor $sl.Colors.VirtualEnvForegroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.VirtualEnvBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
    }
    else {
        $prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
    }

    # Writes the drive portion
    $prompt += Write-Prompt -Object (Get-ShortPath -dir $pwd) -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
    $prompt += Write-Prompt -Object ' ' -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor

    $status = Get-VCSStatus
    if ($status) {
        $themeInfo = Get-VcsInfo -status ($status)
        $lastColor = $themeInfo.BackgroundColor
        $prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $sl.Colors.PromptBackgroundColor -BackgroundColor $lastColor
        $prompt += Write-Prompt -Object " $($themeInfo.VcInfo) " -BackgroundColor $lastColor -ForegroundColor $sl.Colors.GitForegroundColor
    }

    if ($with) {
        $prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor -BackgroundColor $sl.Colors.WithBackgroundColor
        $prompt += Write-Prompt -Object " $($with.ToUpper()) " -BackgroundColor $sl.Colors.WithBackgroundColor -ForegroundColor $sl.Colors.WithForegroundColor
        $lastColor = $sl.Colors.WithBackgroundColor
    }

    # Writes the postfix to the prompt
    $prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor
    $prompt += ' '
    $prompt
}

$sl = $global:ThemeSettings #local settings
$sl.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0)
$sl.Colors.SessionInfoBackgroundColor = [ConsoleColor]::DarkGray
$sl.Colors.PromptForegroundColor = [ConsoleColor]::White
$sl.Colors.PromptSymbolColor = [ConsoleColor]::White
$sl.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue
$sl.Colors.GitForegroundColor = [ConsoleColor]::Black
$sl.Colors.WithForegroundColor = [ConsoleColor]::White
$sl.Colors.WithBackgroundColor = [ConsoleColor]::DarkRed
$sl.Colors.VirtualEnvBackgroundColor = [System.ConsoleColor]::Red
$sl.Colors.VirtualEnvForegroundColor = [System.ConsoleColor]::White

7. 最终效果

final


参考链接

  • PowerShell 美化指南
  • README - oh-my-posh
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

美化 PowerShell 的相关文章

随机推荐

  • MYSQL单行长度不能超过 65535

    报错 xff1a Row size too large The maximum row size for the used table type not counting BLOBs is 65535 mysql属于关系型 xff0c 行式
  • jdbcTemplate.batchUpdate在批量执行的时候,性能差没有效果,看看怎么解决的。

    我用的阿里druid数据库连接池 xff08 其实这个和连接池毛线关系没得 xff09 xff0c 创建jdbctemplate在执行insert 15000条数据时 xff0c 我发现还是30条 xff0c 20条 xff0c 35条这样
  • rabbit-mq 本地环境搭建

    一 xff1a 安装RabbitMQ需要先安装Erlang语言开发包 xff0c 直接下载地址 xff1a http erlang org download otp win64 18 3 exe 尽量安装时不选择C盘 xff0c 避免操作系
  • oracle小数点前面没有0,纠结解惑

    一1 天前台人找到我 xff0c 说我们安装的数据库有问题 xff0c 为什么小数点前面是0就不显示呢 xff0c 我去看了一下 xff0c command窗口要显示 SQL gt create table ml test num numb
  • 网监后台管理系统设计思路

    本次做的是网监系统saas服务平台的后台管理系统 xff0c 不涉及复杂功能逻辑 就是从菜单 模板 系 统 组织架构 角色 用户的设计思路 产品需求 xff1a 在各个省市网监系统的数量不断增长 xff0c 且系统逻辑和功能模块大致相同 x
  • 分类算法 c4.5 详解

    C4 5是一系列用在机器学习和数据挖掘的分类问题中的算法 它的目标是监督学习 xff1a 给定一个数据集 xff0c 其中的每一个元组都能用一组属性值来描述 xff0c 每一个元组属于一个互斥的类别中的某一类 C4 5的目标是通过学习 xf
  • 正则表达式匹配Html代码中图片路劲

    正则表达匹配图片路径 public static string GetHtmlImageUrlList string sHtmlText 定义正则表达式用来匹配 img 标签 Regex regImg 61 new Regex 64 34
  • (GPU版)Pytorch+pycharm+jupyter安装记录(截至23年3月14日)

    由于搞了一台旧主机 xff0c 主机上没有pytorch等软件程序 xff0c 所以重新装一遍 xff0c 顺便记录一下 xff01 一 安装显卡GPU的驱动程序 xff0c 搞定CUDA先 WIN 43 R打开命令行 xff0c 输入命令
  • Alibaba官方最新发布的这份Java学习导图+彩版手册,真不是吹的

    时间飞逝 xff0c 转眼间毕业七年多 xff0c 从事 Java 开发也六年了 我在想 xff0c 也是时候将自己的 Java 整理成一套体系 这一次的知识体系面试题涉及到 Java 知识部分 性能优化 微服务 并发编程 开源框架 分布式
  • linux下使用第三方商店安装应用

    安装 snap store 进行下载 xff0c 相当与第三方应用商店 xff0c 但是往往比某一个官方软件源里面的应用要丰富或更实用 到 snap docs 中选择你的 linux 版本进入安装文档 xff0c 根据指示一步一步安装即可
  • Centos7离线安装sqlserver2017

    Centos7离线安装sqlserver2017 根据操作系统版本选择下载匹配的sqlserver版本 可以在这里找一下https packages microsoft com config 我选择是先在一台有网的机器上下载好rpm安装包之
  • HC-05蓝牙模块配置

    目录 1 连接蓝牙模块a 蓝牙模块通过USB转TTL连接电脑b 打开串口助手 xff0c 波特率设置为38400c 检验是否连接成功 2 配置波特率3 修改密码4 设置主从模式5 设置蓝牙连接模式6 查询自身地址7 添加配对蓝牙地址8 测试
  • Windows沙盒技术调研

    转载自 xff1a 移动云开发者社区 一 Windows沙盒技术介绍 Windows沙盒提供了轻型桌面环境 xff0c 可安全地隔离运行应用程序 沙盒环境中Windows软件保持 34 沙盒 34 状态 xff0c 并独立于主机运行 沙盒是
  • OS + Linux Shell bash / sh / ksh / csh / tcsh / adb shell

    s Android adb shell ADB Android debug bridge Android手机实际是基于Linux系统的 Google提供的ADB工具包带有fastboot exe rar http dl iteye com
  • kali利用CVE_2019_0708(远程桌面代码执行漏洞)攻击win7

    一 漏洞说明 2019年5月15日微软发布安全补丁修复了CVE编号为CVE 2019 0708的Windows远程桌面服务 xff08 RDP xff09 远程代码执行漏洞 该漏洞在不需身份认证的情况下即可远程触发 危害与影响面极大 目前
  • 数据库系统原理1

    第一章 数据库管理技术发展的不同阶段形成不同的特点 数据描述经历了三个阶段对应于三个数据模型 第二章 数据库系统的生命周期 xff0c 书中可能和我们学习软工的时候有些出入 xff0c 其实就是不同时间有不同的理解 xff0c 横看成岭侧成
  • ssh详解

    SSH ssh secure shell protocol 22 tcp 安全的 具体的软件实现 xff1a OpenSSH ssh协议的开源实现 xff0c CentOS dropbear xff1a 另一个开源实现 SSH协议版本 v1
  • spring框架的简单配置步骤——小马同学@Tian

    spring框架配置步骤 1 导入jar包 本教程使用spring5 1 5 xff0c 在pom xml中进行导入依赖 Maven方式 xff1a span class token tag span class token tag spa
  • PSReadLine - Powershell 的强化工具

    PSReadLine Powershell 的强化工具 UPDATE 2022 3 4 根据其 Github README 的说明 xff0c If you are using Windows PowerShell on Windows 1
  • 美化 PowerShell

    美化 PowerShell UPDATE 2022 3 4 本文使用的 oh my posh 基于 V2 版本 xff0c 而更新且功能更强大的新版本已经发布 xff0c 如需使用请参考其官方文档 1 准备工作 Step1 下载并安装 Po