75 Zsh Commands, Plugins, Aliases and Tools

James Hibbard
Share

I spend a lot of my day in the terminal, and my shell of choice is Zsh — a highly customizable Unix shell that packs some very powerful features. As I’m a lazy developerTM, I’m always looking for ways to type less and to automate all the things. Luckily this is something that Zsh lends itself well to.

In this post, I’m going to share with you 75 commands, plugins, aliases and tools that will hopefully save you some keystrokes and make you more productive in your day-to-day work.

If you don’t have Zsh installed on your machine, then check out this post, where I show you how to get up and running.

15 Things Zsh Can Do out of the Box

Zsh shares a lot of handy features with Bash. None of the following are unique to Zsh, but they’re good to know nonetheless. I encourage you to start using the command line to perform operations such as those listed below. It might seem like more work than using a GUI at first, but once you get the hang of things, you’ll never look back.

  • Entering cd from anywhere on the file system will bring you straight back to your home directory.
  • Entering !! will bring up the last command. This is handy if a command fails because it needs admin rights. In this case you can type sudo !!.
  • You can use && to chain multiple commands. For example, mkdir project && cd project && npm init -y.
  • Conditional execution is possible using ||. For example, git commit -m "whatever..." || echo "Commit failed".
  • Using a -p switch with the mkdir command will allow you to create parent directories as needed. Using brace expansion reduces repetition. For example, mkdir -p articles/jim/sitepoint/article{1,2,3}.
  • Set environment variables on a per-command basis like so: NODE_DEBUG=myapp node index.js. Or, on a per-session basis like so: export NODE_DEBUG=myapp. You can check it was set by typing echo $<variable-name>.
  • Pipe the output of one command into a second command. For example, cat /var/log/kern.log | less to make a long log readable, or history | grep ssh to search for any history entries containing “ssh”.
  • You can open files in your editor from the terminal. For example, nano ~/.zshrc (nano), subl ~/.zshrc (Sublime Text), code ~/.zshrc (VS Code). If the file doesn’t exist, it will be created when you press Save in the editor.
  • Navigation is an important skill to master. Don’t just rely on your arrow keys. For example, Ctrl + a will take you to the beginning of a line.
  • Whereas Ctrl + e will take you to the end.
  • You can use Ctrl + w to delete one word (backw­ards).
  • Ctrl + u will remove everything from the cursor to the beginning of the line.
  • Ctrl + k will clear everything from the cursor to the end of the line. These last three can be undone with Ctrl + y.
  • You can copy text with Ctrl + Shift + c. This is much more elegant than right clicking and selecting Copy.
  • Conversely, you can paste copied text with Ctrl + shift + v.

Try to commit those key combos to memory. You’ll be surprised at how often they come in handy.

15 Custom Aliases to Boost Your Productivity

Aliases are terminal shortcuts for regular commands. You can add them to your ~/.zshrc file, then reload your terminal (using source ~/.zshrc) for them to take effect.

The syntax for declaring a (simple) alias is as follows:

alias [alias-name]='[command]'

