Developer Digs: Customizing Your Terminal

Senior Engineer at PRPL, Aaron Gidusko, gives us a peek at his terminal along with some helpful tips for customizing your own.

Programmers spend a lot of time looking at a terminal. A million years ago, aesthetic and comfort were the least of concerns for terminal interfaces because computers barely worked and were limited to one color.

Seriously. Here, take a look:

Let’s take a few steps back.

WTF is a Terminal??

If you don’t know what terminals are, you are probably more physically fit than I am. To explain, let’s first start with how we access our computers. We normally use our computers via Graphical User Interfaces, or GUIs (pronounced “Gooeys”). GUIs are the iconic and beautiful colorful screens filled with apps and icons that we instantly recognize as either Windows or Mac, all navigable through a mouse or trackpad.

Terminals are just another type of text-only interface to computers where you type really explicit instructions about really specific things you want the computer to do. But I will save my breath on the specifics and let you read this for more on how a terminal works.

A Better Looking Future

It’s 2017 and terminals don’t have to look awful anymore. And they shouldn’t either because life is much easier when your terminal looks neat and is easy to navigate. Today, users can add features that report the weather, list the current playing song, and even make conversation.

The experience of customizing your terminal is bizarre, arcane, and takes a lot of time. Most of getting your setup just the way you like it will involve copying and pasting things you don’t understand from people who may or may not know what they’re talking about.

That’s where I come in.

I’m going to walk through my personal terminal customizations. And since I use the same setup on every machine I work on, I’ll also show you how I keep my changes synchronized.

Getting Fancy

Let’s take a peek at my terminal:

I use Bash. A lot of developers here at PRPL use Z Shell, because they’re too cool for Bash, but these are the contents of my ~/.bashrc file and my rationale. I use these configurations on both my macOS laptops and the other Linux machines I connect to during the day, so there’s a lot of environment detection built into them. I try to keep it all portable.

Source the System’s Bashrc File

if [ -f /etc/bashrc ]; then
. /etc/bashrc
Fi

If the system has a bashrc file defined already, it (presumably) includes configuration that the system administrator intends every user on the machine to use. If it exists, you want to source it to make sure you’re honoring the admin’s wishes.

Enable Bash Completion

if ( which brew >> /dev/null ); then
if [ -f $(brew — prefix)/etc/bash_completion ]; then
. $(brew — prefix)/etc/bash_completion
fi
Fi

Bash Completion is a powerful utility that aids auto-completion of many commands. More than helping you auto-complete the names of available programs, it can even help you with the parameters those commands take. Installing it on macOS is super easy.

Setting your $PATH

#export PATH=/usr/local/opt/node@6/bin:$PATH
#export PATH=/Applications/MAMP/bin/php/php5.6.28/bin:$PATH

Your $PATH system variable determines which programs can be found and executed on the system. Normally you won’t need to change it, but sometimes you need to use very particular versions of runtimes from the command line, and not all of them have convenient version managers like Ruby and Python. I like to keep all of my $PATH modifications in the same place, and comment out additions that I only need some of the time.

Color

export LSCOLORS=gxBxhxDxfxhxhxhxhxcxcx

You can color code directory listings output by the ls command by exporting the $LSCOLORS variable. Generators online can help you find your preference.

