powershell@posh主题设置和自定义@动态补全配置

posh自定义:扩展或修改主题

refs

检查预览所有内置的主题

  • Themes | Oh My Posh

  • 在powershell中输入Get-PoshThemes,posh会将所有主题输出,并且会输出主题配置文件所在目录

    • To change your theme, adjust the init script in C:\Users\cxxu\Documents\PowerShell\Microsoft.PowerShell_profile.ps1.
      Example:
        oh-my-posh init pwsh --config 'C:\Users\cxxu\AppData\Local\Programs\oh-my-posh\themes\jandedobbeleer.omp.json' | Invoke-Expression
      

主题配置文件所在目录

  • 环境变量:$env:POSH_THEMES_PATH

  • Themes location: $env:LOCALAPPDATA\Programs\oh-my-posh\themes

基于已有的主题进行更改

  • Customize | Oh My Posh

  • 先复制一份基本满意的主题

    • 我想使用内置的一个iterm2主题,然而,他的第二个字段颜色过深,我希望将其调整为亮色的
    • 复制一份改主题配置文件
    • 利用编辑器(例如vscode)将副本打开,(您可以参考官网的字段介绍,也可以凭着经验来做一些基本的颜色修改)
  • 可以看到,第二个segment背景被我调为浅色,路径部分看起来更加清晰了

  • 在这里插入图片描述

主题配置文件预览

  • 主题文件是json文件,例如iterm2.omp.json预览一下

  • {
      "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
      "blocks": [
        {
          "alignment": "left",
          "segments": [
            {
              "background": "#003543",
              "foreground": "#fff",
              "powerline_symbol": "\ue0b0",
              "properties": {
                "windows": "\uf179"
              },
              "style": "powerline",
              "template": " {{ if .WSL }}WSL at {{ end }}{{.Icon}}",
              "type": "os"
            },
            {
              "background": "#003543",
              "foreground": "#d2ff5e",
              "powerline_symbol": "\ue0b0",
              "style": "powerline",
              "template": "{{ .UserName }} ",
              "type": "session"
            },
            {
              "background": "#afffdd",
              "foreground": "#003544",
              "powerline_symbol": "\ue0b0",
              "properties": {
                "folder_separator_icon": "/",
                "style": "full"
              },
              "style": "powerline",
              "template": " \ue5ff {{ .Path }} ",
              "type": "path"
            },
            {
              "background": "#d2ff5e",
              "background_templates": [
                "{{ if or (.Working.Changed) (.Staging.Changed) }}#ff9248{{ end }}",
                "{{ if and (gt .Ahead 0) (gt .Behind 0) }}#f26d50{{ end }}",
                "{{ if gt .Ahead 0 }}#89d1dc{{ end }}",
                "{{ if gt .Behind 0 }}#f17c37{{ end }}"
              ],
              "foreground": "#193549",
              "powerline_symbol": "\ue0b0",
              "properties": {
                "fetch_stash_count": true,
                "fetch_status": true
              },
              "style": "powerline",
              "template": " {{ .HEAD }}{{ if .Staging.Changed }} \uf046 {{ .Staging.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Working.Changed }} \uf044 {{ .Working.String }}{{ end }}{{ if gt .StashCount 0 }} \ueb4b {{ .StashCount }}{{ end }} ",
              "type": "git"
            },
            {
              "background": "#33DD2D",
              "background_templates": [
                "{{ if gt .Code 0 }}#f1184c{{ end }}"
              ],
              "foreground": "#242424",
              "powerline_symbol": "\ue0b0",
              "properties": {
                "always_enabled": true
              },
              "style": "powerline",
              "template": " \uea6c ",
              "type": "status"
            }
          ],
          "type": "prompt"
        },
        {
          "alignment": "right",
          "segments": [
            {
              "background": "#f36943",
              "background_templates": [
                "{{if eq \"Charging\" .State.String}}#33DD2D{{end}}",
                "{{if eq \"Discharging\" .State.String}}#FFCD58{{end}}",
                "{{if eq \"Full\" .State.String}}#0476d0{{end}}"
              ],
              "foreground": "#242424",
              "invert_powerline": true,
              "powerline_symbol": "\ue0b2",
              "style": "powerline",
              "template": " {{ if not .Error }}{{ .Icon }}{{ .Percentage }}{{ end }}{{ .Error }}\uf295 \ueb2d ",
              "type": "battery"
            },
            {
              "background": "#0087D8",
              "foreground": "#003544",
              "invert_powerline": true,
              "powerline_symbol": "\ue0b2",
              "properties": {
                "display_mode": "context",
                "fetch_virtual_env": true
              },
              "style": "powerline",
              "template": " \ue235 {{ .Venv }} ",
              "type": "python"
            },
            {
              "background": "#003543",
              "foreground": "#fff",
              "invert_powerline": true,
              "powerline_symbol": "\ue0b2",
              "style": "powerline",
              "template": "<#fff> \ue641 </>{{ .CurrentDate | date .Format }} ",
              "type": "time"
            }
          ],
          "type": "prompt"
        },
        {
          "alignment": "left",
          "newline": true,
          "segments": [
            {
              "foreground": "#FFD700",
              "style": "plain",
              "template": " \u26a1 ",
              "type": "root"
            },
            {
              "foreground": "#f1184c",
              "style": "plain",
              "template": "🚀 ",
              "type": "text"
            }
          ],
          "type": "prompt"
        }
      ],
      "console_title_template": "{{ .Shell }} in {{ .Folder }}",
      "final_space": true,
      "transient_prompt": {
        "background": "transparent",
        "foreground": "#FFD700",
        "template": "{{if .Root}}\u26a1 {{end}}🚀 "
      },
      "version": 2
    }
    

