Tutorial

How to Use Z Shell (zsh) on a Cloud Server

Published on July 1, 2013
How to Use Z Shell (zsh) on a Cloud Server

Intro

This articles covers how to use zsh on an cloud server. You can verify that zsh is installed on your VPS by simply trying to start it:

zsh

If you have zsh installed, you will launch right into that shell.

Otherwise, it is recommended that you install zsh before proceeding.

Using zsh

It might be difficult at this point to understand how much more powerful zsh is than a shell like bash. Let’s start by looking into the autocomplete capabilities of the shell.

Autocompletion

Let’s imagine that we want to install “vim-addon-manager” to help us organize our text editor configuration file. We can’t remember what the package is called though. Instead of using apt-cache to search for the package name, we can just use zsh to give a list of packages that start with “vim”.

sudo apt-get install vim<tab>
vim                vim-doc            vim-lesstif        vim-scripts
vim-addon-manager  vim-gnome          vim-migemo         vim-syntax-go
vim-athena         vim-gtk            vim-nox            vim-syntax-gtk
vim-common         vim-gui-common     vim-puppet         vim-tiny
vim-conque         vimhelp-de         vim-rails          vim-vimoutliner
vim-dbg            vim-latexsuite     vim-runtime        

When we use <tab> completion with apt, we see that it gives us a list of available packages. We can then type in the additional letters needed to get a unique hit. In this case, typing “-ad<tab>” will complete the command as needed.

sudo apt-get install vim-ad<tab>

Let’s make our autocomplete even more powerful. Let’s add the following section to our .zshrc:

nano ~/.zshrc
zstyle ':completion:*' menu select
setopt completealiases

This will do two things. It will let us autocomplete any aliases that we might write in our .zshrc, and it also lets us select out of a menu when there are multiple selections available. Let’s source the .zshrc file again to load our changes and then try the apt-get example again.

source ~/.zshrc
sudo apt-get install vim<tab>

It looks the same. The difference is that now we can press the <tab> key again and then use the <tab> and arrow keys to select your options. This can result in selecting much faster in some cases.

Globbing

One of the options we set up initially was to use extended globbing expressions. Globbing is a type of syntax that uses special symbols to match and filter results when searching or passing arguments to any program or function.

Let’s try a few examples. First, we’ll try some more traditional globbing in case you aren’t familiar with the idea. Let’s create a testing directory and populate it with some files.

mkdir test
cd test
touch file1 file2 file3 file100 file120 file122 file200 file222
touch file250 file800 file808 file80 somefile anotherfile
touch thisotherfile file.txt file.c file.o file.html file.css
touch completelydifferent different separate
mkdir directa directb directc directd

Our test directory now has a good number of files and directories that we can select and manipulate.

Let’s select everything with the word file in it. The asterisk (*) character stands in for 0 or more characters.

ls *file*
anotherfile		file120		file200		file3
file808			file.html	somefile	file1
file122			file80		file.c		file.o
thisotherfile		file100		file2		file250
file800			file.css	file.txt

The result is every file with the word “file” in its title. Let’s say that we only want to find files that begin with “file”.

ls file*
file1		file122		file222		file80
file.c		file.o		file100		file2
file250		file800		file.css	file.txt
file120		file200		file3		file808
file.html

This cuts down on our results a bit, but let’s narrow this down further. The caret character (^) is used to negate the pattern following. So let’s say we want all files that start with “file” but we don’t want any where “1” is immediately following “file”.

ls file^1*
file2		file222		file3		file800
file.c		file.html	file.txt	file200
file250		file80		file808		file.css
file.o

Let’s say we want to find all files that begin with “file” and have a number afterwards ranging from 100 to 300. We can use the less than (<) and greater than (>) signs to enclose a range of numbers.

ls file<100-300>
file100		file120		file122		file200
file222		file250

If we want all of those results, but we specifically do not want file200, we can tell zsh to not select it with the tilde (~) character.

ls file<100-300>~file200
file100		file120		file122		file222
file250

Let’s do some more complex selections. We can qualify our results further by providing a selector within a set of parentheses. If we want to select everything that is a regular file (not a directory, a link, etc), we can use something like this.

ls *(.)
anotherfile		file100		file200			file80
file.css		separate	completelydifferent	file120
file222			file800		file.html		somefile
different		file122		file250			file808
file.o			thisotherfile	file1			file2
file3			file.c		file.txt

Notice how none of the directories that we created are listed. If instead, we only want directories, we can use this:

ls *(/)
directa:

directb:

directc:

directd:

Next, let’s select the 5 newest files in our test directory. We can do that by using the “o” qualifier in parentheses. This selects our sorting method. We’re going to follow the “o” with an “m” which means we’re sorting by modification time. Finally, we’ll provide a range in brackets that tells zsh how many results we want.

ls *(.om[1,5])
completelydifferent	different	file.css	file.html	separate

Miscellaneous

Here are some extra ideas to try out.

We can configure zsh to automatically use specific programs to open a file based on the file extension. We will accomplish this using “suffix aliases”. These can be added to our .zshrc so that if we simply type the name of a file and hit return, it will be opened with the correct program.

Let’s use “less” to open any files with an extension of .view and use “nano” to open any files that end in .edit. Add this line to the bottom of your .zshrc.

nano ~/.zshrc
alias -s view=less
alias -s edit=nano
source ~/.zshrc

Now, let’s create two files to test this on.

touch test.view test.edit

If we type:

test.view

The file will open in “less” when we hit enter. However, if we type:

test.edit

The file will open in nano, as expected.

Another nice thing that zsh provides for us is completion hinting. The zsh gives us great visual indicators when it comes to entering multi-lined input. Let’s give it a try. If we type something like this and hit “enter”:

print “this is a line

We’ll be presented with a prompt like this:

dquote>

We can complete this line by ending our input with the second set of double quotes.

 dquote> that goes onto the next line”

As you can see, it tells us what kind of input needs to be closed to complete the statement. We can see a similar thing happen if we try to type a simple shell script into the prompt.

if [[ -o interactive ]]; then
then> print yes
then> else
else> print no
else> fi

As you can see, it gives us the same kind of hinting. This can be very helpful if you have long input statements that span multiple lines.

Setting zsh as the default shell

If you decide that zsh on a cloud server is suitable for your daily shell needs, you can make it the default shell for your user. This way, every time you log in, your preferences will be loaded and a zsh session will be spawned. You will no longer have to type “zsh” to get to zsh in your VPS.

chsh -s $(which zsh)

Next time you log in, you’ll be presented with a zsh prompt.

By Justin Ellingwood

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Switch out the apostrophes:

zstyle ‘:completion:*’ menu select # nope
zstyle ':completion:*' menu select # yep

Thanks

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel