Get Started with Command Line and Z Shell

April 28, 2014 (10y ago)

To develop a web application, tooling and workflow are very important. Taking the time to learn and master the command line is not only highly recommended but also required to make use of tools that will help you develop faster and gain more control of your workflow.

This post summarizes what you need to know to get started with the command line and shares some personal recommendations on setting up the command prompt on MAC OS X, but it applies to *nix as well.

What is a Shell?

The Shell is an application that offers interactive console or terminal access to a computer system. It lets you interact with applications on your computer through the command line. A command-line interface (CLI) is a mechanism for interacting with a computer operating system or software by typing commands to perform specific tasks, and a command-line interpreter then receives, parses, and executes the requested user command.

Most operating systems offer a command-line interface, but that doesn't mean the built-in version is best. MAC OS X comes with Terminal, however, there's a terminal emulator for Mac OS X that is more customizable and does amazing things out-of-the-box; it's called iTerm. If you are using a Windows machine, I'd recommend installing cygwin.

In order to use the command line prompt, you will need to memorize commands. Start with the basic system commands, and once you've mastered that, you'll catch up quickly with other tools such as gulp.js. Sayanee runs a great podcast on tech tools called Build Podcast; I recommend you watch this episode to get started.

https://vimeo.com/43649618

Setup

ZSH (Z Shell) is a powerful command interpreter for shell scripting; it is a modern Unix shell that is backward compatible with bash. The main features of zsh are the true auto tab completion and autocorrect, shared history over all terminals, history sub-string search, and the ability to customize your prompt among many other.

So, why is Zsh better than Bash?

  • Very customizable prompt
  • Autocorrection
  • Fast autocompletion that also gives you a keyboard-navigable completion list. Let's look at an example, Kill <tab> outputs the list of current processes whereas bash displays a list of files in the current directory.

Here's what you need to do to install or update ZSH on MAC OS X:

Apple comes with Bash as the default Shell. That might be fine for many users, but as mentioned before, I prefer Z shell, which is already included in Mac OS X Mavericks.

Out of the box, Mac OS X version 10.8.x (Lion) comes with zsh version 4.3.11 (i386-apple-darwin12.0). However, zsh is currently at version zsh 5.0.2 (x86_64-apple-darwin13.0). You can use Homebrew to install the newer version.

In order to use Homebrew, first, you need to install Command Line Tools for Xcode by running: xcode-select --install or update to XCode5. The Command Line Tool package gives terminal users many commonly used tools, utilities, and compilers, including make, GCC, clang, perl, svn, git, size, strip, strings, libtool, cpp, what, and many other useful commands that are usually found in default Linux installations.

Open the console and run brew install zsh.

Now, in order to change the default shell to zsh, run chsh -s /bin/zsh. Verify that you're running zsh after opening a new terminal with ps -o comm $$.

In iTerm settings, make sure it is running zsh.

Customization

Customizing the command prompt is different in different shells; I'm going to cover zsh here.

Zsh is widely used by the open-source community, and because of that, there are many open-sourced projects with snippets, functions, configurations, themes, plugins, modules you can leverage to personalize your shell prompt.

Oh-My-ZSH: the most popular with over 600 contributors on GitHub and many plugins that you can easily integrate. There's a similar project for Windows called oh-my-cygwin.

Zach Holman's dotfiles: features auto sourcing Zsh files, easy Zsh completion extensions, and a local bin folder for executables. The included Rakefile will symlink anything ending in .symlink to your ~ folder. What's great about Holman's dotfiles is the organization per topic - here you can read more about it. I believe since then people have started to follow this pattern. There are many dotfiles on GitHub https://github.com/search?q=dotfiles, and on the following link, you will find a good selection and categorization http://dotfiles.github.io.

YADR: it is a collection of the best bits of all the top dotfile repos, vim and zsh plugins curated in one place, into a simple and cohesive way of working. It has many vim plugins (90+), all under one roof, working together, each plugin researched and configured to be at its best, often with better shortcut keys. Zsh plugins based on Prezto, and all things are vimized: irb, Postgres command line, etc.

I'm currently using Oh-My-ZSH as a base for my configuration. You can find the detailed installation steps and documentation here. YADR has been stalled for a while, and Prezto, although very well documented, doesn't have the community support oh-my-zsh has.