Is there a way for the command line and the output to be in different colors? For example, I've been playing with the PS1 value so my prompt is in red, but I'd like to have the command line in green and the output in white (ex: if I type "ls", that's in green, and then the file list is in white).
3 Answers
At the end of your PS1 have an escape sequence set the color of your input. For example,
PS1="\h \w$ \e[0;32m"
(all that matters is the \e[0;32m
part at the end (which could be any color), the rest is up to you)
To set the output color differently, you need to "trap" the "DEBUG" signal and call some function. Here's what I have
debug()
{
echo -n $'\e[0m';
}
trap debug DEBUG
It's not complicated, it just ensures that whenever you enter a command (which causes the DEBUG signal) the debug
function is called.
So just copy those lines into your .bash_profile or .bashrc or whatever you're using, and change the '\e[0m' to whatever color you like.
-
The
PS1="\h \w$ \e[0;32m"
line causes problems with line wrapping in OS X. Surround ANSI escapes with\[
and\]
to solve. In other words:PS1="\h \w$ \[\e[0;32m\]"
– MalströmOct 28, 2014 at 12:47
You need to have the shell, or a program, emit the appropriate escape sequence to change the colours after you press enter.
The bash shell used by current OS X doesn't have an obvious facility for doing this.
-
Is there any way to alias a return key to be "color escape sequence + return"?– punkkatJun 27, 2012 at 20:38
You could bind another key to send the escape sequence and a return.
- In Terminal's preferences, click on the Settings pane, then the Keyboard tab.
- Click the plus button.
- Choose a modifier and key. Set the action to
send string to shell
and the string to\033[m\015
. Click OK.
You'll have to get used to using that keyboard shortcut instead of return…although there's probably an app that will let you the system treat return in Terminal as that alternate shortcut.