tips@如何确定选择想要的颜色代码

  • (可选操作)可以建立一个css文件来时时预览您的颜色
  • 在这里插入图片描述

设置@切换主题👺

Set-PoshPrompt函数👺
  • oh-my-posh init pwsh | Invoke-Expression

  • 可以用-c参数顺便指定使用的主题,例如:oh-my-posh init pwsh -c "$env:POSH_THEMES_PATH\1_shell.omp.json" | Invoke-Expression

  • 事实上,早期的set-poshtheme可以方便的切换主题,现在的新版本被移除了,切换主题比较麻烦点,可以自行编写函数实现类似的效果,不知道后期会不会改回来

  • powershell函数配置

    • function Set-PoshPrompt
      {
          <# 
          .synopsis
          设置oh-my-posh主题,可以用 ls $env:POSH_THEMES_PATH 查看可用主题,我们只需要获取.omp.json前面部分的主题配置文件名称即可
          .example
          🚀 Set-PoshPrompt ys
          # cxxu @ cxxuwin in ~\Desktop [21:17:20]
          $ Set-PoshPrompt 1_shell
           Set-PoshPrompt iterm2
           #>
          param (
              # [Parameter(Mandatory)]
              [string]
              $Theme
          )
          oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\$Theme.omp.json" | Invoke-Expression
      }
      

默认主题

  • Default themes

  • You can find the themes in the folder indicated by the environment variable POSH_THEMES_PATH.

  • For example, you can use oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" | Invoke-Expression for the prompt initialization in PowerShell.

官方的方法

  • 官网中提到,可以通过export-poshTheme等操作来修改,但是对于复杂的主题,导出文件内容已经和源文件(.json)发生了变化,实践过程中容易报错

历史命令动态补全

启用动态补全

  • Adding Predictive IntelliSense to my Windows Terminal PowerShell Prompt with PSReadline - Scott Hanselman’s Blog

    Install-Module PSReadLine -AllowPrerelease -Force
    Import-Module PSReadLine
    Set-PSReadLineOption -PredictionSource History
    Set-PSReadLineOption -PredictionViewStyle ListView
    Set-PSReadLineOption -EditMode Windows
    
    
    
  • PS C:\Program Files (x86)\oh-my-posh> ls
    <-/10>                                                        <History(10)>
    > ls iterm*                                                       [History]
    > ls iterm                                                        [History]
    > ls $env:POSH_THEMES_PATH                                        [History]
    > ls hans*                                                        [History]
    > ls $posh_theme_home\iterm2_cxxu2.omp.json                       [History]
    > ls  $posh5_theme_home\iterm2_cxxu2.omp.json                     [History]
    > ls win*                                                         [History]
    > ls -Recurse -Directory  *power*                                 [History]
    > ls -Directory  *power* -Recurse                                 [History]
    > ls *power* -Recurse                                             [History]
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cxxu1375

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

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

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

打赏作者

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

抵扣说明:

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

余额充值