By default, the ZSH auto completion is very rough. This seems very weird when you think of how powerful ZSH's expansion system is where you can expand any expression with the TAB key. But when you hit TAB to complete something, the only thing you can do is to toggle through a very basic list of files or commands.
But in fact, the ZSH completion system is very powerful. And when I say that, I mean very powerful. ZSH has a completely programmable completion system. For a long time, this has been a killer feature of ZSH. It still is, but other shells such as Bash have now implemented this as well. But in some areas, the ZSH completion system might still be the better one.
To enable the advanced completion system, you need to load the function
autoload -U compinit && compinit
That doesn't seem to change much, but when you now try the TAB key for some commands, you'll notice that they have a pretty intelligent auto completion now. One example is the
kill command. It expects the PID of the process which you want to send a signal to. When you hit
kill<TAB> you'll see a list of processes with their PIDs from which you can choose one. With TAB you can cycle through them. But you can also write something like
kill someprogramname<TAB> and it'll show you a list of all processes for this program with their PIDs through which you can again skip with the TAB key.
ZSH already comes with completion packages for all basic commands. But if you want even more completion, e.g. for some distribution-specific commands, you might install the corresponding packages from your package manager (usually called something like
That's already very exciting, but there's even more. Currently you only have a list and you have to skip through all entries to come to the last one. What if there would be some kind of a graphical menu? Actually, there is. You only have to activate it:
zstyle ':completion:*' menu select
When you now hit TAB to invoke the completion, you'll see a list pretty much like the old one, but when you hit TAB again, the first entry gets selected. You can now toggle through all of the entries with TAB just like before, but you can also use the arrow keys. That is pretty handy if you have a very long list. Long lists are split into columns and with the left and right arrow keys you can switch between these columns without having to go through all entries of the current column first.
You can also configure that the menu selection only comes up when there is more than a certain number of entries. The following shows the selection menu only when there are 20 ore more entries. Otherwise the conventional TAB completion is used:
zstyle ':completion:*' menu select=20
The same behavior applies to all other completion features, including normal command completion, parameter completion, file completion, variable completion etc.
Note: don't confuse menu selection with menu completion. menu completion is an older feature which only controls how ZSH behaves when a character is ambiguous. With
the next character of the first possible match is inserted automatically and you can get to the next by pressing TAB. If
menu_complete is not set, you'd have to type the next character explicitly or hit TAB again to cycle through the possible completions. menu selection, however, is a newer feature which is much more powerful. But of course can combine both.
Read more about menu selection and menu completion:
- zsh.sf.net Menu completion and menu selection
- zsh.sf.net Completion styles
- Arch Linux Wiki: Command Completion