4

Is it possible to have a different color for the text that I type from the text for Terminal responses? I have adjusted the text colors using Terminal's Preferences > Unnamed > Colors facility, but can not find any way to have separate colors for my typing and Terminal's typing. Thanks.

1 Answer 1

5

Yes, it is possible. You need to change the value of the PS1 variable, which is what sets your prompt (often something like user@ubuntu /home/ $). Edit ~/.bashrc and find this section:

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

Next, you need to decide what color you want. I will use bold for this example, see here for more options. So, the escape code for bold is \e[01m and we need to add that to the end of the PS1 variable so that everything printed after the prompt (so, anything you type) will be bold.

Then, we need to somehow turn it off again before the response is printed, and we can do that by "trapping" the "reset color" command, which is \e[0m to the DEBUG signal, making it be executed whenever you execute a command.

I realize this is not very clear, but this is what you want to end up with:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \e[01m'
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

trap 'printf "\e[0m" "$_"' DEBUG

This should give you a situation like this where what you type is in bold, but what the terminal returns is not:

screenshot showing the bold and non bold text

1
  • Thank you, @terdon, for that very clear solution. With some trepidation I edited .bashrc and your solution works beautifully! Now my Terminal sessions are much easier to read and I won't get lost when going back to review what has been done during a long session. Thanks again!
    – ve3oat
    Jan 30 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.