-->

How to color a prompt on FreeBSD/cshrc?

2020-05-21 05:34发布

问题:

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.

回答1:

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%}



回答2:

To my knowledge FreeBSD comes with tcsh by default. Have a look at the examples.

Another list for other shells as well (bash, csh, tcsh, ksh, etc.) is available. Taken from that link and tested with tcsh (I do not have csh installed):

To color the prompt you will want to place this symbol in your prompt. %{\033[Xm%}.

Certain colors need a semicolon in order to appear. Yellow […] is 1;33 do not use just 33 or it will come out brown. If you have a 0;31 you don't need to place the 0.

The colours are ANSI. Have a look at the ANSI colours list; simply replace X with the colour code.

X = 0 resets the colours: %{\033[0m%}.



回答3:

# Add these lines to your ~/.cshrc.mine file on the linux grace machines...

# Colors!
set     red="%{\033[1;31m%}"
set   green="%{\033[0;32m%}"
set  yellow="%{\033[1;33m%}"
set    blue="%{\033[1;34m%}"
set magenta="%{\033[1;35m%}"
set    cyan="%{\033[1;36m%}"
set   white="%{\033[0;37m%}"
set     end="%{\033[0m%}" # This is needed at the end... :(

# Setting the actual prompt

set prompt="${green}%n${blue}@%m ${white}%~ ${green}%%${end} "

# Clean up
unset red green yellow blue magenta cyan yellow white end


标签: freebsd csh tcsh