无需插件即可自定义 bash 提示符

无需插件即可自定义 bash 提示符

我正在自定义 bash 提示符,以使其看起来更漂亮一些。这是我所做的(在 ~/.bashrc 文件中,Ubuntu 20.04)。

# Customizing prompt
t1_bg="\[$(tput setab 31)\]"
t1_fg="\[$(tput setaf 31)\]"
t2_bg="\[$(tput setab 69)\]"
t2_fg="\[$(tput setaf 69)\]"
RESET="\[$(tput sgr0)\]"

triangle_1=$(echo -e "${t1_fg}${t2_bg}\uE0B0${RESET}")
triangle_2=$(echo -e "${t2_fg}\uE0B0")

PS1="${t1_bg} \u${RESET}${triangle_1}${t2_bg} \w ${RESET}${triangle_2}${RESET} "

在此输入图像描述

如图所示,三角形有点偏离,并且文本没有垂直居中。此外,我希望提示和输出之间有更多的间距。有什么办法可以解决这些问题吗?

最后,为什么窗口标题栏不显示当前目录?我只修改了 PS1 变量。

答案1

正如 frabjous 在评论中所说,三角形的外观取决于字体。我安装了 hack-ttf 字体(按照建议合成壳)并且问题得到解决。安装(至少在 Ubuntu 上)可以通过以下方式完成:

sudo apt install fonts-hack-ttf

为了修复标题栏,我在 .bashrc 的末尾添加了:

case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

当先前在 bashrc 上也找到相同的代码时,我无法理解这是如何解决问题的。一般来说,bashrc 上添加的最终代码是:

# Customizing prompt
t1_bg="\[$(tput setab 31)\]"
t1_fg="\[$(tput setaf 31)\]"
t2_bg="\[$(tput setab 69)\]"
t2_fg="\[$(tput setaf 69)\]"
RESET="\[$(tput sgr0)\]"

triangle_1=$(echo -e "${t1_fg}${t2_bg}\uE0B0${RESET}")
triangle_2=$(echo -e "${t2_fg}\uE0B0")

case "$TERM" in
    xterm*|rxvt*)
        PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
        ;;
    *)
        ;;
    esac

相关内容