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:
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.
The file should be blank. So lets get modifying.
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:
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
\s Shell name
\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:
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.
Finally close your colour.
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.
Dark Gray 1;30
Light Blue 1;34
Light Green 1;32
Light Cyan 1;36
Light Red 1;31
Light Purple 1;35
Light Gray 0;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.
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.
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.
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.
and you will get a result similar to the following.
507 cd ./temporary
You can then use this history to see previous commands. To access a previous command simply press the up arrow or type the following.
The number refers to a history command which you have just seen.
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.