Customising The Terminal

How To Edit Your Bash Profile

David Corkett
8 min readFeb 18, 2017

If you are new to web development, you have probably heard of or started to use the command line. From CSS compilers such as Sass to version control using Git, today’s web developers have an array of tools specifically designed for front-end languages and workflow that utilise the command line.

The tools that employ the command line, help streamline a developer’s workflow, and thus a lot of time will be spent using it. The purpose of this article is to give an introduction to creating an environment within the terminal by creating and editing your .bash_profile. Creating this type of environment will allow you to customise your terminal. By the end of this article you should be able to create a greeting message every time you open the terminal and create aliases which will make your workflow more efficient.

Before you start, this article assumes:

  • You are using a Mac.
  • You are using the Terminal Applicationpreinstalled on all macs
  • You have some experience in using the terminal
  • You are using Nano as a Terminal Text Editor — if you don’t know what this is yet, don’t worry we will cover this later in the article
  • You are using the Bash Shell — we will also cover how to check/change to this in the article

Getting Started

To check you are using the Bash shell, or would like know for future reference, follow these instructions:

Settings > Users & Groups > Right click your User Name > Advanced Options > Login shell > Make sure the drop down says /bin/bash > Press Okay

If you had to change the login shell, you will need to quit and restart the terminal app for it to take effect

Make sure the drop down says /bin/bash

Before you can start, you need to be able to find it. So what we need to do is open the terminal:

Open your Applications folder > then open the Utilities folder > Open the Terminal application.

You may want to add this to your dock. I like to launch terminal by using Spotlight search in OS X, searching for “terminal”. You can do this by pressing cmd + spacebar .

In order to start editing your .bash_profile, we first need to create it. We will do this using a text editor. Fortunately, the terminal has one built in called Nano. It works a little differently to a non-terminal text editor, such as textedit, in that all the interactions are performed via the keyboard and not the mouse.

Type the following and press enter:

, type the following and press enter:

nano ~/.bash_profile# . before bash_profile means it is a hidden file# ~ (Tilde) tells terminal we want it located in the home directory

You should see the following:

Empty bash_profile in Nano

Create A Greeting

Let’s add a greeting. It can be anything you want. Type the following:

echo "Your Message"

My preference is for it to let me know that my .bash_profile has activated, like this:

echo "Hello, Your Bash Profile Is Running…"# The echo command prints its arguments in the terminal

We now need to save the .bash_profile in Nano. We do this with by typing ctrl + O(note this is the ctrl button and not the cmd button). Nano will ask you to confirm this, so let’s press enter to confirm and exit nano by pressing ctrl + xso we can test it.

If you’re looking at your text editor and wondering why you do not see your morale building welcome message, don’t worry.

In order to get the message to appear every time we open a terminal window, we need to activate the changes from the .bash_profile. We do this with the source command:

source ~/.bash_profile

You should now see your message above your command prompt:

Terminal displaying you welcome message above command prompt.

You now have your greeting appearing in the terminal, however it’s a bit time consuming to type these commands every time we want to edit the .bash_profile and activate changes.

nano ~/.bash_profilesource ~/.bash_profile

Use Aliases

As developers every second counts and it is the shortcuts that save us time. Remember the Aliases mentioned at the beginning of the article. We are going to use these to create the shortcuts we want. After this, we will only have to type profile instead of nano /.bash_profile to edit the .bash_profile, and src instead of source /.bash_profile. You can set up an alias now by typing the following straight into the terminal

alias profile="nano ~/.bash_profile"alias src="source ~/.bash_profile"

Let’s test it out. Type in your new alias command profile and see that it has opened the .bash_profile in the Nano editor.

You’re probably wondering why we didn’t save the aliases in the bash profile. I did this to illustrate why it is important to save your aliases and not just in the terminal. Quit the terminal app and reopen it, now try using the profile alias we just defined, you’ll most probably see a message similar to this:

-bash: profile: command not found

This is because the aliases we set up were not permanent and only existed for that last session that we closed. In order to have them saved permanently, we need to set it in the .bash_profile.

Let’s open Nano and edit our .bash_profile with nano ~/.bash_profile

Now let’s set up our aliases:

#alias for nanoalias profile="nano ~/.bash_profile"#alias for sourcealias src="source ~/.bash_profile"

Let’s save it with cmd + o, then press enter and cmd + x to exit nano.

We need to use the long command to activate the changes in the .bash_profile one last time as the new aliases have not as yet been activated. Type source ~/.bash_profile.

Let’s open our .bash_profile in Nano with our new alias profile

You should see your .bash_profile open in Nano.

You can create as many aliases as you want if it speeds up your workflow. I also use home as an alias for cd ~.

The Command Prompt

So far we have covered how to create a welcome message, and create aliases. Next we will see how to change the command prompt (the text you see to the left of all your commands). It should look something like this:

The command prompt can be customised too.

To do this we need to open up our .bash_profile in Nano as before, by using our predefined alias profile. You can change the command prompt to show the date, time, username, current working directory, the full path to the current directory you are on, and even change the $ sign to whatever you want.

I personally like my hostname to be displayed as well as my current directory (saves me typing pwd every time I want to find out which directory I am in). I am also keeping the $, as I prefer this. I will list the notations needed to create your own but for now type the following:

PS1="\h: \W: $ "

Save and exit nano (you know how to do it by now), and call the .bash_profile again with our src alias. Exit and re-open terminal and you should see the changes.

This is how I personally like to set up my terminal, but you can play around to find your preferred set up. You can even set up your command prompt to appear on multiply lines like this :

PS1="\h: \W:$ "
# List Of Other Commands For Command Prompt\d:       #date\h        #host\n:       #newline\t:       #time\u:       #username\W:       #current working directory - capital W used\w:       #full path to current directory - lower case w used#For More Check Out This 

No More Ugly Terminal

The terminal can look quite daunting to start off with, especially with the default black and white profile. You can change this in the terminals preferences with the pre-installed themes. Personally, I found most of these ugly and decided to create my own, changing the background opacity & colour.

My customised terminal theme.

To do this, open the terminals preferences (terminal > preferences) and click the profiles icon in the top of the window. Next press the little plus sign in the bottom left and name it whatever you like.

Creating a New Theme.

To the right you can play around with the background colour, opacity and blur. Below are the setting I use:

Colour choice of my Terminal text & cursor.

You can customise the terminal’s cursor too. Located at the bottom of the window under the cursor heading, you can choose between block, underline, and vertical. You can also choose whether you want the cursor to blink or not by checking the blink cursor box. To the right you can also change the colour and opacity of the cursor . I have also changed the font of my terminal, you can do this by clicking on the change button. For the colour of my text and cursor I use #52C1FF.

Once you have finished creating your theme you need to press default in the bottom left of the profile editor. To see your amazing new terminal set up in action, you will need to exit and re-open the terminal.

During this article we have looked at how to change to the bash shell, use the terminals inbuilt text editor to edit your .bash_profile, create a greeting message, create alias’ that save time, and customise your command prompt. We have also looked at and introduced how to customise the look of the terminal. There are many more ways you can customise your terminal to create a productive and comfortable work environment. Have a look at the extra reading for more tips on customising your terminal.

If you enjoyed this article, please recommend it on medium and let us know on twitter (@spartaglobal). If you’d like to know more about what we do, DM us on twitter or send us an email at academy@spartaglobal.com

--

--