我可以将 bash 提示的输入颜色更改为与终端默认值不同的颜色吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16636007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:28:06  来源:igfitidea点击:

Can I change the input color of my bash prompt to something different than the terminal default

linuxbashunixdotfiles

提问by dylanized

My default terminal color is gray, that's fine.

我的默认终端颜色是灰色,没关系。

My bash prompt displays a bunch of colors, this works fine:

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

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

See example: http://cl.ly/image/002f210X1f1u

参见示例:http: //cl.ly/image/002f210X1f1u

But the text I type in, at the end of the prompt, is gray. I want it to be white (ANSI code "[37m").

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

If I add a COLOR_WHITE at the end of the prompt, instead of the COLOR_RESET, then the default terminal color changes to white until it is reset. This makes a weird effect of some gray text, with some white text bleeding through at the top.

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

See example of problem: http://cl.ly/image/1Z3g3v0e083B

请参阅问题示例:http: //cl.ly/image/1Z3g3v0e083B

How can I change the "input text" color of the Bash prompt, to something other than the terminal default color? Thanks for any suggestions!

如何将 Bash 提示的“输入文本”颜色更改为终端默认颜色以外的颜色?感谢您的任何建议!

回答by Shayan Amani

Simplyadd the following line:

只需添加以下行:

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

Preview:enter image description here

预览:在此处输入图片说明

This is my preferred colors. You can customize each part of prompt's color by changing mcodes (e.g. 34m) which are ANSI color codes.

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

List of ANSI Color codes:

ANSI 颜色代码列表:

  • Black: 30m
  • Red: 31m
  • Green: 32m
  • Yellow: 33m
  • Blue: 34m
  • Purple: 35m
  • Cyan: 36m
  • White: 37m
  • 黑色:30m
  • 红色:31m
  • 绿色:32m
  • 黄色:33m
  • 蓝色:34m
  • 紫色:35m
  • 青色:36m
  • 白色:37m

回答by dlink

Try this one, it is simpler:

试试这个,它更简单:

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

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

回答by santiago arizti

I found that in my installation of Debian 8 I already have this but it is commented by default, it is

我发现在我的 Debian 8 安装中我已经有了这个,但默认情况下它被评论,它是

# 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)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[00m\]$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$ '
fi
unset color_prompt force_color_prompt

I just uncommented the line that says force_color_prompt=yes

我只是取消了对说的行的注释 force_color_prompt=yes

If you didn't already have this in your .bashrc then you can copy this and paste it in yours.

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

回答by demure

I would suggest changing your terminal emulator's settings. It appears you are using iTerm2 (if you are on iTerm, I suggest looking at iTerm2), so:

我建议更改终端模拟器的设置。看来您正在使用 iTerm2(如果您使用 iTerm,我建议您查看 iTerm2),因此:

Settings -> Profiles -> Your Profile -> Color. Under 'basic colors' adjust 'foreground'

设置 -> 个人资料 -> 你的个人资料 -> 颜色。在“基本颜色”下调整“前景”

For just changing the color of the input text, in zsh you could use a

仅更改输入文本的颜色,在 zsh 中,您可以使用

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

Source 1

来源 1

I have found a hack-ish way to try this with bash:

我找到了一种使用 bash 尝试此操作的 hack-ish 方法:

Not natively, but it can be hacked up using the DEBUG trap. This codesets up preexec and precmdfunctions similar to zsh. The command line is passed as a single argument to preexec.

Here is a simplified version of the code to set up a precmd function that is executed before running each command.

不是原生的,但可以使用 DEBUG 陷阱破解它。此代码设置 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"
}

trap 'preexec_invoke_exec' DEBUG This trick is due to Glyph Lefkowitz; thanks to [bcat] for locating the original author.

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

trap 'preexec_invoke_exec' DEBUG 这个技巧是由于Glyph Lefkowitz;感谢 [bcat] 找到原作者。

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

Source 2

来源 2