Customizing The Mac Terminal Bash Prompt 6

Terminal

Share
Customizing The Mac Terminal Bash Prompt

Hey

I like using Terminal. It can do hundreds of different things on my Mac which I wouldn’t normally do. Since I use it so much why don’t I customize it to my liking. Using a bit of Unix and Linux bash scripting it can easily be done. It doesn’t take a lot of changing but getting it exactly as you want is quite hard. You can’t really do a lot of harm with this tutorial, it just takes a while to tweak it to exactly as you want.

To customize the bash prompt open up Terminal. In your home directory type:

ls -a

This will list all of the available files in your home directory included the ones that are hidden. You should see a file that is named “.bash_profile”. This is the file that loads when a Terminal window or tab is opened. If it isn’t there, don’t worry you can great a new file and the settings should apply. To start modifying the Terminal open up the file using nano. You can use this command.

nano .bash_profile

The file should be blank. So lets get modifying.

Simple Commands

One of the simplest commands to apply in the bash profile is alias. Say, for example, you always navigate to a really deep folder. This can take time, if you make mistakes it takes even longer. You can simply add an alias so you type in a shortcut to take you directly there. You can add, for instance, the following in the nano window.

alias shortcut = "cd /direct/path/to/deep/folder"

Save the file by pressing Ctrl + O. Change shortcut to a name you can remember and the path to your folder. Now open a new bash window and test out the command. You need to open a new window every time so the changes can take effect. I usually keep one window open with the .bash_profile open and then use another for testing. When you have finished press Ctrl + X to quit nano. More information can be found at the bottom of your Terminal screen.

If all goes well Terminal should take you to the correct folder. You can also use this command to run other commands. Take the list files example below. I have mapped it do the normal list files commands, but extra options are enabled.

alias ls="ls -ahl"

Customizing How Bash Looks

Actually changing how the command line looks can also be done within the .bash_profile file. This takes a bit thinking as you have to get the syntax correct otherwise things tend to look a bit funny. For example the following in the profile file:

export PS1="\u@\d:"

Will give a result similar to the one below.

James@Sun Oct 19:

You can change any of the \u and \d values to different variables. You can also add in other characters into the mix. As a result you can change it to something useful. For the full table refer to the mini-table below.

\! History number of command
\# Command number of current command
\d Date
\h Host
\n Newline
\s Shell name
\t Time
\u Username
\W Current working directory
\w Full path to current directory
\\ A backslash

With these commands you can change exactly what comes up and in what order. As a side note make sure you include some sort of finishing character, usually the dollar sign ($), this is so you can easily see the beginning of the prompt.

Tip: You can add spaces in between characters to space them out. As well as this if you add a space before the closing double quotes you can separate the command from the prompt.

Tip: If you want just a simple prompt of the dollar sign, simply write the dollar sign.

Adding A Splash Of Colour

Things look a lot better with colour. A little bit of colour can break up the harshness of the Terminal prompt. Actually using the colour is quite hard. It has a very specific piece of syntax. Building up and changing colours of various things is actually quite simple. To comprises of two pieces of code, the little bit which you want to change according to the list previous, and then an ending bit of code.

To start changing the colour of the prompt right the following in your quotes:

