Zsh Tricks to Blow your Mind
Tags: productivity, shell, discuss, tips
Published: 2021-02-17 19:51:26

---
title: Zsh Tricks to Blow your Mind
published: true
description: 
tags: productivity, shell, discuss, tips
cover_image: https://twilio-cms-prod.s3.amazonaws.com/images/Copy_of_Generic_Blog_Header_4_dfkSriJ.width-1616.png
---
This blog post was written for [Twilio](https://www.twilio.com/) and [originally published on the Twilio blog](https://www.twilio.com/blog/zsh-tricks-to-blow-your-mind).

In UNIX-based programming, we often talk about shells, terminals, and the command line interfaces. Bash is probably the most well-known, but there are other widely-used options as well, such as Zsh or the Z shell. Read on to learn more about Zsh and some tips and tricks to optimize your development.

### What is Zsh?
[Zsh](https://github.com/ohmyzsh/ohmyzsh), also known as the Z shell, extends functionality of the Bourne Shell (sh), offering newer features and more support for plugins and themes. Starting with MacOS Catalina in 2019, Zsh became the default login and interactive shell in Mac machines.

### Install Zsh
You can install [Zsh](https://brew.sh/) using Homebrew with `brew install zsh`.

While you're at it, you can also install the most popular Zsh plugin, [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh), that comes with many built-in plugins and themes using this install script:
```bash
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
```

### 9 Lesser-Known Zsh Tips and Tricks
1. <em>[My teammate Valériane Venance tweeted about the `take` command](https://twitter.com/valeriane_IT/status/1359433683506262016) which creates a new directory and changes to it, thus eliminating the need to type the two commands `mkdir` and `cd`.</em> `take` will also make intermediate directories as needed.
![take example](https://twilio-cms-prod.s3.amazonaws.com/images/j3U3-JIccLp-w9U6_-JOG2it7X_GyYkCHOpNByHSN_GLy.width-1600.png)
2. <em>Searching through history.</em> In the terminal, pressing the up arrow cycles through previous commands you used starting with the most recent first. With Zsh, you can cycle through times you used a specific command, ie. `mkdir`.
![search history of mkdir uses](https://twilio-cms-prod.s3.amazonaws.com/original_images/NLVN2cW1nu4TtxchyotrhRkm5zXqji_pL06nVLkEYk4sW672_iO0rnwEEVdbhZCDI27sCsiLg7Si_S)
[My wonderful teammate Matthew Gilliard told me about `ctrl-r ___`](https://twitter.com/MaximumGilliard/status/1147113043052113924) which finds the last time you searched for the given phrase. Clicking `ctrl-r` again would find the second most recent, then third, etc.
![cmd-r example](https://twilio-cms-prod.s3.amazonaws.com/original_images/Y73UIsvfNntxscPwBVLT-IZA_mODIgePdVhNBP90aGINVbNMKxUwdw6m0gRF8QrQTdRS2wSME_P1ug)
3. Automatic `cd`. No need to type `cd` to switch directories, just type the directory name.
4. <em>Mass rename files with `zmv`.</em> To install zmv, run `autoload zmv`. I downloaded a lot of images for a machine learning model and wanted to rename them to be more consistent (ie. epcot-1.jpg, epcot-2.jpg, epcot-3.jpg… instead of 1.jpg, 2.jpg, 3.jpg...) The command to do so would be `zmv '(*).(jpg|jpeg)' 'epcot-$1.$2'`. To check what would happen before running the command, you can add `-n`, instead running `zmv -n '(*).(jpg|jpeg)' 'epcot-$1.$2'`
![check mass renaming using zmv -r](https://twilio-cms-prod.s3.amazonaws.com/images/tcdT37LNVuRR1lmA7c_ivOEK9eAiRS1EOacPWRy7zwVGN.width-1600.png)
What does that command mean? `(*).(jpg|jpeg)` finds each file in the directory that ends in either `.jpg` or `.jpeg`. Then `epcot-$1.$2` says to edit each file name by prepending `epcot-` followed by the original file name (represented by `$1`) and then the original file type (with `$2`).

5. <em>Perform calculations from the command line like a calculator.</em>
![calculations with zcalc](https://twilio-cms-prod.s3.amazonaws.com/images/lKp36T5JCcKhvGh4NWFOMrDk3Va2ghtR-VRGKyuegTJU3.width-1600.png)
6. <em>Bountiful potential plug-ins.</em> As mentioned in the <em>Installation</em> section, oh-my-zsh comes with many plug-ins. You would include the `npm` and `sudo` plug-ins by adding this line to your `~.zshrc` file: `plugins=(npm sudo)`. Some stand-out plug-ins include
    * `npm` adds auto-complete to `npm` commands.
    * `sudo` adds--you guessed it--`sudo` to a command even after you've typed it by clicking `esc` twice.
![sudo with esc-esc](https://twilio-cms-prod.s3.amazonaws.com/original_images/escesc.gif)
    * `zsh-autosuggestions` suggests commands while you type according to your history of previous commands and completions.
![zsh-autosuggestions gif](https://twilio-cms-prod.s3.amazonaws.com/original_images/URTQRFT6coapY1IGBvRUyo5Gik0kbZxZKlQfImvcNIQGwD5JRxrdjlsTTnpuHZ_zeFPTwN5FNdn0B7)
( `web-search` lets you open a search engine from your command line: running `google ___` will search Google for the given phrase, ie. Serena Williams as shown below.
![search Serena Williams via the command line](https://twilio-cms-prod.s3.amazonaws.com/original_images/google.gif)
    * `git` includes many aliases for git commands, letting you type less and save time. Why type `git add` when you can type `ga` instead? [More aliases are listed here](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/git).
![example of ga .](https://twilio-cms-prod.s3.amazonaws.com/images/uuQ8yjyqWYgAbAfaflnmvKCGdhgOQavB4oEqV7ZvTJULF.width-1600_VDheRVg.png)
7. <em>Park a command.</em> `Ctrl-q` "parks" the command you're currently typing and takes you back to the prompt, letting you start over and type another command. Once you run that other command, the original command is un-parked and refills the command line so you can continue--this is good for if you, say, forgot to do a command before a command. 
![ctrl-q parks a command for later](https://twilio-cms-prod.s3.amazonaws.com/original_images/ctrlq.gif)
8. <em>Easily edit a command after you've typed it on the command line.</em> If you've typed or pasted a long command and decide you need to edit it before running, `ctrl-x-e` opens it in an editor (usually vi, but you can set it to be any text editor with the `$EDITOR` environment variable.)
9. <em>Key combo shortcuts</em>. Type `ctrl l` instead of `clear` to clear your terminal.
### What's Next for Turbo-Charging your Shell
This is just the beginning--there is so much more you can do with zsh and oh-my-zsh. Check out [this oh-my-zsh cheat sheet](https://github.com/ohmyzsh/ohmyzsh/wiki/Cheatsheet) and [this list of ZSH frameworks, themes, and plug-ins](https://github.com/unixorn/awesome-zsh-plugins) maintained by Twilio engineer Joe Block, and let me know online what your terminal looks like!