Aliases are great for often-used commands, long commands, or commands with a hard-to-remember syntax. Here are some of the ones I use on a regular basis:

  • A myip alias, which prints your current public IP address to the terminal: alias myip='curl http://ipecho.net/plain; echo'.
  • A distro alias to output information about your Linux distribution: alias distro='cat /etc/*-release'.
  • A reload alias, as I can never seem to remember how to reload my terminal: alias reload='source ~/.zshrc'.
  • An undo-git-reset alias: alias undo-git-reset-head="git reset 'HEAD@{1}'". This reverts the effects of running git reset HEAD~.
  • An alias to update package lists: alias sapu='sudo apt-get update'.
  • An alias to rerun the previous command with sudo: alias ffs='sudo !!'.
  • Because I’m lazy, I have aliased y to the yarn command: alias y='yarn'. This means I can clone a repo, then just type y to pull in all the dependencies. I learned this one from Scott Tolinski on Syntax.
  • Not one of the ones I use, but this alias blows away the node_modules folder and removes the package-lock.json file, before reinstalling a project’s dependencies: alias yolo='rm -rf node_modules/ && rm package-lock.json && yarn install'. As you probably know, yolo stands for You Only Live Once.
  • An alias to open my .zshrc file for editing: alias zshconfig='subl $HOME/.zshrc'.
  • An alias to update the list of Ruby versions rbenv can install: alias update-available-rubies='cd ~/.rbenv/plugins/ruby-build && git pull'
  • An alias to kick off a server in your current directory (no npm packages required): alias server='python -m SimpleHTTPServer 8000'.
  • You can also create an alias to open documentation in your browser: alias npmhelp='firefox https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/npm'.
  • A global alias to pipe a command’s output to less: alias -g L='| less'. You can use it like so: cat production.log L.
  • A global alias to pipe a command’s output to grep: alias -g G='| grep'. You can use it like so: history G ssh.
  • You can also use functions to create aliases. The following (taken from here) creates an alias that adds, commits, and pushes code to GitHub:
    bash
    function acp() {
    git add .
    git commit -m "$1"
    git push
    }

There are lots of places to find more ideas for aliases online. For example, this Hacker News discussion, or this post on command line productivity with Zsh.

15 Cool Things You Can Do with (Oh My) Zsh

Oh My Zsh is a community-driven framework for managing your Zsh configuration and comes bundled with thousands of helpful functions, helpers, plugins and themes. If you’re going to make the Z shell your daily driver, you should really install Oh My Zsh.

Here are fifteen useful things Oh My Zsh can do for you:

  • The take command will create a new directory and change into it. take my-project replaces mkdir my-project && cd my-project.
  • zsh_stats will give you a list of the top 20 commands and how many times they’ve been run.
  • Oh My Zsh simplifies navigating your file system. For example, .. is an alias for cd ...
  • In the same way, ... moves you up two directories, .... moves you up three, and ..... moves you up four.
  • You can omit the cd when navigating. Typing /, for example, will take you straight to your filesystem root.
  • Partial matching is also supported. For example, typing /h/j/De and pressing TAB, then Return, takes me to /home/jim/Desktop/.
  • rd is an alias for rmdir and md is an alias for mkdir -p.
  • You can type d to list the last used directories from a terminal session.
  • You can then navigate to any of these using cd -n, where n is the directory number.
  • Tab completion is another great feature. For example, typing ls - and pressing TAB will list all of the command’s options, along with a helpful description of what they do. This also works for cap, rake, ssh, and kill.
  • Typing alias lists all of your current aliases.
  • With globbing (a Zsh feature), you can list files with a particular extension. For example, ls *.html will list all HTML files in the current directory. To include subdirectories, change to: ls **/*.html.
  • Glob qualifiers allow you to select types of files by using flags. For example, ls -l **/*(.x) will find all executable files in the current directory and all sub-directories.
  • You can search for files by date modified. For example, ls *(m-7) will list all files modified within the last week.
  • You can search for files by size. For example, ls *(Lm+1) will find all files with a size larger than 1MB.

Using Plugins for Fun and Profit

Oh My Zsh ships with a lot of plugins. You should look through these and invest some time learning those that will help your workflow.

Here are three plugins I regularly use, that provide a ton of handy shortcuts and aliases.

10 Nifty Git Aliases

The git plugin provides many aliases and several useful functions. Why not go through these and attempt to memorize your top ten? Here are the ones I use most often.

  1. g is a handy alias for git. This means you can type things like g clone <whatever> instead of git clone <whatever>. Might only be two keystrokes, but they soon add up.
  2. gaa is an alias for git add all. I use this one all the time.
  3. gb is an alias for git branch, which will list all of the branches in the current repo and show you which one you’re on.
  4. gcb is an alias for git checkout -b, the command that allows you to create a new branch.
  5. gcm is an alias for git checkout master. This returns you to the master branch.
  6. gdca is an alias for git diff --cached. This allows you to diff any files you’ve staged for commit.
  7. gf is an alias for git fetch.
  8. gm is an alias for git merge.
  9. gp is an alias for git push. To sync a fork of a repo, you could do: gf upstream, gm upstream/master, followed by gp.
  10. glog is an alias for git log --oneline --decorate --graph, which will give you a pretty git branch graph.

