Sponsors

Apple Store

Visit the official Apple Store to buy a Mac online. Free shipping!



Editors' Picks


Comments

Contact Us

Search

Top Posts

Categories

Recent Posts

Our Feeds


How to Customize your Terminal Prompt

Whether you use the Terminal occasionally or regularly, you might find it appropriate to change the way it looks beyond the color scheme and transparency settings. How about changing the actual command line prompt? It’s quite easy, and can be a fun way to further customize your Macs appearance. This how-to will involve some really minor usage of the command line, which we assume you’re somewhat familiar with if you are wanting to change the way it looks.


The default command line prompt in Mac OS X is something like:
ComputerName:CurrentDirectory User$ or MacBook:~/Desktop Admin$

Kind of boring and not the best, right? It’s quite easy to change. We’re going to assume you use the default bash shell, so you’ll be editing either the file .bashrc by default, or .profile if you installed fink. At the Terminal prompt, type: nano .bashrc

Now type: export PS1=" "

Between the quotation marks, you can add the following lines to customize your Terminal prompt:

  • \d - Current date
  • \t - Current time
  • \h - Host name
  • \# - Command number
  • \u - User name
  • \W - Current working directory (ie: Desktop/)
  • \w - Current working directory, full path (ie: /Users/Admin/Desktop)

So, let’s say you want your Terminal prompt to display the User, followed by the hostname, followed by the directory, the .bashrc entry would be:

export PS1="\u@\h\w$ "

which will look like:
Admin@MacBook~Desktop/$ "

Cool huh? You can also change the prompt itself to anything, it doesn’t have to be the $ sign, simply replace it with whatever else you’d like to use, : for example would be:

export PS1="\u@\h\w: "

which is the same as above, but : instead of $
Admin@MacBook~Desktop/: "

So, play around and see what you prefer. By the way, if you’re new to the Terminal text editor nano, you save a file by hitting control-o, and you exit the file and program by hitting control-x. You can also just use a standard text editor, like TextWrangler or TextEdit. There’s also a simple how-to designed to make your Terminal prompt resemble the one from the TV show LOST, but it is less useful in the real world, read that here if you are interested.

Comments:

Comments: 24

Comment from anonymous
Time: December 11, 2006, 11:25 pm

I think

\n

creates a line break in the prompt, too. It’s not ideal, I guess, but when you’re in a really deep directory, it’s good not to have the prompt all the way on the right edge of the screen, but it’s also nice to see the entire directory without asking (pwd) all the time.

Also play around with

\T

for another time format,

\s

for shell name

\v

for shell version. I am sure there are more, but enjoy!

Comment from nick
Time: December 12, 2006, 8:11 am

I like to update the title of the terminal window as well, so here’s mine as it stands currently:

case $TERM in
xterm* | aterm | rxvt | screen )
XTITLE=”\[\e]0;\u@\h (\w)\a\]” ;;
* )
XTITLE=”" ;;
esac
PS1=”$XTITLE”"[\u@\h:\w]\n\\$ ”

While I’m here and in case anyone uses GNU/screen, it’s worth mentioning: http://dischord.org/blog/2006/11/22/gnuscreen-and-dynamic-titles-for-ssh/

Comment from fak3r
Time: December 12, 2006, 8:23 am

This is not an OS X trick, in fact I post my .bashrc online: http://phil.cryer.us/code/dotfiles/bashrc and use it on FreeBSD/Linux at home, and Solaris, AIX, HP-UX at work. I’ve shared this with many co-workers and friends too, and it’s easy enough to modify if you need to. It gives output like this:

[09:51:54] [uname@hostname ~/home]$

I like having the time on each line, it makes it very easy to deal with logs to compare when an even happened. With 10 terms open having the uname and hostname listed is great, and the path is a standard, but this one truncates so it doesn’t push your $ all the way over to the side. I’ve tweaked this for years, but this is how I use it day to day.

HTH

f

Comment from Nick
Time: December 12, 2006, 9:17 am

A good PS1 for dark backgrounds, colorful and informative.

