How to Customize the Command Prompt

How to Customize the Command Prompt

Tutorial Details
  • Topic: Bash Prompt Customization
  • Difficulty: Intermediate
  • Estimated Completion Time: 20-30 mins

I’m a big fan of the terminal: whether you’re leveraging a handful of commands (or more!) to improve your development process, or just using it to quickly move around your drives and open files and folders, the command line is an awesome tool. However, if you use it often, you’ll want to customize it to your needs. I’ll show you how to do that today!

I’m often asked, “How did you get your command prompt to look like that?” Well, in this tutorial, I’ll show you exactly how to do it. It’s pretty simple, and won’t require too much of your time.

I should note that what I’m showing you is specifically for the bash shell; this is the default shell on Mac and most Linux systems. If you’d like a bash prompt on Windows, you might want to check out Cygwin.


How does it Work?

Before we get started, let’s talk for a minute about how you customize your bash prompt. It’s not quite the same as you’re average application: there’s no preferences panel. Your customizations are stored in a file. If you’re on Linux (or using Cygwin), that will be your .bashrc file; on Mac, that’s your .bash_profile file. In both cases, this file is kept in your home directory (if you aren’t sure where that is for a Cygwin install, run the command echo $HOME). Note that I’ll only refer to the .bashrc file from here on out, but use the .bash_profile if you’re on a Mac.

Note that on Macs (and possibly Linux machines; I’m not sure), files that begin with a period are hidden by default. To show them, run these two lines in the terminal

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

So, what goes in that .bashrc file? Each line is actually a command that you could run on the command line. In fact, that’s how these config files work: When you open the console, all the commands you’ve written in the config file are run, setting up your environment. So, if you just want to try out some of what I’ll show below, just type it on the command line itself. The simplicity here is beautiful.


Customizing PS1

Let’s start with a definition. The prompt is what you see at the beginning of the line, each time you hit enter on the command line. Here’s what the default settings are for the Mac:

Mac Default Terminal

In this case, the prompt is andrews-macbook:~ screencast$. There’s a few variables here: andrew-macbook is the name of this computer, ~ is the current directory (the home directory) and screencast is the username. Let’s customize this a bit.

Open up either your .bashrc file. The way we set what information is displayed in the prompt is with the PS1 variable. Add this to the file:

PS1='->'

Notice that I don’t put spaces on either side of the equals sign; that’s necessary. Save this file in your home directory and re-open a terminal window. Now, you should have a prompt that looks like this:

Mac Customized Terminal

I’ll note here that if you find it tedious to close and re-open your terminal each time you make a change to your .bashrc or .bash_profile, there’s a bit of a shortcut: You can load any bash customization file with the source command. Run this in your terminal:

source ~/.bashrc 

Still too long? Well, a single period (.) is an alias for source. Happy now? If you’re quick, you’ll realize that we can use the source command to include other files within our .bashrc file, if you want to split it up to keep in under control.

Let’s customize our prompt a bit more. We can use built-in variables in the string we assign to PS1 to include helpful information in the prompt; here’s a few useful one:

  • \d: Date
  • \h: Host
  • \n: Newline
  • \t: Time
  • \u: Username
  • \W: Current working directory
  • \w: Full path to current directory

So, if you set your prompt to this:

PS1='\n\W\n[\h][\u]->'

you should see something like this:

Mac Customized Terminal

Notice a few things here: firstly, we’re using a bunch of the variables shown above to give us more information. But secondly, we’re including a few newlines in there, and getting a more interesting prompt: we have the current directory on one line, and then the actual prompt on the next line. I prefer my prompt this way, because I always have the same amount of space to write my commands, no matter how long the path to the current directory is. However, there’s a better way to do this, so let’s look at that now.


Customizing PROMPT_COMMAND

The better way to do this is the use the PROMPT_COMMAND variable; the contents of this variable isn’t just a string, like with PS1. It’s actually a command that executed before bash displays the prompt. To give this a try, let’s add this to our .bashrc:

