powershell命令行提示符样式配置自定义@ohmyposh安装@卸载

安装oh my posh

安装模式

  • oh my posh下载被制作为一个.exe程序,可以手动下载安装,并且可以选择安装模式

    • 仅为当前用户安装
    • 为所有用户安装
  • 命令行安装通常是仅为当前用户安装(不需要管理员权限),而GUI安装界面还可以选择为所有用户安装,这需要管理员权限

  • 不同安装模式安装后环境变量以及对应的安装目录不同,通过 $env:POSH_THEMES_PATH获取路径

  • 如果为当前用户安装过,再次为所有用户安装,那么当前用户的环境变量会盖过系统环境变量(如果只需要保留一个,可以考虑卸载掉一个)

权限问题

  • 仅为当前用户安装的好处是修改posh的配置文件不需要管理员权限,比较方便

基本使用步骤

  • Now that Oh My Posh is installed, you can go ahead and configure your terminal and shell to get the prompt to look exactly like you want.

posh命令和posh for powershell 命令👺

  • 安装完posh后,其会自动配置环境变量,因此可以在任意shell中访问到posh程序(命令行程序)

    • PS 🕰️5:09:29 PM [C:\Users\cxxu\Desktop] 🔋100%→posh -h
      oh-my-posh is a cross platform tool to render your prompt.
      It can use the same configuration everywhere to offer a consistent
      experience, regardless of where you are. For a detailed guide
      on getting started, have a look at the docs at https://ohmyposh.dev
      
      Usage:
        oh-my-posh [flags]
        oh-my-posh [command]
      
      Available Commands:
        cache       Interact with the oh-my-posh cache
        completion  Generate the autocompletion script for the specified shell
        config      Interact with the config
        debug       Print the prompt in debug mode
      

posh提供的可用函数

  • 检查posh模块提供的所有可用的powershell函数

    🚀 gcm *posh*|where {$_.Source -like "oh-my-posh*"}
    
    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Function        Enable-PoshLineError                               0.0        oh-my-posh-core
    Function        Enable-PoshTooltips                                0.0        oh-my-posh-core
    Function        Enable-PoshTransientPrompt                         0.0        oh-my-posh-core
    Function        Export-PoshTheme                                   0.0        oh-my-posh-core
    Function        Get-PoshThemes                                     0.0        oh-my-posh-core
    Function        Set-PoshContext                                    0.0        oh-my-posh-core
    

posh初始化和主题设置👺

oh my posh 字体问题

自动启动posh@powershell的配置文件 “$Profile”

  • 根据需要修改以下配置文件,其中#xxx是注释,不会运行
#import Module (recommended add the next line,but,of course ,you can  choose not  to add the item,if you do not use the git tools)
# Import-Module posh-git

# import posh
Import-Module oh-my-posh
#set theme
Set-poshPrompt paradox
#set tab auto completion(optional item)
#(the command line will try to offer you a list(candidated) when you press the `tab`key
#of course,if the current path have only one or even none can match what you want to match,it will just try to complete the current object name

Set-PSReadlineKeyHandler -Key Tab -Function Complete

推荐的 posh-git模块

自定义powershell命令行提示符的样式👺

简单修改

  • 使用posh等美化工具需要比较大的开销,反映会变慢;

  • 我们可以利用以下方法简单地修改默认地powershell提示符样式

    function prompt_style
    {
        Set-Item -Path function:prompt -Value { 'PS ' + '🕰️' + (Get-Date -Format T) + ' ' + "[$(Get-Location)]" + ' ' }
        # 显示时分秒,可以用-Format T 或 -Displayhint time
    }
    
  • PS 🕰️11:45:19 [C:\repos\scripts]
  • 日期或时间显示字段设置:Get-Date (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn

带上电量

  • function batteryChargePer
    {
        # get battery charge:
        $charge = Get-CimInstance -ClassName Win32_Battery | Select-Object -ExpandProperty EstimatedChargeRemaining
        return $charge
        # "Current Charge:[ $charge %]."
        # -replace '.*\[(.*)\].*', '$1'
    }
    
    function prompt_style
    {
        Set-Item -Path function:prompt -Value { 'PS ' + '🕰️' + (Get-Date -Format T) + ' ' + "[$(Get-Location)]" + " 🔋$(batteryChargePer)%" + ' ' }
        # 显示时分秒,可以用-Format T 或 -Displayhint time
    }
    

基本的配置效果👺

  • 在这里插入图片描述

卸载@移除 oh-my-posh👺

使用winget卸载

手动卸载

  • 找到posh安装目录,找到unins000,卸载

    • PS C:\Program Files (x86)\oh-my-posh> ls
      
              Directory: C:\Program Files (x86)\oh-my-posh
      
      
      Mode                LastWriteTime         Length Name
      ----                -------------         ------ ----
      d----          2024/2/8     23:45                  bin
      d----          2024/2/9      0:39                  themes
      -a---          2024/2/8     23:45          23103   unins000.dat
      -a---          2024/2/8     23:45        3209720 󰣆  unins000.exe
      -a---          2024/2/8     23:45          24097   unins000.msg
      
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cxxu1375

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值