# High Intensity
IBlack=’\e[0;90m’ # Black
IRed=’\e[0;91m’ # Red
IGreen=’\e[0;92m’ # Green
IYellow=’\e[0;93m’ # Yellow
IBlue=’\e[0;94m’ # Blue
IPurple=’\e[0;95m’ # Purple
ICyan=’\e[0;96m’ # Cyan
IWhite=’\e[0;97m’ # White
Color_Off=’\e[0m’ # Text Reset

Color is super important in any scenario where a human eye is looking at a lot of similar images for a long time. The block above sets up some friendly variable names for some of the color codes I’ll use later on when customizing the command prompt.

Default Options for Common Commands

# Default options
alias ls="ls -lah"
alias grep='grep --color=auto'

These are a few options that I want to use all the time for common commands. For ls, these flags give us long format, hidden files, and human-friendly file sizes.

A Better Git Status

alias         s=git_status
function git_status() {
if git ls-files >& /dev/null; then
git branch -avv;
echo "";
git status -s
else
echo 'This is not a git directory'
fi
}

I alias just the letter s to dump out this git status report. It shows the status of remote branches, as well as my working directory.

Customizing the Command Line

Naturally the most important part in tweaking your terminal is setting up your command prompt. This is easy to do by simply setting your PS1 variable.

For even more control, though, including conditional logic, you can set the PROMPT_COMMAND variable to a function that will execute that function whenever the prompt is drawn. 
Here is a look at the function I use to draw my prompt:

PROMPT_COMMAND=prompt_func

function prompt_func() {
# Initialize the prompt
PS1=""

# Add a newline
PS1="\n"

# Status of last command
PS1+='`if [ $? = 0 ]; then echo "\[\033[01;32m\]•"; else echo "\[\033[01;31m\]•"; fi` '

# Command history number
PS1+="$ICyan\!$Color_Off "

# User and hostname
PS1+="$IYellow\u@\h$Color_Off "

# Current time
PS1+="`date +%H:%M:%S` "

# Working branch (If folder is a GIT folder)
PS1+="$ICyan`__git_ps1`$Color_Off "

# Working directory
PS1+="$IGreen\w$Color_Off "

# macOS only - Add the current Spotify track and artist
if [[ "$OSTYPE" == "darwin"* ]]; then
PS1+=" "
PS1+="$IPurple"

PS1+=`osascript -e 'if application "Spotify" is running then
tell application "Spotify" to name of current track as string
end if'`;

PS1+=" - "

PS1+=`osascript -e 'if application "Spotify" is running then
tell application "Spotify" to artist of current track as string
end if'`;
fi

PS1+="$Color_Off"

# Add a newline
PS1+="\n"

# Add a trailing dollar sign
PS1+="\$ "
}

There’s a lot going on here so I’ll break it down:

  • I add a newline before each prompt. This spaces my prompt out a bit from the output of the last command.
  • I add the return code of the last command executed, rendered as either a red or a green dot on the left side of my prompt. This is useful for quickly determining if the most recent command was successful without having to, you know, actually read anything.
  • I add the history number of the next command I’m about to type. You can replay a command from history by typing ! followed by the history number. So if I ran some long command I don’t feel like typing again at history number 1545, I can just scroll through my buffer until I find it and then run it again by typing !1545
  • I add the user and host I’m logged into as well as the date and the time.
  • If I’m inside a git repository, I add the current git branch and status of the working directory, followed by the working directory’s name.
  • If I’m on macOS and running Spotify (which I am here), I use osascript to print out the track and artist of the currently playing song.

The end result looks like this:

Manage Your History File

export HISTCONTROL=ignoredups:erasedups # No duplicate entries
export HISTSIZE=100000 # Increase history file length
export HISTFILESIZE=100000 # Increase history file size
export HISTIGNORE=”&:ls:[bf]g:history:exit” # Ignore boring entries

These variables configure the behavior of the history file. Removing duplicate entries and ignoring boring stuff like ls help keep the history clean of junk.

Print the Operating System You’re Using

if ( which lsb_release >> /dev/null ); then
OS=$(lsb_release -si)
ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')
VER=$(lsb_release -sr)
echo $OS $ARCH $VER
fi

These lines will print the Linux distro and version to the screen as soon as I connect to a machine, just to remind me what I’m working with.

Keeping it All in Sync

Whenever I have adjustments to this file or any of my other “dotfiles,” I can make the change to the machine I’m working on and then synchronize it with all of the other machines I use. This is easy to do because I keep all of my customization files in a public git repository on Github.

You can find the .bashrc file described above and some of my other preferences at https://github.com/prplaaron/dotfiles.

Whenever I connect to a new machine, the first thing I do is clone down this repo and install my preferences as symlinks by running the overwrite_and_link_dotfiles.sh script in the repo. It’s pretty aggressive, instantly nuking any files that are already in place, so before making the move, make sure it’s exactly what you want.

From then on, my machine and all other machines I use can push and pull the latest configuration changes to the Github repository, and I’ll always have whatever my latest preferences are on any machine I use.

Now It’s Your Turn

Since we don’t live in the stone age anymore, or even worse, the age of dial-up internet, it is time to put some effort into customizing your terminal. We, as programmers, spend a lot of our time staring at this screen, so why not make it look nice and do neat things? Whether you use Bash or Z Shell, the result is worth the investment of your time spent in customization. Your eyes and sanity will thank you later.

What kind of cool features do you have added onto your terminal? Tell us in the comments!


If you enjoyed this article written for Orlando product design company Purple, Rock, Scissors, please hit the heart below to recommend it to others!

Like what you read? Give Aaron Gidusko a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.