Reader small image

You're reading from  Learning Shell Scripting with Zsh

Product typeBook
Published inJan 2014
Publisher
ISBN-139781783282937
Edition1st Edition
Tools
Right arrow
Author (1)
Gaston Festari
Gaston Festari
author image
Gaston Festari

Gastón Festari is a scripting language enthusiast with over five years of experience and a firm believer in free, open source software. Currently working as a developer for Globant, he likes to spread the word about zsh at different meetups and events when away from the keyboard.
Read more about Gaston Festari

Right arrow

Chapter 5. Completion

This is what most users switch to zsh for: completion. In this chapter, we'll meet one of the best features of zsh: compsys. Known as "the new" completion mechanism, this chapter focuses on its various functions and configuration. We will learn to tweak the completion behavior so that it's no longer restricted to filenames and bump it up using styles and our own functions. When we're done, you should be able to read most zsh scripts as well as tweak many of the existing functions.

Getting started with completion


Nobody really likes to type boring filenames, and that's what got completion started back in the day—type a few letters of a filename, press Tab, and the shell will do the rest for you. Zsh goes the extra mile though and actually allows you to complete almost anything. By default, the Tab key is bound to a completion command in zsh.

Like Bash, zsh defaults to filename completion. Unlike anything else, however, zsh can enable the completion for practically everything that dares to rear its head in the command line—paths, external and built-in commands, aliases, functions, and options; you name it. And even if you can't name it, you can program it, as we will learn shortly.

Originally, zsh used a built-in module with a special syntax in order to provide completion. Luckily for us, this was eventually replaced by an even simpler mechanism. We'll focus on the new completion system that is entirely based on shell functions.

Go ahead and pop open that .zshrc file of...

Command correction


Completion can also correct any misspelled commands that you might have typed. We'll use the following format for our style:

zstyle ':completion:*' completer _expand _complete _correct

And we'll test the autocorrect functionality with the following:

% prnti<Tab>
corrections (2 errors)
print   printf
original
prnti

Zstyle noticed that we misspelled print and is being quite verbose regarding this. Remember you can use the Tab key to cycle through the list of available options.

Alternatively, you could use the correct option if you want a more "hold me by the hand" approach. Specifically, this option will make zsh ask you for confirmation every time it suggests a correction:

% setopt correct
% prnti<Tab>
zsh: correct 'prnti' to 'print' [nyae]?

This peculiar nyae acronym stands for No, Yes, Abort, and Edit, and works in the following way:

  • n: This will force the shell to execute whatever you typed in the command line (prnti in this particular case).

  • y: This will execute...

Function definitions


Finally, we will turn our attention to compsys, zsh's completion system. This is one of the most complex parts of the shell for users and developers alike. Before we dive into compsys, however, we need to make a quick stop and meet an actual function in the wild.

Tip

As usual, you can learn more about compsys via the manpages. Of particular interest are man zshcompsys and man zshcompwid.

Here's what one of these looks like:

hi() {
print 'Hello, world'
}

Here, we have defined the hi function, which is how we'll call it again later when we need it. This will, in turn, print Hello, world every time we use it. So let's get to it, shall we?

Open your terminal emulator of choice, and type the following (one line at a time):

% hi() {
function> print 'Hello, World!'
function> }

Notice how zsh realized this was indeed a function we were trying to define and immediately used the continuation prompt (function>), allowing you to continue working on it? How nice of zsh to wait...

Summary


We are almost done with this adventure, and it seems you are now more ready than ever to start tackling major annoyances like your favorite program not having a set of completion definitions. Even better, you can tweak and improve the existing functionality, which otherwise would make your work really frustrating.

Besides writing your own functions, we also learned how to tweak the shell behavior and go a step above filename completion. With a bit of practice and further tweaking, you can now become a real speed demon of the command line. Best of all, it only takes a couple of Tab presses to get there.

Summing it up, here's what's covered in this chapter:

  • The types of completion available to zsh—zstyles and functions, which allow you to customize the behavior of the completion mechanism and extend its functionality

  • The different types of completers (particularly correct, approximate & ignore) and their role when defining zstyles

  • A few tips for creating and extending your our own completion...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Shell Scripting with Zsh
Published in: Jan 2014Publisher: ISBN-13: 9781783282937
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Gaston Festari

Gastón Festari is a scripting language enthusiast with over five years of experience and a firm believer in free, open source software. Currently working as a developer for Globant, he likes to spread the word about zsh at different meetups and events when away from the keyboard.
Read more about Gaston Festari