惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
Martin Fowler
Martin Fowler
爱范儿
爱范儿
IT之家
IT之家
Last Week in AI
Last Week in AI
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
The Cloudflare Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
The GitHub Blog
The GitHub Blog
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog

CMTOPS.DEV

CMTOPS.DEV CMTOPS.DEV CMTOPS.DEV CMTOPS.DEV CMTOPS.DEV CMTOPS.DEV
CMTOPS.DEV
Timofey Chuchkanov · 2024-08-05 · via CMTOPS.DEV

You Don't Need a New Fancy Shell With Plugins to Get a Decent $PS1

~3 min read

Updated on

Quite a lot of people who want to get a decent-looking shell prompt, install a fancy shell like Zsh, and a whole framework like oh-my-zsh with a bunch of plugins on top of it. You don't actually need all of this.

Contents

  • Intro
  • Where to Start
  • My Prompt
    • Main Code
    • Where I Place My $PS1
    • The Result
  • The End

Intro

Sometimes, I notice people — especially those who are new to Linux and other Unix-like systems — who get bored of their shell’s default prompt.

Some of them get a shell like fish or Zsh with a framework like oh-my-zsh. It’s ok to have a different taste, and use what you like — that’s the whole point of having so many variety in software, after all.

However, it’s not rare among such people to not even use other features of these shells, frameworks, and whatnot.

There’s no point in overcomplicating things just to set an environment variable controlling your prompt appearance. I’ve been there too at some point many years ago. That’s a skill issue.

Where to Start

Just as with everything else in this beautiful world of Unix-like, you start by reading documentation.

Open up your terminal, or a dedicated website with man pages or GNU info documents rendered to HTML, and go through your shell’s manual. There, search for PS1, an environment variable meant to describe how your main prompt should look like.

Here are a couple of examples with the corresponding sections:

Don’t rush to create the most badass prompt you’ve ever seen just yet.

Start small, experiment a bit. When you get comfortable, you can try to add colors and different shapes, if you want to. Personally, I prefer my prompt to be more minimalistic.

My Prompt

To give you a sense of what you can do without any special shell magic, here’s the prompt I use daily on my OpenBSD, FreeBSD, and Linux machines.

It works on both Bash and Korn Shell. I haven’t tested it with other shells yet.

Main Code

# This function is used to get the current branch of a git repo, if the $PWD is a git repo.
# It's referenced later to define the PS1 itself.
__cstm_git_ps1()
{
    local PROMPT=""
    local GIT_BRANCH=""
    local GIT_DIRTY=""

    if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
        if [ ! -z "$(command -v git)" ]; then
            local GIT_BRANCH="$(git branch --show-current)"
            local GIT_DIRTY="$(git status --porcelain)"
        fi
    fi

    if [ ! -z "${GIT_BRANCH}" ]; then
        PROMPT=" (${GIT_BRANCH})"
    fi

    if [ ! -z "${GIT_DIRTY}" ]; then
        PROMPT="$PROMPT dirty"
    fi

    echo "$PROMPT"
}


# Note that single quotes (') are used here.
# If you use double quotes (") instead, command substitution will be performed during the config file load time.
export PS1='[\u @ \h :: \w]\n$(__cstm_git_ps1) ► ' # This won't work with zsh

If you use zsh, set the PROMPT_SUBST option, and your prompt to this:

setopt PROMPT_SUBST # Otherwise, the `$(__cstm_git_ps1)` substitution below will be printed as-is

export PS1=$'[%n @ %m :: %d]\n$(__cstm_git_ps1) ► '

Where I Place My $PS1

Bash

Just put this into your ~/.bashrc, and you’re good to go.

Don’t forget to reload your config.

Korn Shell

For this one, I prefer to store configuration in a ~/.kshrc file. This is a conventional name, and the shell doesn’t load this file by default.

So, an additional step is required.

# This piece of code in your ~/.profile ensures ksh loads its config from ~/.kshrc
if [ "$SHELL" = /bin/ksh ]; then
    export ENV=$HOME/.kshrc
fi

Information on this particular env variable is present in the man page description section.

Again, don’t forget to reload your config.

The Result

The resulting prompt looks like this:

[username @ resolv :: ~]
 ► uname -s
OpenBSD
[username @ resolv :: ~]
[username @ resolv :: /tmp/testrepo]
 (main) ►

The End

Here you go! Good luck hacking, and have fun customizing your environment!


Thank you for reading, and have a good rest of your day! (^ ~ ^ )

If you have any questions/suggestions, or found an error, contact me!