Change color of input text, not output

5,243

You can use a trap to achieve this: trap 'echo -ne "\e[0m"' DEBUG

According to bash's man: a trap on DEBUG executes

before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function

So every time you execute the command, the shell will insert \e[0m disabling the previous color code. Only to have it re-set via the prompt, once the command has executed.

I found the admitedly hackish trick here: https://wiki.archlinux.org/index.php/Color_Bash_Prompt#Different_colors_for_text_entry_and_console_output

Share:
5,243
Dr. Gianluigi Zane Zanettini
Author by

Dr. Gianluigi Zane Zanettini

Updated on September 18, 2022

Comments

  • Dr. Gianluigi Zane Zanettini
    Dr. Gianluigi Zane Zanettini almost 2 years

    I need to capture screenshots of the terminal where the typed commands visually "stand out" from the rest. Right now I'm using this to make my typed commands "red":

    PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[1;34m\]\w\[\e[m\] \[\e[0;34m\]\$ \[\e[m\]\[\e[0;31m\]'
    

    The problem is: everything after the prompt is red, not only my typed text

    red linux output

    How can I fix this?

    I'm using bash (CentOS 7).

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 9 years
    No, this only changes the colors of the prompt but leaves the command line and the output from programs in the same color.
  • JackyJohnson
    JackyJohnson over 5 years
    PS1 is the prompt, what the question is asking is the input text. It's completely different.
  • Bratchley
    Bratchley over 4 years
    Otherwise this seems pretty close to a "good enough" solution. I just can't have my terminal's colors flail around like that since I use those shortcuts often.