Setting Up Your macOS Terminal

PUBLISHED ON JAN 20, 2018 / 9 MIN READ

Why Should I Do This?

Well… have you seen Apple’s offerings at a terminal? As a software developer, you will extensively use the command line, as it offers faster, more efficient use of your computer.

I am of the firm belief that if you want comfortable, easy usage of your command line, you should customise it to how you want it!

Brew - the Missing Apple Package Manager

Brew is a macOS package manager, this means you can install various UNIX packages (like wget - a way to download webpages) with one simple, worry-free command. Begin by installing brew through curl-ing their install script: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

To use brew (we will install wget as an example), do brew install wget. You may need brew to install modules like pip, which is python’s package manager.

iTerm2

This is the bit where the going gets good. macOS’s ‘Terminal’ app is seriously lacklustre, it doesn’t offer tabs, cannot split itself, doesn’t allow profiles. A far superior terminal is iTerm2, which can be downloaded from their website.

The download should be a ‘.zip’ file containing the application ‘iTerm’. Extract the application from the zip archive, and place it in your ‘Applications’ folder. macOS’s indexing should soon add it to your spotlight search1.

iTerm2 has some interesting features like ‘Shell Integration’2 which can allow you to right click on files on a server and download them. Read more iTerm in the documentation.

ZSH

Z Shell (ZSH) is the first step to unlocking the customisability of your terminal. We to install ZSH, we are going to use our recently-installed brew package manager! Do brew install zsh zsh-completions to install ZSH.

oh-my-zsh offers the next step in customisability. Much like Brew, install it through curl-ing their install script: sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

You will get a pretty cool ‘OH MY ZSH’ ASCII art, and suddenly colour will bless your terminal. At this point I want to draw your attention to the themes offered by oh-my-zsh. Take your time to find one you like (some have built in git status!), and note down the name of it.

We are now going to apply the theme you have chosen! Open the configuration file for zsh, by doing nano ~/.zshrc. change the ‘ZSH_THEME=“robbyrussell”’ to whichever name you want - you can come back and change it whenever you like.

If you want to go really over the top, you can install more packages like ‘Powerline’ or ‘Airline’ (Airline is only for Vim).

Vim

Vim is a text editor that has an infamously steep learning curve, however it has some very, very powerful tools built inside of it. You may ask ‘why on earth would I use Vim when I have nicer, graphical alternatives like Atom’, that is a good question, but you must remember that as a software developer there may be instances in which you may need to write to a file, and the only way to do it is through command line, where of course Vim is installed.

Why Vim over something like Nano or Emacs? Another good question, Vim offers more customisability and features than Nano. Some people may like to use Emacs, as it has some incredible powerful properties like undoing redos (if you undo something a bunch, and write, you can’t redo, but you can in Emacs). I personally feel Vim is more understandable, so I have catered this post towards it.

To customise Vim, we are going to use Vim for the first time. It’s much like nano, but has some interesting quirks. Run the command vim ~/.vimrc

You will be presented with a blank file! This means we are ready to enter our config, I’ll guide you through your first time with Vim:

  1. Copy the text from below.
  • In the terminal window with Vim open, press the colon ‘:’, and type ‘set paste’, press enter to run the command. This puts Vim in a nice state that handles pastes well.
  • press ‘i’ (this enables insert mode).
  • Press ⌘-v to paste (you should know this one).
  • Press Escape to leave insert mode.
  • Press ‘:’ to enable Vim commands.
  • Type ‘wq’ and press enter, this ‘writes and quits’ vim.

And voilà! you now have a customised Vim terminal. You can confirm this by opening the previous example (press up arrow key to access previous commands), and seeing the syntax highlighting and other features enabled.

~/.vimrc example

"Enable mouse usage"
set mouse=a

"Enable line numbering, syntax highlighting and cursor position"
set number
syntax on
set ruler

"Various settings to make tabs better"
set expandtab
set softtabstop=4
set expandtab
filetype indent plugin on
set autoindent
set backspace=indent,eol,start

"Other fun stuff"
set confirm
set wildmenu
set showcmd

"Makes the colon map to the semicolon - useful when entering commands"
nnoremap ; :

"Search customisation"
set hlsearch
set ignorecase
set smartcase

For more information on how Vim works, I strongly recommend the Vim Wiki, In reality though, there is no substitute for experience, so try using Vim (it gets easier I promise), and soon you will probably prefer it over applications like word.

tmux

tmux is probably my single most favorite terminal tool. tmux, a so-called “terminal multiplexer” acts like a window manager inside the terminal, you can create multiple windows and panes within a single terminal window.

Honestly - it’s amazing, it makes working so much quicker as it allows you to switch between things quickly, not have to close applications to make room for others, and customise to your specific use case.

Now I’ve gushed enough over this fantastic piece of software, you can install it with Brew! Run brew install tmux to obtain it, and then enter tmux to enter your first ever session.

The status bar on the bottom of your terminal is a critical part of tmux, it shows some system information as well as the open windows you have.

