Last Saturday I went over the very basics of Unix shell prompt. Today I’m going to continue talking about the shell prompt from where that left off and offer you an overview of your bash, Z Shell, and a few more tips to get you set up for Rails development.

Bash vs. Z Shell

When using Unix, you have two basic choices. You may not know you had this choice if you already use Terminal (as we went through last Saturday), but your basic choices are between one shell call Z Shell and another called Bash. (This is an oversimplification because there are in fact many more than this but these are the two most popular.)

Z Shell, also known a Zsh, was invented by Paul Falstad in 1990. Like most choices in the world of Unix, people who use Zsh really like it for good reason. It has great features: spell checking, fancy path expansions, and a whole lot more.

Bash was released around the same time by someone else named Brian Fox in 1989. Bash is the default on macOS. It is the one that most people know. It doesn’t have as many shiny features, but it has features of its own. For example, if you’ve ever in your shell navigation to a directory, type a few first letters and press tab. Your shell beeps at you, indicating that there’s more than one option to complete the file path. Then press tab again and your shell shows you a list of options that would match your file path. This is known as double-tab. (Covered last week in this post.)

Bash is by far the more popular because it is the default, but die-hard Unix users and ‘power user’ programmers tend to prefer Zsh.

In 2020, the tables turned when Apple made Zsh the default in Mac OS Big Sur.

The examples shown here and generally in this tutorial are for Bash.

1) Ruby Version Manager

For sanity sake, you’ll want to install a tool call Ruby Version Manager (or rvm) that will let you switch between versions of Ruby.

You type rvm list to see the list the installed versions of Ruby. To use a version, type rvm use (for example, rvm use 2.6.4). To install a new version of Ruby, use rvm install (for example, rvm install 2.6.6)

2) Node Version Manager

For sanity sake, you’ll want to install a tool call Node Version Manager (or nvm) that will let you switch between versions of Node.

You type nvm list to see the list the installed versions of Node. To use a version, type nvm use (for example, nvm use 12.8.0). To install a new version of Node, use nvm install (for example, nvm install 12.16.3)

3) Set the default EDITOR for you Shell

Sometimes you are on the shell prompt and your shell prompt needs you to edit a document, save it, and “give it back” to the shell for processing. This can happen, for example, when you are making a commit for git merge conflict. The shell will look to see if you have a preferred EDITOR set. An EDITOR is any program you use to edit text files. Many old-school Ruby developers still use TextMate for its simplicity and speed, but you can easily use the built-in macOS tool TextEdit too. You can use a shell-based editor, like Emacs or Vim, but you’d have to decide between Emacs and Vim which is beyond the scope of this introduction.

For example, to set TextMate as your editor, in your ~/.bash_profile

export EDITOR='mate -w'

4) Git autocomplete on the shell prompt

Go here, right-click and choose “Save As…”, save this file to your home directory as ~/.git-completion.bash
(Remember, that ~ means your home directory.) Then add the following line to your ~/.bash_profile script

source ~/.git-completion.bash

That will tell your shell prompt to load the git autocomplete script.

When switching into a git branch, you can now hit tab for auto-complete and tab-tab for auto-suggest, just like filepaths in your Unix system, to navigate and choose the git branch name you want.

5) Show the current git branch on the shell prompt

Add this to your ~/.bash_profile script

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

Now when your working at your shell prompt, you will see the name of the current working git branch in parens. Here, the shell prompt is telling me that my CWD (current working directory) is ~/Work/_LEARNING/rails-6-0-test and that the branch I’m on is master.

I hope you’ve enjoyed this debugging tips & tricks Saturday.