\[\e[0;31m\]

I have no idea what the \e means, but it is important. The 0;31m refers to a colour value which will be explained later. Then add what you want to change. So you end up with something similar to the text below.

\[\e[0;31m\]\h

Finally close your colour.

\[\e[0;31m\]\h\[\e[m\]

That will change the prompt so the host name of the prompt is red. You then have to go through every part that you want to change. If you want to leave something the default colour, don’t surround it in colour code.

Tip: You can change the colour of the text you write with by adding the beginning but of colour code but not finishing it.

The colour numbers which you change can be found in the table below. Simply change the 0;31 to any of the following.

Black 0;30
Dark Gray 1;30
Blue 0;34
Light Blue 1;34
Green 0;32
Light Green 1;32
Cyan 0;36
Light Cyan 1;36
Red 0;31
Light Red 1;31
Purple 0;35
Light Purple 1;35
Brown 0;33
Yellow 1;33
Light Gray 0;37
White 1;37

Bash To DOS

You can make the bash prompt look like a DOS prompt. Although you will be sent to the depths of hell for even thinking about making your Mac look like a windows machine, you can easily do it. If you type the following it should look like a Windows prompt.

export PS1="C:\w>"

Tip: You can run commands from your prompt by surrounding your command in back ticks (`). It works kinda ok, but not the best results. For example you could do the previous command with the following: export PS1=”C:`pwd`>”. That would print the current directory when you start. It doesn’t sadly update.

History

One final area of customizing the bash prompt I would like to show you is the history. This comes in part from the TextMate blog. What you can do is increase or decrease the history size, remove duplicates and make sure it saves your history on the exit of the shell. This can be done through these three commands in your bash_profile file.

export HISTCONTROL=erasedups
export HISTSIZE=10000
shopt -s histappend

The first line makes sure you remove duplicates in the list. The second controls the size of your history. The last line makes sure your history is appended when you close the prompt. I am not sure if the last line works. But the other two definitely do. You can use this command to change how long your history is. If you like a long history you can change this value to suit you.

To access your history simply type the following.

history

and you will get a result similar to the following.

507  cd ./temporary
508  clear
509  exit
510  history

You can then use this history to see previous commands. To access a previous command simply press the up arrow or type the following.

!1234

The number refers to a history command which you have just seen.

Conclusion

You could spend a whole afternoon fiddling around with prompt getting it to suit how you want. Most of your time will be spent getting the colours and command correct. I have managed to get my prompt exactly how I want it. If you ever want to get rid of your changes, simply open the file and delete everything inside it. It will then revert to your default.

In a future post I will go through variables and commands. You can make your bash really work for yourself. All you need to do is know how to do it. If you have any tips or interesting results with the bash prompt using this tutorial please leave a comment below.

If you want to take your skills with Terminal a bit further I recommend you check out the Terminal Category on this site. If you fancy reading a book there is a couple on Amazon that I regularly see mentioned and recommend, O’reilly Unix Geeks and Unix Under the Hood both are designed for Mac OS X and take Terminal further.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips

6 Responses to “Customizing The Mac Terminal Bash Prompt”

  1. 1

    Tip: I had to change

    alias shortcut = “cd /direct/path/to/deep/folder”

    To

    alias shortcut=”cd /direct/path/to/deep/folder”

    to get this to work

    ~kor

    Comment By Kor on August 8th, at 1:20 pm

  2. 2

    Hi,

    I am a banker & I just bought a Mac computer with my bonus I last week. Its the first time that I am using a Mac operating computer & I didn’t quite understand how to use it properly though I am able to get familiar with the basic operations.
    I was able to find this post on Mac terminal skins – http://www.levoltz.com/2009/08/08/macchanging-skins-of-the-terminal/ but is quite hard & complicated to understand for me. Its the first post that came across for me since I mostly use that blog for information.
    Its a good blog, but then, I found yours & its really great there are so many & topics to discuss. Although its hard for me to understand some things in this, I guess that blog posts will be a fast track to get familiar Mac operating systems.

    I thank you you for this amazing post. Keep it up. I will keep in touch.

    Thanks again.

    Comment By Ajmaal Firdowzi on August 10th, at 9:57 pm

  3. 3

    \e means escape. For more information look for the good old DEC video terminal standard VT100 (http://en.wikipedia.org/wiki/VT100). Starting at this point you’ll find more about escape sequences.

    Comment By kay on September 8th, at 10:47 pm

  4. 4

    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 colorful prompt: http://github.com/rafmagana/geek_painter

    Comment By Rafael Magana on July 23rd, at 6:59 pm

  5. 5

    Seems to be whitespace sensitive.

    Comment By Byron on January 24th, at 8:43 pm

  6. 6

    Hi.
    In the color part I need to change the \e by an 33.
    They both work for color, but the first cause and weird behavior when using long commands and history with long lines.

    Thanks

    Comment By Gustavo on July 29th, at 10:06 pm