10 Handy npm Aliases

The npm plugin provides completion as well a bunch of useful aliases.

  • npmg is an alias for npm install --global, which you can use to install dependencies globally on your system. For example, npmg nodemon.
  • npmS is an alias for npm install --save, which you use to install dependencies and add them to the dependencies section of your package.json. Note that, as of npm 5.0.0, this is the default when running npm i <package>.
  • npmD is an alias for npm install --save-dev, which you use to install dependencies and add them to the devDependencies section of your package.json.
  • npmO is an alias for npm outdated, which will check the registry to see if any (or, specific) installed packages are currently outdated.
  • npmL is an alias for npm list, which will list installed packages.
  • npmL0 is an alias for npm list --depth=0, which lists top-level packages. This is especially useful for seeing which modules are installed globally without flooding your terminal with a huge dependency tree: npmL0 -g.
  • npmst is an alias for npm run start, an npm script often used to start an application.
  • npmt is an alias for npm run test, which, as you might guess, is used to run your tests.
  • npmR is an alias for npm run. On its own, this command will list all of a project’s available npm scripts, along with a description of what they do. Used in conjunction with a script name, it will run that script, For example, npmR build.
  • npmI is an alias for npm init. This will ask you a bunch of questions, then create a package.json based on your answers. Use the -y flag to automate the process. For example, npmI -y.

10 Time-saving Rails/Rake Aliases

This plugin adds completion for the Ruby on Rails framework and the Rake program, as well as some aliases for logs and environment variables.

  • rc is an alias for rails console, which allows you to interact with your Rails app from the CLI.
  • rdc is an alias for rake db:create, which (unless RAILS_ENV is set) creates the development and test databases for your app.
  • rdd is an alias for rake db:drop, which drops your app’s development and test databases.
  • rdm is an alias for rake db:migrate, which will run any pending database migrations.
  • rds is an alias for rake db:seed, which runs the db/seeds.rb file to populate your development database with data.
  • rgen is an alias for rails generate, which will generate boilerplate code. For example: rgen scaffold item name:string description:text.
  • rgm is an alias for rails generate migration, which will generate a database migration. For example: rgm add_description_to_products description:string.
  • rr is an alias for rake routes, which list all of an app’s defined routes.
  • rrg is an alias for rake routes | grep, which will allow you to list and filter the defined routes. For example, rrg user.
  • rs is an alias for rails server, which launches the Rails default web server.

Additional Resources

The main job of the plugins listed above is to provide aliases to often-used commands. Please be aware that there are lots more plugins out there that augment your shell with additional functionality.

Here are four of my favorites:

  • sudo allows you to easily prefix your current or previous commands with sudo by pressing ESC twice.
  • autosuggestions suggests commands as you type based on history and completions. If the suggestion is the one you’re looking for, press the key to accept it. A real time saver!
  • command-not-found: if a command isn’t recognized in the $PATH, this will use Ubuntu’s command-not-found package to find it or suggest spelling mistakes.
  • z is a handy plugin that builds a list of your most frequent and recent folders (it calls these “frecent”) and allows you to jump to them with one command.

And don’t forget, if you spend a lot of time in the terminal, it’s worth investing some effort in making it visually appealing. Luckily, Oh My Zsh ships with a whole bunch of themes for you to choose from. My pick of the bunch is Agnoster.

You can find out more about themes in my article 10 Zsh Tips & Tricks: Configuration, Customization & Usage.

Conclusion

So there we have it: 75 Zsh commands, plugins, aliases and tools. I hope you’ve learned a trick or two along the way, and I encourage you to get out of your GUIs and into the terminal. It’s easier than it looks and a great way to boost your productivity.

If I’ve missed your favorite plugin, or time-saving alias/command, let me know on Twitter.

Want to get even more out of your toolkit? Check out Visual Studio Code: End-to-End Editing and Debugging Tools for Web Developers from Wiley.