ZSH Gem #5: Menu selection

Posted by | Comments (3) | Trackback (1)

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 compinit first.

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 zsh-completion).

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

setopt menu_complete

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:



There have been 3 comments submitted yet. Add one as well!
Brian wrote on : (permalink)
I've found this useful for highlighting the matching part of the menu. autoload -U colors ; colors zstyle -e ':completion:*' list-colors 'thingy=${PREFIX##*/} reply=( "=(#b)($thingy)(?)*=00=$color[green]=$color[bg-green]" )' Which is a slight improvement over what's discussed "here":http://www.zsh.org/mla/users/2008/msg00703.html. This might be able to be improved to use standard ls colors with some work.
Janek Bevendorff
Janek Bevendorff wrote on : (permalink)
Yep, thanks. I have something similar here in my setup. But I'll show that in a later article. ;-)
hynt wrote on : (permalink)
a little script i made i'm newbie what u think? http://pastebin.com/2DwWdVRJ

Write a comment:

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

By submitting a comment, you agree to our privacy policy.

Design and Code Copyright © 2010-2024 Janek Bevendorff Content on this site is published under the terms of the GNU Free Documentation License (GFDL). You may redistribute content only in compliance with these terms.