[FAQ] - Setting the UNIX shell prompt

michaelsanford

Translator, Web Developer
Use these modifiers with set prompt = "[modifiers]" in ~/.tcshrc

Code:
 prompt  The string which is printed  before  reading  each
               command from the terminal.  prompt may include any
               of the following formatting sequences  (+),  which
               are replaced by the given information:

               %/  The current working directory.
               %~  The  current working directory, but with one's
                   home directory represented by  `~'  and  other
                   users' home directories represented by `~user'
                   as per Filename substitution.  `~user' substi-
                   tution  happens  only if the shell has already
                   used `~user' in a pathname in the current ses-
                   sion.
               %c[[0]n], %.[[0]n]
                   The  trailing component of the current working
                   directory, or n trailing components if a digit
                   n  is given.  If n begins with `0', the number
                   of skipped  components  precede  the  trailing
                   component(s)  in  the format `/<skipped>trail-
                   ing'.  If the ellipsis shell variable is  set,
                   skipped   components  are  represented  by  an
                   ellipsis so the whole  becomes  `...trailing'.
                   `~' substitution is done as in `%~' above, but
                   the `~' component  is  ignored  when  counting
                   trailing components.
               %C  Like %c, but without `~' substitution.
               %h, %!, !
                   The current history event number.
               %M  The full hostname.
               %m  The hostname up to the first `.'.
               %S (%s)
                   Start (stop) standout mode.
               %B (%b)
                   Start (stop) boldfacing mode.
               %U (%u)
                   Start (stop) underline mode.
               %t, %@
                   The time of day in 12-hour AM/PM format.
               %T  Like  `%t', but in 24-hour format (but see the
                   ampm shell variable).
               %p  The `precise' time of  day  in  12-hour  AM/PM
                   format, with seconds.
               %P  Like  `%p', but in 24-hour format (but see the
                   ampm shell variable).
               \c  c is parsed as in bindkey.
               ^c  c is parsed as in bindkey.
               %%  A single `%'.
               %n  The user name.
               %d  The weekday in `Day' format.
               %D  The day in `dd' format.
               %w  The month in `Mon' format.
               %W  The month in `mm' format.
               %y  The year in `yy' format.
               %Y  The year in `yyyy' format.
               %l  The shell's tty.
               %L  Clears from the end of the prompt  to  end  of
                   the display or the end of the line.
               %$  Expands the shell or environment variable name
                   immediately after the `$'.
               %#  `>' (or the first character of the promptchars
                   shell  variable) for normal users, `#' (or the
                   second character of promptchars) for the supe-
                   ruser.
               %{string%}
                   Includes  string as a literal escape sequence.
                   It should be  used  only  to  change  terminal
                   attributes  and  should  not  move  the cursor
                   location.  This cannot be the last sequence in
                   prompt.
               %?  The  return  code of the command executed just
                   before the prompt.
               %R  In prompt2, the  status  of  the  parser.   In
                   prompt3,  the  corrected  string.  In history,
                   the history string.

               `%B', `%S', `%U' and `%{string%}' are available in
               only eight-bit-clean shells; see the version shell
               variable.

               The bold, standout  and  underline  sequences  are
               often  used to distinguish a superuser shell.  For
               example,

                   > set prompt =  "%m  [%h]  %B[%@]%b  [%/]  you
                   rang? "
                   tut [37] [2:54pm] [/usr/accts/sys] you rang? _

               If `%t', `%@', `%T', `%p', or `%P'  is  used,  and
               noding  is  not  set,  then  print  `DING!' on the
               change of hour (i.e, `:00' minutes) instead of the
               actual time.

               Set by default to `%# ' in interactive shells.

       prompt2 (+)
               The string with which to prompt in while and fore-
               ach loops and after lines ending in `\'.  The same
               format  sequences may be used as in prompt (q.v.);
               note the variable meaning of `%R'.  Set by default
               to `%R? ' in interactive shells.

       prompt3 (+)
               The  string  with  which to prompt when confirming
               automatic spelling correction.   The  same  format
               sequences  may  be  used as in prompt (q.v.); note
               the variable meaning of `%R'.  Set by  default  to
               `CORRECT>%R (y|n|e|a)? ' in interactive shells.

This is just an excerpt from the tcsh manpage, but I thought it might be handy to have a dedicated FAQ.

Here are a few other useful threads:
- http://www.macosx.com/forums/showthread.php?s=&threadid=9091
- http://www.macosx.com/forums/showthread.php?s=&threadid=7133
- http://www.macosx.com/forums/showthread.php?s=&threadid=25512
 
I like to have my prompt on red and bold - and the rest of the text in normal black (on transparent white). So, red bold prompt

Code:
set prompt="%{\033[31m\033[1m%}[%T][%n@%m %c3] %#%{\033[0m%} "
 
Example: my ~/.tcshrc reads:

set prompt = "[%M:%B%~%b]%# "

Which gives a prompt of the following format:
[amras.no-ip.local.:~]%
[amras.no-ip.local.:/var/run]%

Decompose it:
- The full host name is displayed. (%M)
- The path relative to ~/ is displayed. (%~)
- The path is bolded (%B %b).
- The prompt character is either '%' for normal users or 'root#' ... for root (%#).

Also. I have a dynamic DNS client with no-ip.com so I set my local host name to amras.no-ip.local for consistency.

Giaguara, if you'd like to append more info on setting colours would be :cool: (I have bold colours configured in the terminal window settings, myself).
 
Colors:

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

How to set colors I remember having it seen here - but the format the set prompt code is given in teh article does not seem to work - thus after trying and trying and trying untill setting it to what I wanted exactly, the code was the one i posted below ( set prompt="%{\033[31m\033[1m%}[%T][%n@%m %c3] %#%{\033[0m%} " )

To have e.g. magenta prompt but similar just change the 31 to 35 - if you are lazy trying to decipher the full prompt code thing, just try e.g. bold and any color you like - it really helps to find what you were doing or what the machine answered while working on termianl. :p
 
Back
Top