PROMPT_COMMAND='echo "comes before the prompt"'

We’re using the echo command here; if you aren’t familiar with it, you just pass it a string, and it will write it to the terminal. By itself, it’s not incredibly useful (although you can use it to view variables: echo $PS1), but it’s great when used with other commands, so display their output. If you added the line above, you should see this:

Mac Customized Terminal

Let’s do something more useful here. Let’s write a bash function that we will assign to PROMPT_COMMAND. Try this:

print_before_the_prompt () {
    echo "comes before the prompt"
}

PROMPT_COMMAND=print_before_the_prompt

If you use this, you shouldn’t see a difference in your prompt from what we have above. Now, let’s make this useful.

print_before_the_prompt () {
  echo "$USER: $PWD"
}

PROMPT_COMMAND=print_before_the_prompt

PS1='->'

Here’s what you’ll get:

Mac Customized Terminal

That’s a good start, but I want to do a bit more. I’m going to use the printf command instead of echo because it makes including newlines and variables a bit easier. A quick background on the printf command: it takes several paramters, the first being a kind of template for the string that will be outputted. The other parameters are values that will be substituted into the template string where appropriate; we’ll see how this works.

So let’s do this:

print_before_the_prompt () {
    printf "\n%s: %s\n" "$USER" "$PWD"
}

See those %s parts in there? That means “interpret the value for this spot as a string”; for context, we could also use %d to format the value as a decimal number. As you can see, we have two %ss in the “template” string, and two other parameters. These will be placed into the string where the %ss are. Also, notice the newlines at the beginning and end: the first just gives the terminal some breathing room. The last one makes sure that the prompt (PS1) will be printed on the next line, and not on the same line as PROMPT_COMMAND.

You should get a terminal like this:

Mac Customized Terminal

Adding Some Color

Looking good! But let’s take it one step farther. Let’s add some color to this. We can use some special codes to change the color of the text in the terminal. It can be rather daunting to use the actual code, so I like to copy this list of variables for the color and add it at the top of my .bashrc file:

txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White

bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White

unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White

bakblk='\e[40m'   # Black - Background
bakred='\e[41m'   # Red
badgrn='\e[42m'   # Green
bakylw='\e[43m'   # Yellow
bakblu='\e[44m'   # Blue
bakpur='\e[45m'   # Purple
bakcyn='\e[46m'   # Cyan
bakwht='\e[47m'   # White

txtrst='\e[0m'    # Text Reset

There’s some method to this madness: The first set are turn on normal coloring. The second set turn on bold coloring. The third set turn on underlined coloring. And that fourth set turn on background coloring. That last one resets the coloring to normal. So, let’s use these!

print_before_the_prompt () {
    printf "\n $txtred%s: $bldgrn%s \n$txtrst" "$USER" "$PWD"
}

Here, I’ve added $txtred before the first %s, and $bldgrn before the second %s; then, at the end, I’ve reset the text color. You have to do this because once you set a color, it will hold until you either use a new color or reset the coloring. You’ll also notice that when setting a variable, we don’t prefix it with a dollar sign; but we do use the dollar sign when using the variable: that’s the way bash variables work. This gives us the following:

Mac Customized Terminal

Let’s move on to the final step: adding some scripting to give us even more information.


Adding Version Control Information

If you’ve seen the screencasts that come with my book Getting Good with Git, you might remember that I have some version control information showing in my prompt. I got this idea from the excellent PeepCode “Advanced Command Line” screencast, which share this, as well as many other great tips.

To do this, we’re going to need to download and build the script that finds this information. Head on over to the repository for vcprompt, a script that outputs the version control information. If you’re familiar with the Mercurial version control system, you can use that to get the repo, but you’ll most likely want to hit that ‘zip’ link to download the script code as a zip file. Once you unzip it, you’ll have to build the script. To do this, just cd into the unzipped script folder and run the command make. Once this command runs, you should see a file named ‘vcprompt’ in the folder. This is the executable script.

