10

I know how to change the color of ls output, but I would like to change the color of all terminal output.

To make clear what I mean, below is a picture of my terminal. My text color is orange.

However, I would want the -bash: p: command not found, test, and the output of df to be a different color, so that it is easier to see the difference between what I type. Is this possible?

enter image description here


This is how it looks with @jmhindle answer. You can change the number after setaf for a different color (e.g. 2 for green).

enter image description here

3
  • 1
    How did you set the Blue and Green text in your screenshots?
    – pkamb
    Apr 4, 2020 at 8:24
  • This is so long ago I don't remember, sorry
    – Niek
    Apr 8, 2020 at 8:54
  • FYI for any other oh-my-bash users: you can just customize your PS1 shell prompt by following these instructions
    – Wassadamo
    Sep 26 at 19:39

1 Answer 1

5

I think you can do this by installing a DEBUG trap in ~/.bash_profile.

See the answer to this superuser question.

I added the following:

preexec() { 
    tput setaf 4
    :
}
preexec_invoke_exec() {
    [ -n "$COMP_LINE" ] && return
    local this_command=`HISTTIMEFORMATE= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//"`;
    preexec "$this_command"
}

trap 'preexec_invoke_exec' DEBUG

tput setaf 4 changes foreground colour to blue.

3
  • Thanks! I'll update my question to include how it looks now
    – Niek
    Dec 9, 2015 at 9:26
  • while :; do [ "${i:=0}" -lt 8 ] || i=0; tput setaf "$i"; sleep .5; : "$((i++))"; done & A good (and non-destructive) prank. ;) (To get rid of it—kill %1 followed by tput setaf 7.)
    – Wildcard
    Mar 18, 2016 at 4:35
  • For me this is changing all of the text color in terminal, both typed text and output.
    – pkamb
    Apr 4, 2020 at 8:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .