Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Modify your terminal shell prompt UNIX
The tcsh shell (the one that launches when you start a terminal) has a prompt which looks like this:

[machinename:~] username%

You can modify the prompt with the set prompt = command (subtle, I know!). Over on the MacNN forums, this thread includes a great tutorial (written by blanalex) on how to generate some unique and useful prompt strings, including those with colored foregrounds and backgrounds.

I've reproduced the final how-to from the discussion in the remainder of this article, in case the MacNN posting goes away anytime soon.

Posted by blanalex on 01-05-2001 02:26 PM            
------------------------------------------------------------------------
"Here's the quick-and-dirty tcsh prompt how-to!

1- The first thing I suggest is to consult the man page of tcsh, in the ``Special Shell Variables'' somewhat near the end. There you will see what are the special symbols that can be used in the prompt. The most used symbols are:

%/ Current working directory
%~ Same as the above, but it will replace /Users/Username with ~
%M FQDN (fully qualified domain name, such as panoramix.scoeur.csvdc.qc.ca)
%m domain name (the 'panoramix' part in the adress above)
%t,%T time of the day in 12- and 24-hour format respectively
%p,%P time of the day, with seconds, in 12- and 24-hour format respectively
%n user name
%# 'default' character prompt:
expands to '$' for normal users
expands to '#' for root user

2- Using those symbols you can start to play a bit:
set PROMPT="[%m:%c3] %n%#"
is the default prompt is tcsh
set PROMPT="[%T][%n@%m %c3] %#"
gives my favorite prompt, such as: [13:40][blanalex@panoramix ~] $

To save your new prompt, you just have to write your set PROMPT= in ~/.tcshrc, and now each time you will open a terminal window, your prompt will be displayed.

3- Colors
Now when you want to add colors to your prompt, you have to use ANSI sequences. Those are a bit more complicated to use, but we'll get through.

There is many ANSI commands. There's some commands to move the cursors, others to assign command to keys, others to set the title of the terminal window, other to set the graphic mode and many, many others.

For changing the graphic mode (mainly the color and the background of the text), the ANSI command is 'm'.

