【问题标题】:Can I change the input color of my Bash prompt to something different than the terminal default? [closed]我可以将我的 Bash 提示的输入颜色更改为不同于终端默认值的颜色吗? [关闭]
【发布时间】:2013-05-19 14:57:02
【问题描述】:

我的默认终端颜色是灰色,这很好。

我的 Bash 提示显示一堆颜色,这很好用:

PS1="${COLOR_RED}\u${COLOR_WHITE}@${COLOR_RED}${COMPUTERNAME} ${COLOR_BLUE}\w${GITPROMPT} ${COLOR_RESET}"

但是我在提示符末尾输入的文本是灰色的。我希望它是白色的(ANSI 代码“[37m”)。

如果我在提示符末尾添加 COLOR_WHITE 而不是 COLOR_RESET,则默认终端颜色会变为白色,直到它被重置。这会产生一些灰色文本的奇怪效果,一些白色文本在顶部渗出。

如何将 Bash 提示的“输入文本”颜色更改为终端默认颜色以外的颜色?

【问题讨论】:

标签: linux bash unix dotfiles


【解决方案1】:

只需添加以下行:

export PS1=" \[\033[34m\]\u@\h \[\033[33m\]\w\[\033[31m\]\[\033[00m\] $ "

预览:

这些是我喜欢的颜色。您可以通过更改作为 ANSI 颜色代码的 m 代码(例如 34m)来自定义提示颜色的每个部分。

ANSI 颜色代码列表:

  • 黑色:30m
  • 红色:31m
  • 绿色:32m
  • 黄色:33m
  • 蓝色:34m
  • 紫色:35m
  • 青色:36m
  • 白色:37m

【讨论】:

    【解决方案2】:

    试试这个。比较简单:

    export PS1="\e[0;32m\t \e[32;1m\u@\h:\e[0;36m\w\e[0m$ "
    

    【讨论】:

    • 当我将它添加到我的 bash_profile 时,这似乎没有任何作用
    • 你用的是什么外壳? (回声 $SHELL)。这仅适用于 bash。你也可以尝试直接在命令行输入。
    【解决方案3】:

    我建议更改终端模拟器的设置。

    看来您正在使用iTerm2(如果您使用的是 iTerm,我建议您查看 iTerm2),所以:

    设置配置文件您的配置文件颜色。在“基本颜色”下,调整“前景”。

    如果只是改变输入文本的颜色,在 Z shell (zsh) 中你可以使用一个

    preexec () { echo -ne "\e[0m" }
    

    Source 1

    我找到了一种用 Bash 尝试这个的 hack-ish 方法:

    不是原生的,但可以使用 DEBUG 陷阱破解。 This code 设置 preexec 和 precmd 功能类似于 zsh。命令行作为单个参数传递给 preexec。

    这是一个简化版本的代码,用于设置在运行每个命令之前执行的 precmd 函数。

    preexec () { :; }
    preexec_invoke_exec () {
        [ -n "$COMP_LINE" ] && return  # do nothing if completing
        local this_command=$(history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g");
        preexec "$this_command"
    }
    

    陷阱'preexec_invoke_exec'调试
    这个技巧归功于Glyph Lefkowitz;感谢 [bcat] 找到原作者。

    http://www.macosxhints.com/dlfiles/preexec.bash.txt

    Source 2

    【讨论】:

    • 我明白这一点。但是,我正在尝试将“输入文本”颜色更改为与前景不同的颜色。可能不是这样做的方法,但我想我会问:)
    【解决方案4】:

    我发现在我安装的Debian 8 (Jessie) 中我已经有了这个,但默认情况下是注释掉的。这是

    # uncomment for a colored prompt, if the terminal has the capability; turned
    # off by default to not distract the user: the focus in a terminal window
    # should be on the output of commands, not on the prompt
    force_color_prompt=yes
    
    if [ -n "$force_color_prompt" ]; then
        if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
            # We have color support; assume it's compliant with Ecma-48
            # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
            # a case would tend to support setf rather than setaf.)
            color_prompt=yes
        else
            color_prompt=
        fi
    fi
    
    if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    fi
    unset color_prompt force_color_prompt
    

    我刚刚取消了force_color_prompt=yes 的注释。

    如果您的 .bashrc 文件中还没有这个,那么您可以复制它并将其粘贴到您的文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-24
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      相关资源
      最近更新 更多