PS1=”\n33[1;33m-33[1;34m-(33[1;37m\u33[1;34m@33[1;33m\h33[1;34m)-33[1;33m-33[1;34m-(33[1;33m\w33[1;34m)-33[1;33m-\n\[33[1;33m\]-\[33[1;34m\]-\[33[1;33m\]-#\[33[1;37m\] “

Comment from Nate
Time: December 12, 2006, 10:23 am

This one looks pretty nice (I’ve been using it for years)
export PS1=’\u@\h[\w]\$ ‘

looks like this:
nhart@sectumsempra[/usr]$
where /usr changes to your current dir and the $ changes to # if you are r00t.

Comment from Gary
Time: December 12, 2006, 1:32 pm

Don’t forget to run:

. .bashrc

… to effect the change in your bash session. That’s dot space dot bashrc.

Comment from gid
Time: December 12, 2006, 2:14 pm

I myself am partial to:

PS1=’\u@\h \[\e[33m\]\W\[\e[0m\] \[`if [[ $? = 0 ]]; then echo ‘\”\e[32m=)\e[0m'\''; else echo '\''\e[31m=(\e[0m'\'' ; fi`\] $ ‘

works with linux/bash anyway =)

Pingback from OS X Daily » How to Customize your Terminal Prompt - Mac OS X Apps, Tips, Tricks, News, Updates, Everything Mac OS X : Popular Bookmarks : eConsultant
Time: December 23, 2006, 5:50 pm

[...] OS X Daily » How to Customize your Terminal Prompt - Mac OS X Apps, Tips, Tricks, News, Updates, Everything Mac OS X Posted in bookmarks | Trackback | del.icio.us [...]

Comment from anonymous
Time: December 30, 2006, 1:24 pm

a very nice article by IBM just reminded me of your site:

http://www-128.ibm.com/developerworks/linux/library/l-tip-prompt/

Comment from Alpharetta
Time: January 24, 2007, 5:31 pm

!!!! swweetttt

Comment from Strafverteidiger München
Time: April 24, 2007, 12:12 pm

very cool.

Comment from lipitor
Time: May 29, 2007, 9:38 am

Did the LOST prompt. Awesome!

Comment from mel
Time: July 24, 2007, 9:48 pm

I was frustrated until I realized that you have to quit terminal and restart before the customization takes effect. ah…the joys of customization…

Pingback from Top News Apple » How to Customize your Terminal Prompt (OS X)
Time: November 20, 2007, 6:06 am

[...] read more | digg story [...]

Comment from mrc
Time: December 4, 2007, 7:58 pm

for whatever reason, it appears that my .bashrc file is not being executed when i start up Terminal. i can set the “run command” for the basic window profile to execute the . .bashrc command to change the prompt, but it seems like it should work automatically. have i missed something?

Comment from Ivan Storck
Time: December 12, 2007, 6:22 am

I’m having the same problem as mrc - it seems like .bashrc is not being executed. I have even quit and restarted Terminal. Any ideas?

Comment from nick
Time: January 29, 2008, 5:29 pm

I had the same problem re: .bashrc not being executed…try making the same edits as above to .bash_profile instead. worked for me.

Comment from Dave
Time: March 12, 2008, 6:55 pm

I’m running Tiger and you can use the same code in your ~/.profile file. I assume this will change only the settings for that user, though.

Comment from Braxo
Time: June 24, 2008, 2:25 pm

To have this work after saving .bashrc you can put the exact code into .bash_profile.

But even better, keep the code in .bashrc and in .bash_profile put the line:

“source ~/.bashrc” without the quotes if you want the prompts to be the same in login and non-login sessions.

For my prompt, I chose this: [17:19:21] User@Host::/current_directory >:

With Terminal set to Pro style, and the time, @, :: and >: are yellow and User, Host are teal and directory green. Contents of my .bashrc is:

export PS1=”\e[33;40m[\t] \e[36;40m\u\e[33;40m@\e[36;40m\h\e[33;40m::\e[32;40m\w\e[33;40m >: \e[0m”

Comment from Hawkeye
Time: July 1, 2008, 1:28 am

No need to quit the Terminal.app and restart… merely open a new Terminal window (this creates a new session). Or you could simply invoke a new bash session by:

bash -l

Pingback from Customizing Terminal when compiling Mono apps | Dot Mac
Time: December 1, 2008, 11:11 pm

[...] Here’s how to customize your terminal prompt.   To add the location of mono.pc, edit .profile or .bashrc in the root of your home folder, and add this line [...]

Comment from Flüge Bangkok
Time: March 23, 2009, 6:51 am

I switched from vista to mac OS just 2 weeks ago, so I ‘m still a mac beginner.
Thank u yery much for ur tips.
It looks now much better than before. I always wanted to change the actual command line prompt but never succeeded to do it.
I have followed your instructions and also added \T for another time format, \s for shell name \v for shell version, it is really easy .

Comment from Flüge Bangkok
Time: March 23, 2009, 6:51 am

I switched from vista to mac OS just 2 weeks ago, so I ‘m still a mac beginner.
Thank u yery much for ur tips.
It looks now much better than before. I always wanted to change the actual command line prompt but never succeeded to do it.
I have followed your instructions and also added \T for another time format, \s for shell name \v for shell version, it is really easy .

Comment from Flug USA
Time: May 9, 2009, 2:55 am

Thank you
you have really saved my time.I was searching for a long time for a help to cutomize my command prompt.
At the begining I have forgotten the dot space dot bashrc thing in . .bashrc so the changes couldn’t be affected but shortly after I fixed it. :)
That was very helpful especially coz I’m lost fan and I got a nice Lost ’style for my prompt with the following command
export PS1=”\u@\h\w at \t :” .
I’m expecting more tips for mac users in you blog:)
keep up the good work

Write a comment







Social bookmarks:


December 11th, 2006