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.
You're reading from Learning Shell Scripting with Zsh
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...
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...
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...
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 zstylesA few tips for creating and extending your our own completion...