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.
Zsh, 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.
You can install Zsh using Homebrew with
brew install zsh.
While you're at it, you can also install the most popular Zsh plugin, oh-my-zsh, that comes with many built-in plugins and themes using this install script:
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
My teammate Valériane Venance tweeted about the
takecommand which creates a new directory and changes to it, thus eliminating the need to type the two commands
takewill also make intermediate directories as needed.
Searching through history. 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. My wonderful teammate Matthew Gilliard told me about
ctrl-r ___which finds the last time you searched for the given phrase. Clicking
ctrl-ragain would find the second most recent, then third, etc.
cd. No need to type
cdto switch directories, just type the directory name.
Mass rename files with
zmv. 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'
What does that command mean?
(*).(jpg|jpeg)finds each file in the directory that ends in either
epcot-$1.$2says to edit each file name by prepending
epcot-followed by the original file name (represented by
$1) and then the original file type (with
Bountiful potential plug-ins. As mentioned in the Installation section, oh-my-zsh comes with many plug-ins. You would include the
sudoplug-ins by adding this line to your
plugins=(npm sudo). Some stand-out plug-ins include
npmadds auto-complete to
sudoadds--you guessed it--
sudoto a command even after you've typed it by clicking
zsh-autosuggestionssuggests commands while you type according to your history of previous commands and completions. (
web-searchlets you open a search engine from your command line: running
google ___will search Google for the given phrase, ie. Serena Williams as shown below.
gitincludes many aliases for git commands, letting you type less and save time. Why type
git addwhen you can type
gainstead? More aliases are listed here.
Park a command.
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.
Easily edit a command after you've typed it on the command line. If you've typed or pasted a long command and decide you need to edit it before running,
ctrl-x-eopens it in an editor (usually vi, but you can set it to be any text editor with the
Key combo shortcuts. Type
ctrl linstead of
clearto clear your terminal.
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 and this list of ZSH frameworks, themes, and plug-ins maintained by Twilio engineer Joe Block, and let me know online what your terminal looks like!