Score:4

Text color in Terminal window

us flag

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.

Score:5
cn flag

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

ve3oat avatar
us flag
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!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.