For using ANSI commands in your prompt, you must inclose them between %{ and %} to tell tcsh that the text between those two delimiters will not be displayed. All ANSI sequences begins with \033[ and there is no special ending delimiter other than the command itself

So for example, if you want your prompt to be blue, you would have to do:
set PROMPT="%{\033[34m%}[%T][%n@%m %c3] %#"

First, you see the %{\033[34m%} at the beginning to put the text in blue and you may remark near the end the %{\033[0m%} to reset the graphic mode and return to normal (grey) text.

The color codes you can use with this ANSI command are the following:
0 Reset colors to 37;40
1 Bold text. In reality, it's not really bold, text is only brighter.
5 Blinking text. Many terminal apps don't support this
7 Reverse video, inverts the background and the foreground colors
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White
40 Black background
41 Red background
42 Green background
43 Yellow background
44 Blue background
45 Magenta background
46 Cyan background
47 White background

You can combine many color codes in the same command by separating them with a semi-comma (';'), for example, to have a bright blue text on red background you would insert this : %{\033[1;34;41m%}

That's it for this how-to, for more information, I suggest you to read the Bash Prompt Howto, because, almost all the magic is the same between the two, only some variable changes."
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[57,008 views]  

Modify your terminal shell prompt | 9 comments | Create New Account
Click here to return to the 'Modify your terminal shell prompt' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Where to put this
Authored by: ClarkGoble on Apr 04, '01 09:56:18PM

Just a reminder to people who aren't familiar with where OSX puts everything. To make these sorts of shell changes the standard is to create a ~/Library/init/tcsh directory. Then put all these changes in the rc.mine file. (Make sure you save it as text only if you are using Text Edit instead of a unix editor)

For more informatoin on this read the files in /usr/share/init/tcsh which has all the default shell scripts.

BTW - as mentioned in a tip somewhere else here: put aliases in aliases.mine



[ Reply to This | # ]
Additional symbols
Authored by: Kemul on Aug 08, '02 02:27:54PM

%d = day of the week (Mon, Tue, Wed, etc)
%w = month of the year (Jan, Feb, Mar, etc)
%D = date (01-31)
%Y = year (YYYY format)



[ Reply to This | # ]
A litte whitespace for shell prompt
Authored by: cfoster on Dec 05, '02 02:08:34PM

I've found very useful to add:

\n===== (60 of these) =======\n\n

before my other 'set prompt' details. It adds some whitespace around each shell command, making them much easier to differentiate. (I often find shell commands run together and I'm not sure where one starts and the next stops.)

Try it, you'll like it!
-Colin.



[ Reply to This | # ]
Modify your terminal shell prompt
Authored by: proproject on Mar 08, '03 10:05:01AM

So how do I get to a shell in the first place? Unix power user here, but
never owned a mac before.



[ Reply to This | # ]
Modify your terminal shell prompt
Authored by: ranolf on Mar 08, '03 08:00:05PM

  1. Open a Finder window.
  2. Click on the Applications icon
  3. Double-click the Utilities directory
  4. Double-click on the Terminal application
  5. Drag the Terminal icon to the dock and drop it there so you don't have to find it again.

Alternatively, if you have your login prompt setup so that you can type in the login name (as opposed to the mode where you pick it from a list), type >console and hit enter for console mode.

Can also get console mode on startup by holding down option-s or command-s (can't remember which).



[ Reply to This | # ]
Modify your terminal shell prompt
Authored by: muaddib13 on Mar 10, '03 06:07:30PM

launch Terminal.app located in hard drive/applications/utilities



[ Reply to This | # ]
Modify your terminal shell prompt
Authored by: meekish on Jul 29, '07 03:18:34PM

Are you familiar with any color codes besides the ANSI standard? I would like to beautify my shell, but find the 16 colors quite limiting. I was told in #bash on freenode (irc) that the OS X terminal simply wraps an abstraction layer around the ANSI control sequences and may have the ability to render more colors than those provided by the ANSI standard. Is this so?



[ Reply to This | # ]
Modify your terminal shell prompt
Authored by: rafmagana on Jul 23, '10 10:56:54AM
I made a kind of DSL in Ruby to generate valid strings that represent colored text in the terminal, I use it to generate my prompt: http://github.com/rafmagana/geek_painter

[ Reply to This | # ]
Modify your terminal shell prompt
Authored by: agitatedString on Oct 30, '11 02:11:20AM

Is there anything uglier and harder to read and more tedious to modify than ANSI color codes 5 minutes after you have written them?

Is there anything more annoying than ANSI color code spewage in terminals that do not accept them?

Below is what I have come to prefer over raw ANSI color codes. This code (below) has been scraped off the web here and there with miscellaneous changes made that I have long since forgotten. As you can see below the PS1 variable value now almost reads like an English sentence.

I keep the code below near the top of my ~/.bashrc file

START ~/.bashrc code:

#### Smooth the way for DTerm and possibly other terminals that barf on tput colors:
INTERACTIVETERM=-YES-
if [ "$TERM" == "" ]; then INTERACTIVETERM="-NO-"; TERM="vt100"; fi
if [ "$TERM" == "dumb" ]; then INTERACTIVETERM="-NO-"; TERM="vt100"; fi
export INTERACTIVETERM
# Set up TPUT color codes
if [ "$INTERACTIVETERM" == "-YES-" ]; then
    tReset="$(tput sgr0)"
    NORMAL=$(tput sgr0)
    tBold="$(tput bold)"
    BOLD=$(tput bold)
    BRIGHT=$(tput bold)
    tBlack="$(tput setaf 0)"
    BLACK=$(tput setaf 0)
    tRed="$(tput setaf 1)"
    RED=$(tput setaf 1)
    tGreen="$(tput setaf 2)"
    GREEN=$(tput setaf 2)
    tYellow="$(tput setaf 3)"
    YELLOW=$(tput setaf 3)
    pYELLOW="$(tput setaf 190)"
    LIME_YELLOW=$(tput setaf 190)
    tBlue="$(tput setaf 4)"
    BLUE=$(tput setaf 4)
    POWDER_BLUE=$(tput setaf 153)
    tPink="$(tput setaf 5)"
    MAGENTA=$(tput setaf 5)
    tCyan="$(tput setaf 6)"
    CYAN=$(tput setaf 6)
    tGray="$(tput setaf 7)"
    WHITE=$(tput setaf 7)
    tWhite="$(tput setaf 8)"
    TUNON="$(tput smul)"
    TUNOFF="$(tput rmul)"
    BLINK=$(tput blink)
    REVERSE=$(tput smso)
    UNDERLINE=$(tput smul)
    tRandColor=$(tput setaf $(random /0..8/))
    #tRandColor="$(tput setaf $(( $(hostname | openssl sha1 | sed 's/.*\([0-9]\).*/\1/') % 6 + 1 )) )"
else
    tReset=
    tBold=
    tBlack=
    tRed=
    tGreen=
    tYellow=
    tBlue=
    tPink=
    tCyan=
    tGray=
    tWhite=
    tUndOn=
    tUndOff=
    tRandColor=
    BLACK=
    RED=
    GREEN=
    YELLOW=
    BLUE=
    MAGENTA=
    CYAN=
    WHITE=
    LIME_YELLOW=
    POWDER_BLUE=
    BRIGHT=
    NORMAL=
    BLINK=
    REVERSE=
    UNDERLINE=
    BOLD=
fi

export PS1='\[${tRed}\][\!]\[${tBlue}\]\W\[${tReset}${tGreen}\]$(__git_ps1 "[%s]")${tReset}⇶ ' # using this with git-completion.bash

END ~/.bashrc code.

Things like, ${tBold}${tGreen} blah blah blah ${tReset}, work well in here-documents as well. Colors become much easier to use in your bash scripts too:

printf "%s/%s : ${tRed}%s${tReset}\n" "$PWD" "$meat" $(mdls -name kMDItemKind -raw "$meat");



[ Reply to This | # ]