So, how do we use this in our prompt? Well, this brings up an important rabbit-trail: how do we “install” a script (like this one) so that we can use it in the terminal? All the commands that you can run on the terminal are found in a defined array of folders; this array is the PATH variable. You can see a list of the folders currently in your PATH by running echo $PATH. It might look something like this:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

What we need to do is put the executable script vcprompt in a folder that’s in our path. What I like to do (and yes, I learned this trick from that PeepCode screencast, too) is create a folder called ‘bin’ (short for ‘binary’) in my home directory and add that folder to my PATH. Add this to your .bashrc:

export PATH=~/bin:$PATH

This sets PATH to ~/bin, plus whatever was already in the PATH variable. If we now put that vcprompt script into ~/bin, we will be able to execute it in any folder on the terminal.

So, now let’s add this to our prompt:

print_before_the_prompt () {
    printf "\n $txtred%s: $bldgrn%s $txtpur%s\n$txtrst" "$USER" "$PWD" "$(vcprompt)"
}

I’ve added $txtpur %s to the “template” string, and added the fourth parameter"$(vcprompt)". Using the dollar sign and parenthesis will execute the script and return the output. Now, you’ll get this:

Mac Customized Terminal

Notice that if the folder doesn’t use some kind of version control, nothing shows. But, if we’re in a repository, we get the version control system that’s being used (Git, in my case) and the branch name. You can customize this output a bit, if you’d like: check the Readme file that you downloaded with the source code for the vcprompt script.


Moving On!

Here’s our complete .bashrc or .bash_profile file:

export PATH=~/bin:$PATH

txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m'   # Black - Background
bakred='\e[41m'   # Red
badgrn='\e[42m'   # Green
bakylw='\e[43m'   # Yellow
bakblu='\e[44m'   # Blue
bakpur='\e[45m'   # Purple
bakcyn='\e[46m'   # Cyan
bakwht='\e[47m'   # White
txtrst='\e[0m'    # Text Reset

print_before_the_prompt () {
    printf "\n $txtred%s: $bldgrn%s $txtpur%s\n$txtrst" "$USER" "$PWD" "$(vcprompt)"
}

PROMPT_COMMAND=print_before_the_prompt
PS1='->'

Well, that’s a crash-course on customizing your bash prompt. If you have any questions, be sure to drop them in the comments!

