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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
Last Week in AI
Last Week in AI
The Cloudflare Blog
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
S
SegmentFault 最新的问题
量子位
大猫的无限游戏
大猫的无限游戏
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
I
InfoQ
人人都是产品经理
人人都是产品经理
G
Google Developers Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Engineering at Meta
Engineering at Meta

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Creating Beautiful Bash Prompts
2021-03-31 · via jdhao's digital space

This post summarize how to change the Bash shell prompt to a better look. The end result is shown in the title image.

Bash Special escape sequence#

To change the bash prompt, we can directly change PS1 variable. Some special escape sequences1 are predefined by Bash to have special meaning, e.g., \u means the current user name.

Set PS1 to [\u@\h \w] and start bash, the prompt looks like the following:

[jdhao@jdhao-MBP ~/Projects/trial_error]

Using colors#

Colors are important for us to create beautiful prompts. For example, echo -e "\033[4;32mtest\033[0m" will print green test with underline effect. In this command, \033 is the octal representation of ESCAPE control character, which starts an ANSI escape code to control the terminal behavior. You can also use \x1b, which is the hex value of ESCAPE control character. See this post on how to use colors in terminal.

A little trick I learned is to define a color for easier reuse:

GREEN="\e[0;32m"
COLOR_NONE="\e[0m"

When we want to use a color, we can now use the new name instead of the lengthy and obscure escape code.

GREEN="\e[0;32m"
COLOR_NONE="\e[0m"

printf "${GREEN}Green text${COLOR_NONE}"

Putting all together – the use of PROMPT_COMMAND#

We can of course write all the logic to set the PS1 variable in one command, but it could be messy and unreadable. Instead, we can set PS1 via the env variable PROMPT_COMMAND. The value of PROMPT_COMMAND will be executed by Bash as command each time before Bash tries to show the actual prompt.

The following Bash script shows my final working prompt configuration:

GRAY="\e[2;37m"
GREEN="\e[0;32m"
PURPLE="\e[1;35m"
COLOR_NONE="\e[0m"

# Detect whether the current directory is a git repository.
function is_git_repository() {
  git branch > /dev/null 2>&1
}

function set_git_branch () {
    # Note that for new repo without commit, git rev-parse --abbrev-ref HEAD
    # will error out.
    if git rev-parse --abbrev-ref HEAD > /dev/null 2>&1; then
        BRANCH=$(git rev-parse --abbrev-ref HEAD)
    else
        BRANCH="bare repo!"
    fi
}

function set_bash_prompt () {

    if is_git_repository; then
        set_git_branch
    else
        BRANCH=''
    fi

    PS1=""
    # set up user and host
    PS1+="${GRAY}\u@\h${COLOR_NONE} "
    # set up working directory
    PS1+="${GREEN}\w${COLOR_NONE} "
    # set up git branch
    PS1+="${GRAY}${BRANCH}${COLOR_NONE}\n"
    # set up prompt character
    PS1+="${PURPLE}>${COLOR_NONE} "
}

export PROMPT_COMMAND=set_bash_prompt

Ref: