How to color a prompt on FreeBSD/cshrc?

心不动则不痛 提交于 2019-12-03 00:19:07
mpen

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!