Tags: terminal
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://deanperry.net Dean

    That’s great. I’ve always wondered how to change the colour of the text in the Terminal :)

    • http://w3tut.org/ Damu

      same here. loved the section for color text in terminal. gr8 tut overall.

  • http://www.markus-gattol.name Markus Gattol

    Nice one! I’ve done pretty much the same plus I’ve also made it so that I get different colors based on whether I am root or not, or, whether I am using SSH or not:
    http://www.markus-gattol.name/ws/bash.html#colorized_shell_prompt

  • DED

    Nice!! I customized my terminal awhile ago and it is so much more than eye-candy. Even the pretty colors serve a useful purpose in quickly distinguishing the meaning of the output.
    Great write up.

  • Sk1ppeR

    What this has to do with web tutorials >.< I know you are a Mac fanboy but there are people that don't like Mac at all. Since it's an overpriced PC. Go fap yourself

    • http://dorianpatterson.wordpress.com Dorian Patterson

      If you actually read the article, you would have noticed the mention that this is for the bash shell, which is available on multiple platforms (Mac, Linux, Windows, and probably a ton of others). So for those who “don’t like Mac at all” this is still useful.

    • Lukas

      “If you’d like a bash prompt on Windows, you might want to check out Cygwin.”
      And there’s some more info given fore Cygwin users troughout the tutorial, so you can’t blame the author for anything. Plus, there are a lot of webdesigners out there who use the command prompt all the time, so it certainly has something to do with web tutorials.

      If you don’t like it, then don’t read it.

      Oh, and, a Mac is definitely NOT an overpriced pc ;-)

      • Gino

        Mac definitely IS an overpriced pc ;-)

      • Thomas

        MAC is definitely overpriced, but NOT a PC ;)

      • Gino

        Thomas: no and what is it if it’s not a PC? An EC, Enterprise Comuter? Lol, brainwashed…

    • nuku

      Hey dude, I don’t like Mac at all and still I use a shell like bash! (well not bash, but zsh, which is like bash, but better, if you know how to configure it). All my machines run Debian (which is a Linux distribution). You can run it on windows too, using cygwin. I sure am waaaay faster when using a shell than when clicking a mouse — I can keep my fingers on the keyboard the whole time ;)

    • http://www.symetronapps.com luka

      Yeah macs are overpriced but it’s worth it, sort of.

    • Sk1ppeR

      Yeah you are brainwashed i know it’s cool though. And why in the hell i would use linux environment in my windows :O (cygwin) instead if i want the terminal so badly i could’ve installed PowerShell which is way cooler ! And it supports C# not bash scripts ! That’s so much cooler.

      Also i bet that i can reconfigure my network adapter in windows much faster than you do in linux through terminal and vi / nano \m/o_O\m/

      And about the guy stating that Mac is not overpriced PC … I bought 3 days ago a 1000$ PC which have more horsepower than the 3000$ 17″ Macbook Pro LOL. Also i bought a 200$ 24″ LCD Monitor. Man that PC haz gaming and yes it can, in fact, run Crysis on full hd resolution without a prob =)

      • http://www.gredo.co.uk Mikey

        If you are comparing PC vs Mac purely on the “horsepower” of the machine, then you really don’t get what macs are all about. <– don't want to start a PCvsMac debate, just get's boring reading the whole my PC is faster than a mac and it was cheaper!

      • Gino

        Mikey: we can’t get it what? That it’s overpriced and made for noobs?

      • Sk1ppeR

        It all comes down to the simple question. Can it fun Crysis ? :D If it can’t – fuck it !

      • John

        so if I buy any windows pc can it run python, ruby, bash, php, apache, ssh, compile C,C++,Obj-C just out of the box? Can I open a pdf file without any other application? Can it mount Image files? Can i open terminal and have a unix filesystem to work seamlessly with other unix server? can I unzip out of the box? can I use this fantastic OS for a year without having to format and reinstall all the overpriced third-part applications and Windows7 “Enterprise” features? OH MAN WINDOWS IS SO GOOD AT ALL.

      • joshua m

        I love that the comments are that Mac users are “brainwashed”

        Frankly – most mac users, every one of them I know personally, including myself, started on windows.

        Mac OSX does a lot more out of the box than windows does. If you want to complain about the hardware, yeah, its expensive – but its like an acura compared to a honda, it’s basically the same, one is just a luxury brand – and I’ve yet to see a “all in one” pc that performed as well as an iMac or didnt have noisy ass fans – this is because while the “components” might be the same, the motherboard, etc, are engineered for the product and completely customized for cooling and space management. Same goes for the laptops, there isn’t a laptop out that that feels as good as a MBP – and certainly not look as nice.

        But lets put hardware aside – we can (and I do, on one machine, run OSX on custom hardware).

        OSX, out of the box, does pretty much everything anyone needs it to do. On top of that, I have a full CLI built on top of (not emulated for) a *nix/BSD system that works just like pretty much every server I host my sites on. I can ssh “out of the gate”. I can connect to svn+ssh repositories without a fight (have fun trying to get windows svn clients to work tunneled over ssh, its possible, its just painful).

        The fact of the matter is just about every tool I use on a daily basis was already on this machine when I turned it on the first time. The ones I don’t I have on here in a matter of minutes. All of my important settings – all rolled into bash scripts or in my .bash_profile and I keep those in a subversion repo so whenever i get on a machine (mac OR linux) i simply checkout those files, source the profile and I pretty much have ALL of my tools, configured to the way I use them, in minutes… that just doesn’t happen on a windows machine.

        Also, when Lion drops… guess what? I pay $29 and LEGALLY install it on all the machines I have… how’s that windows upgrade price goin for you? (well, with your attitude, you probably pirate it… but you know)

      • Sk1ppeR

        My Windows is not pirated in fact I’ve upgraded from Vista to 7 for ~50$ (don’t remember how much exactly) the cool thing was that the vista was free to me from a student program in the university \m/o_O\m/

        Anyway … why would someone need “python, ruby, bash, php, apache, ssh, compile C,C++,Obj-C just out of the box” ? Do they help you to run Crysis ? O.o NO ! And getting web support is as hard as to go and download WAMP/XAMP (is it still supported ? :O ) although i don’t test stuff locally it’s just a bad practice. I work remotely on my servers anyway (which are infact *nix based BUT that’s just because the OS is free :) )

        Yes you can open a pdf if you pick Google Chrome when Windows asks you which browser you want by default (Thank you EU)

        Um what “unix filesystem to work seamlessly with other unix server?” ? FileZilla and you have “unix filesystem” working seamlessly … wtf ?!?
        Yes actually you can … in fact … unzip out of the box ^.^
        I haven’t reinstalled in years I only upgrade the OS. I don’t have antivirus or firewall and the PC is doing pretty fine. The bad thing is only that i have to run disk defragment once or twice every 3-4 months :/ damn ntfs

        For a shell (if you really can’t manage to work with the amazing graphic interface) you could use PowerShell which comes with Windows 7 and Windows Server 2008 R2 – which actually supports .NET platform. This brings the question…can Mac run C# apps ? Yeah i know you would say “Sure yeah i can use Mono” but i’ll counter you with … How well it can run a .NET application ? :D

        .NET is one of the best (if not THE best) platform for application development at the moment. Neither objective-c can get near it. Java is the only competitor there ! Although i can’t say bad things to java … there are certain flows within the language but if you practice it from a long time you stop noticing them anyway ^^

        QQ

      • Paul R

        Sk1ppeR go lick a window or something.

        You don’t understand the difference between a Mac or a Windows PC, stop trying to justify yourself.

        Windows has its advantages, Mac has its advantages, Linux has its advantages.

        Let people make up their own decisions.

        ——-

        Back on track, excellent tutorial! I didnt know it was this easy to do stuff like this.

      • Eric

        If your entire Mac vs. PC argument rests on the ability to run Crysis out of the box, then I’m pretty sure you’re on the wrong site. I believe Gamespot or IGN would be the appropriate place for you to go trolling. The fact is, my 2008 Mac Pro that is over 3 years old runs Windows 7 better than my brand new Dell at work.

        To some people, aesthetics matter. Macs aren’t right for everyone. And yes, the userbase does have a hell of a lot of hipster kids, but the PC userbase seems to be filled with people who use the word “noob” and replace the letter “s” with “z” on almost every word that ends with an “s.” Gaming isn’t everything to everyone…even though I myself am a gamer and have Windows 7 solely for the purpose of playing games.

        In 2006, I would have told any Mac user to go suck a railroad spike…and then I used one. Now, I’ll never buy anything else.

        TL;DR (in a language you understand): OMGWTFBBQ Mac winz!!! PC is teh suckzorz!!!!!!!! WTF I can totz run ur faggy Crysis at a billion fpszzzzz and shiz. GTFO fagnoob.

      • Sk1ppeR

        Paul why don’t you go and kiss a snow leopard ? 10 if possible :D Seriously that’s the dumbest comment ever

        The fact is, my 2008 Mac Pro that is over 3 years old runs Windows 7 better than my brand new Dell at work. – LOL ! :D That made me laugh !!! Thank you ! Nice joke

        ” I believe Gamespot or IGN would be the appropriate place for you to go trolling.” – Hey I’m as much as a web developer as you are so i believe this is an appropriate place for me even trollin’ , although I am right … it’s just that you are brainwashed makes you post silly posts :/

        Why everyone think that every PC user is either a gamer or someone that can’t make a difference out of laptop and a mac ? :D
        I had Mac book pro .. thank god it broke ! I didn’t even bothered to fix it
        I get it that Apple is luxury mark … with tablets and phones and all … i even had iPhone 3GS but there is only one computer when it comes down to the desktop and/or workstation and it’s the ordinary pc not the Mac pc … Tbh I’m not a big fan of laptops ! Yes they are mobile – you can walk around and do stuff but so i can with my Nexus S … sure i can’t work with it but when i need to work i use my workstation (which happens to be powerfull enough to run Crysis :) )

      • joshua m

        Ever notice how when people dont like windows, they bash windows. But when windows users bash Mac, they almost always bash the mac users?

        Middle School social studies debate lessons should have taught you about “Ad Hominem” attacks:

        “Abusive ad hominem (also called personal abuse or personal attacks) usually involves insulting or belittling one’s opponent in order to attack his claim or invalidate his argument, but can also involve pointing out factual but apparent character flaws or actions that are irrelevant to the opponent’s argument. This tactic is logically fallacious because insults and negative facts about the opponent’s personal character have nothing to do with the logical merits of the opponent’s arguments or assertions.”

      • John

        How could it be a bad practice to have a exactly copy of the web server in my local computer? So if you do f*** things up you lay down your head over the keyboard and cry? Dude you are insane doing updates in the deploy server. It may be a good practice over the ubber C# programmers, but in real world development you are so wrong.

        C# may be good for the microsoft systems but it still a really crappy language because it was based in another crappy language: Java.

        If i make the same question to you: Windows OS can run Obj-C applications? What is your answer? At least we have mono to do the dirty work.

        All your answers don’t work for the point I tried to reach. For all the things you have to download some kind of software to do the basic work, but the OSX don’t.

        You said you want horse power only to play, that’s nice, but i’d rather stay with my macbook and when I come home and if i wanna play something i turn on my ps3 or xbox 360 and enjoy my good couch with a bear and leave the work in it’s own place.

        After all, it’s a matter of which system will make you suffer less. OSX gives me all the power of the UNIX foundation giving me nice things like python, ruby, bash etc.. all out of the box, and the better thing to me: i don’t have to use wine or anything like that to run applications like photoshop and flash. You see the price I am paying for a overpriced hardware? For me it vanishes for all these “little” features that make me work easily doing less steps to achieve that.

        I think that windows users have more money than mac users, because they have time to select the premium hardware they want, assembled it, install the os, search/install all the drivers, search install all the applications to finally start to work. That is a time I don’t have, and time is money! ;)

  • http://twitter.com/brunogama Bruno

    there is a pretty cool project called “Bash-It”, it have a template system for the prompt that it is awesome to look at: https://github.com/revans/bash-it

  • http://twitter.com/brunogama Bruno

    there is a pretty cool project called “Bash-It”, it have a template system for the prompt that it is awesome to look at: https://github.com/revans/bash-it

  • Derick

    How do I get that ‘last login’ info to display when I start up a terminal? I get that line only when when I login into an other computer using SSH. I always thought that some useful info to display anytime I open a terminal. Good job on making the terminal easier on the eyes!

  • http://www.craig-russell.co.uk Craig

    Great article. Adding git branch info to the shell prompt is a great idea, though you can achieve the same thing without the need to use the vcprompt script.
    Just use this in the .bashrc file

    function pre_prompt_print(){
    GIT=”
    BRANCH=`git branch 2> /dev/null | grep \* | awk ‘{print $2}’`
    if [[ "$BRANCH" != "" ]]; then
    GIT=”[git:$BRANCH]”
    fi
    printf “$USER@$HOSTNAME$PWD $GIT\n”
    }

    PROMPT_COMMAND=pre_prompt_print
    PS1=”-> ”

  • nuku

    I seriously recommend zsh. It is the most awesome shell ever, however it takes some time to make it great: You have to know its powers, which — given how much the shell can do – few people probably do. I sure don’t know more than 10% of it, but yet I profit so much.

    zsh has the best completion system ever: if I type cd d/s/p and press tab, that expands to “cd docs/seminar/presentation”. I can tab through selections and even get a menu of them that I can navigate with the arrow keys. It has completion for most tools (like aptitude, ifconfig or iptables) and the completion is way faster than bashes. It also has correction for the completion, you can have lowercase characters match uppercase (which is really handy), etc. You can even get completion for kill – it will list running processes and give the PID of the one you selected. This is the greatest completion system ever, seriously!

    Also, my prompt looks like the following:
    22:25 Mon 20% [~][9.4W 76% 8.2h]
    The left prompt probably needs no further explanation; on the right I first have the current directory, then power usage, then remaining % battery and remaining time on battery. If I plug it in, it changes to charging rate / current percent / remaining charging time. Now that’s pretty awesome, too, right?

    Now I could go on about cool features, but I think I made my point. If I got you curious, keep in mind that out-of-the-box, zsh is pretty lame, you really need a good config. Just google it and steal someone else’s. Also grmls zsh-config is pretty cool. Good luck!

  • Jayphen

    Having a bit of trouble setting the vcprompt format to [%n:%b] … Any ideas? Setting the VCPROMPT_FORMAT variable doesn’t seem to help.

    • Jayphen

      Nevermind – just doesn’t fully support SVN

  • http://joeybutler.net Joey Butler

    Useful post. I recently just customized my shell prompt with git as well. Also added in rvm info to display the current ruby implementation (ree, jruby, etc) and the current gemset. Those rvm items are great for keeping your sanity when dealing with multiple apps / dependencies. Feel free to copy my config from this gist: https://gist.github.com/1039353

  • http://www.helloper.com Per

    I’m using Solarized as well for the command prompt as for Sublime Text 2.

    http://ethanschoonover.com/solarized

  • http://valleec.com ValleeC

    Hey guys do you know why have I to re-source my .bashrc each time I quit Terminal.app and restart it ?
    Would be helping to get rid of this annoyance.

    • http://andrewburgess.ca Andrew Burgess
      Author

      Are you on a Mac? If you are, try renaming your .bashrc file to .bash_profile. Let me know if that works.

  • http://sethetter.com Seth Etter

    Thanks a ton for these tips, my terminal is looking awesome now! :)

  • http://www.twitter.com/driesvints Dries Vints

    I once saw a neat new interface concept for Terminal. It had a beautiful redesign with all sorts of cool interface elements which really turned entire terminal upside down.

    Thing is… I can’t find it anymore. Does anyone knows what I mean and can perhaps share the link to the concept?

  • Daquan Wright

    I love my good old tech folks, lol!

    The mac vs. PC rares its head even here! haha =P

    Take it easy guys, don’t have a heart attack. ;)

  • http://rushthinking.com Amr Tamimi

    I use Oh-My-Zsh https://github.com/robbyrussell/oh-my-zsh this is the best ever!

  • Caleb

    Awesome tutorial. I truly have always wondered how to make a pretty terminal window, and now I have one!

  • Daniel Balfour

    Hey Drew…

    Another winner! Terrific article, really – and such an easy read! Everything is very well explained. Keep up the terrific work. I found this very informative.

  • Joshua

    Great tutorial! Any hope of deleting all the windows vs. mac comments to clean up the comments section? I couldn’t find any useful information in them.

  • Mike

    The new(?) Lion “proxy icon” shown at http://www.macosxautomation.com/lion/terminal.html was not showing up for me in Terminal windows. I removed “PROMPT_COMMAND=print_before_the_prompt” and it started showing up.

  • matt

    Thanks! Is there anyway to change the color of output. For example, if I run a ruby or python file, I’d like to see the resulting “print” outputs in a different color. Or similarly, the text under your ‘git init’ command.

  • ciastek

    I use iTerm2 on OSX Snow Leopard and had to wrap all color escape codes in \[ and \], for example: txtblk=’\[\e[0;30m\]‘
    Without that bash didn’t break long command in many lines.
    http://code.google.com/p/iterm2/issues/detail?id=1302

  • rameshrasaiyan

    Very Interesting… I learned a lot from this Article.