• Home
  • About
    • Rotendahl photo

      Rotendahl

      A site containing teaching material, code projects, and rants

    • Resume
    • Email
    • Twitter
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

Terminal configuration

23 Feb 2019

Reading time ~8 minutes

Every time I’ve been a tutor I have given a short introduction to the shell, I was asked if I would write a short guide to how my terminal is configured. I won’t touch on how to actually use the terminal since there already exists a bunch of excellent guides out there, here is a short list of the some of them:

  • Codecademy A short guide that covers the basics of the command line, it does not require any prior knowledge.
  • Learn it the hard way As the name implies it does not hold your hand, personally I think this approach is more fun.
  • Over the wire A hard guide, it tasks you with using the terminal to find passwords on servers. It’s challenging but extremely fun and satisfying, if you have patience and some experience I recommend this guide.

The configuration guide is meant for macs, but should also work on linux systems. The guide starts with some mac specific stuff, so linux users just jump to section 3. (Windows uses, see link)

1. Step: Install Xcode (Mac only)

Xcode is apple’s development environment it has an IDE and a series of command line utilities that are needed. It’s free and can be installed by opening the terminal app and typing:

    $ xcode-select --install

It might take a while, so go get some of the beverage of your choice.

2. Step: Getting a package manager

We still need to install several more utilities and the first is a package manager. A package manager is like an “app store” for your shell, it can get programming languages, utilities, and libraries. Each linux distribution comes with a built in package manager, Ubuntu comes with apt-get, so if you want to install python you simply type:

    $ sudo apt-get install python # ubuntu only

Apple has not made a package manager, but there exists options for the for mac. Each has their own pros and cons, I prefer homebrew. Here is a quick pros and cons list of the three most popular package managers for the curios.

You can install homebrew by running this command:

    $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Below is some examples of basic homebrew usage

    $ brew install mono # Installs mono
    $ brew search node # Searches after all packages that has node in its name
    $ brew update # Updates brews list of packages
    $ brew upgrade # Upgrades all installed packages to their newest version
    $ brew doctor # Built in troubleshooting, if you ever have problems run this

We can use brew install to get a bunch of terminal utilities, but brew has an extra trick up its sleeve. Homebrew can “tap” repos that provide extra functionality Caskroom is an example of this.

    $ brew tap caskroom/cask

This allows us to install GUI apps from the command line. Instead of searching google for the .dmg file, installing and cleaning up we can run:

    $ brew cask install google-chrome steam skype

It can be confusing when you should use brew cask install vs brew install. A rule of thumb is to use cask if an app has a graphical interface.

Terminal emulator

MacOS has a built in terminal appropriately named terminal.app, it is decent but I prefer iterm2, it has a dropdown window and is easier to customize. We can use homebrew to install it

    $ brew cask install iterm2

Iterm has a bunch of settings for you to customize, open the app’s preferences and peruse the options. I recommend activating a hot key window (You can find the setting under the keys window).

How awesome is this?

Iterm has an option to load a configuration from an url, If you want my setup you can get set it in this gist. Once Iterm is set up you can access the hot key window with a user defined short cut I use altt.

Step 3: Oh my zsh!

Mac specific part over, welcome back linux people!

Bash is the most common shell, and is most likely what your using now and what you’ll encounter on every SSH connection you’re going to make. I recommend using the awesome oh-my-zsh framework for zsh. To install the shell run the following command:

    sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Enter your password in order to change shell, and enjoy the first step towards an awesome shell. Some of the benefits are of oh-my-zsh are presented in gif form below

Kickass autocompletion!

Just pres tab to get suggetions and cycle through them

Command specific history

By pressing↑ you will go back through you command history, but by pressing up after typing you will only go through commands that has the same prefix. In the example above we first cycle through any git commands, and after that only commits.

One of the down sides with zsh as opposed to bash is that the added complexity can slow down the shell, especially if you are navigating a large git repo. Remember that you can always start a bash shell by typing bash, use this to test for portability of your scripts and for the rare occasion when zsh is to slow.

Configuration

To customize and configure oh-my-zsh a dotfile is used. A dotfile is a hidden file, hidden in the sense that you have to type ls -a to see it, that is read as the first thing when opening a new shell. The file is located in your home directory and is called .zshrc. The file is just a series of shell commands, here you can place aliases, plugins, themes, and functions. I’m not going to go into two much detail of what all the options mean but i suggest taking a look at the plugins list, the extract and wd plugins are awesome.

Customize

There are three main things you can customize:

  • Emulator color theme:. These are the colors of you emulator, how you customize them depend on the emulator you’re using. If you’re using iterm some nice themes can be found here
Examples of different color themes

If you’re not using iterm, just google it.

  • The shell theme: This is the layout of the terminal. Some themes are complex and gives you a bunch of information such as host name, current directory, git status, virtual environment etc. The list here shows some nice themes you can test the theme out by changing the theme variable in your .zshrc file and reloading the shell with source .zshrc.
  • The font: Having a nice mono spaced font is essential and most likley what you are used to. A font can also support powerlines which makes themes such as bulletTrain possible.
The bullet train theme with a powerline font.

Follow the installtion guide in the bullet train repo or pick one of the powerline fonts and set the theme to agnoster if you want the theme from above.

Useful terminal utilities

The last thing I would like to show you is a list of clever utilities that makes using the terminal a more pleasant experience.

  • bat: Can be installed with brew install bat for mac people or sudo apt-get install bat for linux peeps. The utility is meant as a cat and less replacement it provides syntax highlighting when outputting to standard our but, degrades gracefully to cat when piped.

  • tldr: If you type tldr curl it will show the most common uses for curl with a short description. Perfect for when you know the name of a command but don’t want to search through man pages to find the proper usage.

  • jq: A json formatter that makes dealing with json data in the shell a joy. It has filtering, pretty printing, and is easy to use.

  • prettier: When you encounter ugly code, fire this off. It formats and cleans javacript and html out of the box, but has official plugins for python.

  • colurls: A replacement for ls that provides a better color scheme, git info, filetype info. It’s a ruby gem, so read the installtion part of its’ readme.

  • surge: Want to quickly get some info up on a sharable url? Then this is for you.

  • youtube-dl: Utility to download and convert youtube videos. Good for if you want to grab some videos for offline viewing or download youtube only songs.

  • fortune: A small joke utility that gives you a fortune, combines perfectly with the two utilities cowsay and lolcat. Install all three and place the command fortune -os | cowsay | lolcat at the bottom of your .zshrc for maximum dankness.

  • trash: A mac specific utility that can be installed with brew install trash. If you have ever mistakenly run rm on a file, you know that rm removes the file immediately. This utility as the name suggests moves it to the trash instead. You can alias it to alias rm="trash" and future you will thank you some day.

That’s it, Enjoy your new maxed out terminal! If you have questions or run into problems write a comment below, and I might get back to you.



blogproductivityjekyll Share Tweet