From here, you can split panes by using the ‘prefix’ followed by a ‘command’. To perform your first pane split, do ‘ctrl-b’ and then ‘%’. Hazar - your terminal is cleaved in two vertically!

There is a lot to learn with tmux - but the best thing you can do is get stuck in and figure it out as you go, you’ll pick it up very quickly!

I am personally not a fan of tmux’s original settings, so I have a .tmux.conf in my dotfiles that you can use (see the end of the post).

SSH

The easiest way to connect to a remote machine to configure it is through SSH. SSH can be used through putting in a command for the server you want to connect to, e.g. ssh user123@192.168.1.1. This has the disadvantage of you having to remember or write down all the server IPs you want to connect to. Fortunately, there is a simpler, better way to use ssh.

We are going to now set up ssh to be used in a way that you can access your servers by calling their ‘nickname’ as such. A server Hostname allows you to remember distinguish which server you are currently accessed to. By changing some config files we can set this up such you can do ssh hostname and connect to your server.

Open ‘~/.ssh/config’ with Vim (or your favourite editor) - this can be done with vim ~/.ssh/config.

You should presented with a blank file. You can now add your servers in the format like the example given here:

~/.ssh/config example

Host Server-Hostname
  HostName 192.168.1.1
  Port 22
  User user123

Some VPS providers require a ssh key, to make one and add it to your ‘~/.ssh/config’ do the following:

  1. Generate a keypair with ssh-keygen -t rsa -C "your_email@example.com"
  • When prompted to “Enter a file in which to save the key”, press Enter. This will put the file in the default location.
  • At the prompt, enter a secure passphrase (this will be the password you enter to use your generated key).
  • The default location is ‘~/.ssh/id_rsa’, if you need to put your public key into a server’s setup, you need the contents of the ‘~/.ssh/id_rsa.pub’ file. The command more ~/.ssh/id_rsa.pub should reveal the contents to terminal, so you can copy it.
  • You now need to add your private side of the key to your ‘~/.ssh/config’ file, an example is shown below.

~/.ssh/config (with a ssh key) example

Host Server-Hostname
  HostName 192.168.1.1
  Port 22
  User user123
  IdentityFile ~/.ssh/id_rsa

The good news here is that you can use your public and private keys for all your services - be they github or ssh logins.

The only word of warning is DO NOT SHARE YOUR PRIVATE KEY! This would give everyone access to your stuff.

XCode Command Line Tools

XCode command line tools offer a lot of packages, that I think are pretty useful for the aspirant software developer. In your terminal, run the command xcode-select --install to run the installer.

Roll your own commands

Since you have zsh running, you can create something called a .zshenv. The .zshenv file you create (in your home directory ‘~') can contain any (bash) functions that you run regularly.

An example that I commonly use to show people is an ‘extract’ command that I use a lot. I can never remember the arguments for tar, so I have a ‘smart’ extract command remember them for me!

Here it is:

## the 'I hate tar' function
extract () {
  if [ -f $1 ] ; then
      case $1 in
          *.tar.bz2)   tar xvjf $1    ;;
          *.tar.gz)    tar xvzf $1    ;;
          *.bz2)       bunzip2 $1     ;;
          *.rar)       rar x $1       ;;
          *.gz)        gunzip $1      ;;
          *.tar)       tar xvf $1     ;;
          *.tbz2)      tar xvjf $1    ;;
          *.tgz)       tar xvzf $1    ;;
          *.zip)       unzip $1       ;;
          *.Z)         uncompress $1  ;;
          *.7z)        7z x $1        ;;
          *)           echo "don't know how to extract '$1'..." ;;
      esac
  else
      echo "'$1' is not a valid file!"
  fi
}

… and this is just pasted in my .zshenv!

I have lots of other examples in my dotfiles… speaking of:

My Dotfiles (my rice)

My dotfiles can be found on my GitHub. Feel free to copy them directly to your home, or steal things as you deem them useful!

I recommend you look at:

  • .vimrc (you will need to install vundle!)
  • .zshenv
  • .scripts
  • .tmux.conf

because they are really the most important things in my dotfiles.

MATLAB in Terminal - Something for the Engineering Students Reading This

Although the MATLAB application has some really good editing features, we may need to be able to run it remotely. Fortunately we can access MATLAB through our terminal.

MATLAB will not create a ‘matlab’ command when it is installed, so we have to create an ‘alias’. An alias is a command we can call, that will execute something longer and more complicated, just like how we did earlier for SSH.

To build the alias, run in your command window (taking care to change the MATLAB version to the one you have installed.

alias matlab="/Applications/MATLAB_R2017b.app/bin/matlab -nodesktop -nosplash"

This will allow you to run matlab wherever you are in terminal and be greeted by a MATLAB prompt!


  1. Use ⌘-space to bring up spotlight, the text entry should auto-focus to it, so you can type any application or file in, and macOS will find it. ↩︎

  2. The easiest way to install shell integration is to select the iTerm2>Install Shell Integration menu item. This can be done for any servers you are connected to (that you have the ability to install things on) as well, so you can right click on files to download etc. ↩︎