How to color a prompt on FreeBSD/cshrc?

mpen picture mpen · Aug 21, 2012 · Viewed 28.6k times · Source

I'm being put in charge of managing a bunch of servers, I want to set up my prompts on each of them so that I don't get confused as to where I am logged in to.

I've edited my .cshrc files and put this in them:

set prompt=`whoami`@`hostname -s`:$cwd'$ '

But I'd like to color that prompt so it stands out a bit more. Maybe green with white text or something. How can I do that? I'm not very familiar with the shell syntax.

I'm SSH-ing in from the standard terminal that comes with Ubuntu, if that's relevant.

Answer

mpen picture mpen · Aug 22, 2012

This page has a pretty good explanation, although the syntax is a bit different in csh. Here's what I came up with:

set prompt="%{\e[32;1m%}%n%{\e[37m%}@%{\e[33m%}%m%{\e[37m%}:%{\e[36m%}%~%{\e[37m%}"\$"%{\e[0m%} "
# root variation:
set prompt="%{\e[31;1m%}root%{\e[37m%}@%{\e[33m%}%m%{\e[37m%}:%{\e[36m%}%/%{\e[37m%}#%{\e[0m%} "

update: the previous prompt I had here didn't actually update when you changed directories. using %n, %~ and %m instead of $cwd or pwd actually update. see here.

%{ ... %} means the stuff between should take 0-width
\e[ ... m specifies the colors and bolding. \e escapes the [ which seems to be necessary (I believe it's equivalent to \033), the m signifies the end.

Use 0 as your color to reset to default.

If you want to set a color and background, simply separate the numbers with semi-colons. Use 1 to enable bolding.

Consult this table to choose your colors:


(source: funtoo.org)

So for example, "Hello World" in bold, cyan on a red background would be %{\e[36;41;1m%}Hello World%{\e[0m%}