福州大学城论坛

 找回密码
 立即注册
开启左侧

美观实用的一款命令终端神器!值得一用

[复制链接]
发表于 2025-1-12 19:57:03 | 显示全部楼层 |阅读模式 来自 LAN
                                                                美观实用的一款命令终端神器!值得一用                           

                                                        Oh - My - Posh 是一款可深度定制的全平台终端工具,是为满足开发人员对简洁、美观且功能强大的提示符需求而诞生的项目。它是跨平台和跨 Shell 的提示符渲染器,以高定制性和低延迟性受到青睐。

美观实用的一款命令终端神器!值得一用

美观实用的一款命令终端神器!值得一用


  • 特性

    • 跨平台支持:支持 Windows、GNU/Linux(WSL)、macOS 三个系统。
    • 多种 Shell 支持:包括 PowerShell、bash、zsh、Fish 等。
    • 高可定制性:通过 JSON 配置文件进行详细配置。
    • 低延迟:优化了性能,保证提示符的快速渲染。

美观实用的一款命令终端神器!值得一用

美观实用的一款命令终端神器!值得一用
Oh - My - Posh 的安装

一、在 Windows 上安装


  • 使用 Scoop

    • 命令:scoop install oh - my - posh

  • 使用 Winget

    • 命令:winget install JanDeDobbeleer.OhMyPosh

美观实用的一款命令终端神器!值得一用

美观实用的一款命令终端神器!值得一用
在 macOS 和 Linux 上安装


  • 使用 Homebrew

    • 命令:brew install jandedobbeleer/oh - my - posh/oh - my - posh

  • 使用 Curl

    • 命令:

      • sudo wget https://github.com/JanDeDobbeleer/oh - my - posh/releases/latest/download/posh - linux - amd64 - O /usr/local/bin/oh - my - posh
      • sudo chmod +x /usr/local/bin/oh - my - posh


美观实用的一款命令终端神器!值得一用

美观实用的一款命令终端神器!值得一用
二、Oh - My - Posh 的配置


  • 使用 JSON 文件配置
    Oh - My - Posh 使用 JSON 文件进行配置,用户可定义提示符的样式、颜色及显示内容。
  • 创建配置文件

    • 例如创建一个名为 posh.json 的配置文件:

{    "final_space": true,    "segments": [        {            "type": "prompt",            "style": "powerline",            "foreground": "#ffffff",            "background": "#4c4c4c"        },        {            "type": "git",            "style": "plain",            "foreground": "#ff9800",            "properties": {                "branch_max_length": 20            }        },        {            "type": "path",            "style": "plain",            "foreground": "#bb86fc",            "background": "#160c28"        },        {            "type": "shell",            "style": "plain",            "foreground": "#76c7c0"        }    ]}



  • 在不同 Shell 中加载配置文件

    • PowerShell:在 $PROFILE 文件中添加oh - my - posh init pwsh --config ~/path/to/posh.json | Invoke - Expression。
    • Bash:在 ~/.bashrc 文件中添加eval "$(oh - my - posh init bash --config ~/path/to/posh.json)"。
    • Zsh:在 ~/.zshrc 文件中添加eval "$(oh - my - posh init zsh --config ~/path/to/posh.json)"。
    • Fish:在 ~/.config/fish/config.fish 文件中添加oh - my - posh init fish --config ~/path/to/posh.json | source。

三、示例配置详解

Git Segment


  • 功能:用于显示 Git 仓库的信息,如当前分支名称、未提交更改等。
  • 示例配置

美观实用的一款命令终端神器!值得一用

美观实用的一款命令终端神器!值得一用
{    "type": "git",    "style": "plain",    "foreground": "#ff9800",    "properties": {        "branch_max_length": 20,        "display_status": true,        "display_stash_count": true    }}
Path Segment



  • 功能:用于显示当前路径,适合长时间导航文件系统的开发人员。可设置路径显示的样式及颜色。
  • 示例配置
{    "type": "path",    "style": "short",    "foreground": "#bb86fc",    "background": "#160c28",    "properties": {        "path_max_length": 40,        "home_icon": "~"    }}
Shell Segment


  • 功能:用于显示当前 Shell 的信息,包括用户名称、主机名称等。
  • 示例配置

{    "type": "shell",    "style": "powerline",    "foreground": "#76c7c0",    "background": "#2c3e50",    "properties": {        "user_format": " $user",        "host_format": " $hostname"    }}
完整配置示例

{    "final_space": true,    "segments": [        {            "type": "prompt",            "style": "powerline",            "foreground": "#ffffff",            "background": "#4c4c4c"        },        {            "type": "git",            "style": "plain",            "foreground": "#ff9800",            "properties": {                "branch_max_length": 20,                "display_status": true,                "display_stash_count": true            }        },        {            "type": "path",            "style": "short",            "foreground": "#bb86fc",            "background": "#160c28",            "properties": {                "path_max_length": 40,                "home_icon": "~"            }        },        {            "type": "shell",            "style": "powerline",            "foreground": "#76c7c0",            "background": "#2c3e50",            "properties": {                "user_format": " $user",                "host_format": " $hostname"            }        },        {            "type": "time",            "style": "plain",            "foreground": "#d3d3d3",            "background": "#444444",            "properties": {                "time_format": "15:04:05"            }        }    ]}
Oh - My - Posh 可以在不同操作系统配置安装以及自定义途径。它丰富的配置选项和简洁的提示符样式能极大提升命令行工具的使用体验,在各个主流操作系统和多种 Shell 环境中都能为用户打造高度定制化的高效提示符。
上篇介绍的神器,需要的可以体验

a7cb452b105c96647f5068a42c57fec3.png
福州大学城论坛 - 声明 1、在发表言论时,请遵守当地法律法规。主题所有言论纯属个人意见,与本站立场无关。
2、本站所有主题由作者发表,作者享有帖子相关版权,其他单位或个人使用、转载或引用本文时必须征得作者同意并注明来源于福州大学城论坛
3、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。
4、帖子不遵守当地法律法规、广告、人身攻击等情况时,福州大学城论坛管理人员有权不事先通知发贴者而删除本文。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|福州大学城论坛 ( 闽ICP备2022007610号-2 )|站点地图

GMT+8, 2025-6-10 03:18 , Processed in 0.349632 second(s), 10 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表