Configure Your Command Line

Let's see what you've learned about the command line and get you set up with the right configuration.

Scroll down...




The previous lesson gave you some practice moving and looking around the command line. Hopefully it's not so scary anymore. In this assignment, we'll make it extra friendly for you by letting you customize how yours looks. Some of these things will be very useful and others will just allow you to enjoy your command line a bit more.

The instructions here will be Mac-biased.

Configuring Your Color Scheme

If you're using the Terminal for Mac, go under the Terminal menu and click "Preferences" to edit your display preferences. You can select from any of the color schemes in the left panel or edit them yourself.

Terminal Preferences Window

If you want to set a color scheme as the default, select it and click the "default" button at the bottom left of the screen.

When you open a new window, it should now default to that color scheme. You can also manually open a window in a different color scheme by clicking the Shell menu, then New Window or New Tab and selecting from under the available options.

Configuring your Command Line

Remember the .bash_profile file we talked about in the previous lesson? The one which contains your configuration information for the shell? In this exercise, you'll get a chance to customize how your prompt looks and some special commands you can run.

The first step is to create the file if you don't have it, so check your user root directory ~ for it. Otherwise, create it using:

$ touch ~/.bash_profile

Then open it up in your text editor. The specific command you use will be dependent on your text editor, and you may need to install that editor's "command line tools" (you will need to Google for the instructions that match your particular editor):

# Sublime Text
$ subl ~/.bash_profile

# Atom
$ atom ~/.bash_profile

The next several sections will have you add to the .bash_profile file to help you customize your command line output and give you some familiarity with working with Bash. This isn't something you'll be doing often while developing, but getting things set up properly will feel good.

The Prompt

Basically, there's a variable that you pass a bunch of strange-looking parameters to and that determines how the prompt looks. You can set it up to display which folder you're currently in, today's date, which Git branch you're on (we'll cover that later)... it's pretty much up to you. It also determines which colors to use to display different things.

If you'd just like a useful prepackaged batch of settings (the ones I use), try adding the following lines to your .bash_profile file:

# In the ~/.bash_profile file
export PS1='\['$PROMPT_COLOR'\][\h]: \w\$\[\e[0m\] '
export EDITOR='subl -w'

Read through this article on configuring your command prompt by Nix Craft to see the syntax for changing specific settings.

Read through This Floss Manual on customization for more details.

Coloring Different File Types

You probably also want to make folders look different than files so it's easier to see them at a glance when you use the ls command. There's another variable called $LSCOLORS which contains this information, and it has its own syntax as well. Basically, it's just a long string of color codes that have a particular order corresponding to each file type.

You'll need to set the CLICOLOR flag to 1 as well. If you're lazy and just want to use my settings, add these lines to your .bash_profile:

# in ~/.bash_profile
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

Or you can customize them by playing around with this interactive color generator from Geoff Greer and using the string provided.

Handy Alias Commands

An "alias" lets you specify a simple command that you want to type in order to run some other command or batch of commands. It's really useful for saving time.

Maybe you don't want to type out the full code to use TextEdit to open a file:

$ open -a TextEdit somefile

You can just create an alias for it:

# ~/.bash_profile
alias textedit='open -a TextEdit'

For example, I got tired of typing $ git push origin master and instead set up $ gpom. You'll probably run into similar situations, and aliases will be there for you when you need them.

.bashrc (optional)

There's another hidden file called .bashrc which you might have. It does the same thing as .bash_profile but for "non-login" shells. You'll occasionally run into this and it can be a head-scratcher. You probably don't want to manage two sets of settings, so (if you have it) add this line to that file to make it use the .bash_profile file instead:

if [ -f ~/.bash_profile ]; then
   source ~/.bash_profile

Thanks to this gist by Avi Flombaum for this tip.

$PATH (optional)

Recall from the previous lesson that the $PATH variable, which gets set up in the .bash_profile file, contains all the directories that Bash is going to search through when it doesn't know what command you just typed. If you install things and it can't figure out where to find them, you'll need to add them to $PATH. You probably don't need to do anything with this until you run into a relevant error, so keep this in your back pocket until then.

To add to $PATH, just separate the directories with colons, and you can either jam them all together in one $PATH variable which gets "exported" to the environment like this (from my machine):

# In ~/.bash_profile
export PATH="$HOME/.rbenv/bin:/usr/local/bin:/usr/bin:/sbin:/usr/local/sbin:usr/local/bin:/usr/local/mysql/bin:./:$PATH"

...or you can manually add individual items by first setting them to a variable and then adding it onto the $PATH like this:

# In ~/.bash_profile
export MYSQL_DIR=/usr/local/mysql/bin

The only important one will be making sure that you search through /usr/local/bin before (to the left of) the usr/bin directory. You'll see this come up during the installation section if yours hasn't been set that way yet.

Git Prompt (Optional)

When you're working with Git, it can be quite helpful to see which branch you are on and the status of that branch. There is a simple Git prompt you can set up which displays all of these things for you. Check out bash-git-prompt here.

Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

Sorry, comments aren't active just yet!

Next Lesson: Terms to Know