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

Create a multi-line Terminal prompt UNIX
Here's a prompt formatting tip for tcsh: Several tips on this site have helped this glistening wet Unix newbie customize my prompt with command history, time, and directory info, as well as colors. However, as the filepath expands when I'm navigating the tree, any long commands I type at the prompt sometimes get wrapped to another line. I wondered if it were possible to have the "%" symbol appear at the beginning of a new line at all times, so out of idle curiosity one day I put a carriage return ("\n") in my prompt, and to my vast surprise, it worked perfectly. I inserted a carriage return at the beginning of the prompt string as well, so it stands alone on a line by itself, as a sort of informational header, while the percent symbol and cursor appear at the beginning of the next line.

Read the rest of the article for the prompt string, as well as a brief tutorial on ANSI codes and their use in the prompt command...

My Terminal window is set to black with 40% transparency, and I find that Courier Bold Oblique 14 pt. set to cyan is the most legible font for me. My prompt is yellow and white:
set prompt = "\n%{\033[1;93m%}>>>%{\033[0;93m%}[%{\033[0;37m%}\\!\!%{\033[0;93m%}]
[%{\033[0;37m%}%D%w%y%{\033[1;93m%}%P %{\033[0;37m%}EST%{\033[0;93m%}][%{\033[0;37m%}%n
%{\033[0;93m%}@%{\033[0;37m%}%m%{\033[0;93m%}][%{\033[0;93m%}%c03]\n\n%#%{\033[0m%} "
[Author's Note: this is all a single line. There are spaces surrounding the "=" sign, and a single space before the closing double quotation mark. Put in ~/.tcshrc or ~/Library/init/tcsh/environment.mine.]

Here's a quick tutorial on color and ANSI codes. Literal strings are enclosed with %{ and %}. ANSI graphics mode begins with \033[ and ends with m. ANSI color codes are placed between \033[ and m, separated by semi-colons. Thus
%{\033[n;n;nm%}
where n is one of the codes from the list below:

ANSI Color codes
0 = default colour
1 = bold
4 = underlined
5 = flashing text
7 = reverse field
31 = red
32 = green
33 = orange
34 = blue
35 = purple
36 = cyan
37 = grey
40 = black background
41 = red background
42 = green background
43 = orange background
44 = blue background
45 = purple background
46 = cyan background
47 = grey background
90 = dark grey
91 = light red
92 = light green
93 = yellow
94 = light blue
95 = light purple
96 = turquoise
100 = dark grey background
101 = light red background
102 = light green background
103 = yellow background
104 = light blue background
105 = light purple background
106 = turquoise background

So the string
%{\033[0;1;5;44;93m%}%n
would reset your user name (%n) to default (0), then set it to bold (1), flashing (5), yellow (93) on a blue background (44).
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[9,454 views]  

Create a multi-line Terminal prompt | 10 comments | Create New Account
Click here to return to the 'Create a multi-line Terminal prompt' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
thanks
Authored by: spnyc on May 17, '02 10:17:10AM

that rocked. i've been wanting to add color to my shell for a while



[ Reply to This | # ]
cool, can we reset
Authored by: brettc on May 17, '02 11:24:27AM

I want to try this one, but be able to reset it back easily if not appropriate for my workflow. What's the best command to put it back to normal, or is "set prompt" for this session only. Sorry to be a Unix dummy...



[ Reply to This | # ]
Re:cool, can we reset
Authored by: Viridian on May 17, '02 12:22:56PM

brettc,

I'm a complete Unix newbie, and don't know of an easy way to switch prompts. I understand that it's possible in bash, zsh and other shells with better scripting capabilities than tcsh. I keep several in ~/Library/init/tcsh/.environment.mine and comment out the ones I'm not using.



[ Reply to This | # ]
Re:cool, can we reset
Authored by: Viridian on May 17, '02 12:24:59PM

brettc,

I'm a complete Unix newbie, and don't know of an easy way to switch prompts. I understand that it's possible in bash, zsh and other shells with better scripting capabilities than tcsh. I keep several in ~/Library/init/tcsh/.environment.mine and comment out the ones I'm not using.



[ Reply to This | # ]
cool, can we reset
Authored by: Riken on May 17, '02 01:14:00PM

The standard prompt is:

set prompt = "[%m:%c3]"

If you don't put a set prompt command in a startup file it only applies for that terminal session.



[ Reply to This | # ]
cool, can we reset
Authored by: Riken on May 17, '02 01:38:24PM

Sorry should be:

set prompt = "[%m:%c3] %n%# "



[ Reply to This | # ]
Not working for me...
Authored by: jiclark on May 17, '02 12:27:42PM
I'm afraid you've got another Unix newbie here. I can't figure out how to get any of this to stick.

Could someone post links to the other hints that tell us how to customize the Terminal, as well as any other sites that might be of help?

Many TIA,
John-o

[ Reply to This | # ]
Not working for me...
Authored by: Riken on May 17, '02 01:18:48PM

I don't know any links (maybe try searching macosxhints for unix help) but to make the command 'stick' edit ~/Library/init/tcsh/.environment.mine and put the command there.

If you don't know how to edit under unix just type:

pico filename

Pico is easy to use and includes help.



[ Reply to This | # ]
explore man tcsh
Authored by: mervTormel on May 17, '02 02:50:18PM

explore man tcsh for prompt options that will help forego much code wrangling:

set prompt

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


shell special variables

ellipsis (+)
If set, the `%c'/`%.' and `%C' prompt sequences
(see the prompt shell variable) indicate skipped
directories with an ellipsis (`...') instead of
`/<skipped>'.

aliases

precmd Runs just before each prompt is printed. For
example, if one does

> alias precmd date

then date(1) runs just before the shell prompts
for each command. There are no limits on what
precmd can be set to do, but discretion should be
used.



[ Reply to This | # ]
explore man tcsh
Authored by: lavacano on May 18, '02 02:55:53AM

using some ideas from this hint, and some from previous hints, I set my prompt to look like this:

>>> (05/18/02 6:35:13am) [user@host]
current path
prompt (%, #)

All color coded of coarse.

set prompt="\n%{\033[1;31m%}>>> (%{\033[34m%}%W/%D/%y} %{\033[0m%}%p%{\033[1;31m%}) [%{\033[0m%}%n%{\033[1;31m%}@%{\033[36m%}%M%{\033[1;31m%}]\n%{\033[1;37m%}%~\n%{\033[0m%}%# "

I find, to my tastes, this prompt looks the best with the colors from the SGI-ish theme for GLTerm (Blueish back, yellow text, blue cursor, purple selection) with heavier than average translucency, as the colors are easy to differentiate between.



[ Reply to This | # ]