Learn how to write your first ZSH Auto-completion function
In this post, I’ll guide you in writing few, very basic Zsh autocompletion functions. Everything will be used at its minimal level.
I’m assuming that you’ve a basic knowledge of bash.
You need activate completion system first if you’re not using something like
oh-my-zsh
. Just run(or add to.zshrc
):
autoload -U compinit compinit
Let’s say our program is called hello
.
Here’s what will happen:
- You write a completion function. It usually starts with
_
(underscore) :
- Bind your function to a command
- Whenever you press <Tab> after
hello
,_hello
will be called.
Whenever you want to throw out possible completions, you’ll use one of the following utility functions(in this post):
compadd
To get:
You’ll write:
compadd cmd1 cmd2 cmd3
_describe
To get:
You’ll write:
Note: In both of above commands, we didn’t consider which argument no. it is, means even hello cmd1 <Tab>
will give same output. Next command will solve this problem.
_arguments
Now this is a powerful one. You can control multiple arguments.
By multiple arguments I mean
hello arg1 arg2
nothello arg1|arg2
Here’s the basic syntax: _arguments <something> <something> ...
where <something>
can either be:
'-o[description]'
for an option'<argument number>:<message>:<what to do>'
for an argument
First one is self-explanatory, whenever called it’ll give
For the second one:
<argument number>
is self-explanatory- I’ll leave
message
empty to demonstrate a minimal example. <what to do>
can be quite a few things, we’ll discuss only two:
- List of arguments possible at given
argument number
. For example, if two arguments(world
anduniverse
) are possible at argument one(hello world|universe
), we can write:
2. Set variable state
to an identifier. For example, if we want to call another function at argument no. 2, we can write:
That might be confusing, lets sum up _arguments
by an example:
Lets say, our program has possible args like:
Its completion function can be:
What Next?
I hope you were able to successfully write your first autocompletion function. I recommed to visit:
Originally published at mrigank11.github.io.