One of zsh's most convenient features is its flexible completion system. These are just a few examples of tab-completion that I use regularly. If you're a bash user, don't fear! Check your distribution's repositories for a bash-completion
package, which provides much of the functionality shown below.
pacman
For me, the most challenging part of installing video drivers on Linux is spelling "nouveau" correctly on my first try. Fortunately, zsh can help.
> pacman -S xf86-video-n<TAB>
xf86-video-neomagic xf86-video-nouveau xf86-video-nv
Beautiful! zsh can work with your favorite package manager to tab-complete the names of available packages.
tar
Using tar can be confounding at times, but zsh has your back.
> tar <TAB>
A -- append to an archive
c -- create a new archive
f -- specify archive file or device
t -- list archive contents
u -- update archive
v -- verbose output
x -- extract files from an archive
Pretty nifty—zsh can show us a summary of tar's options, in case you ever forget that you need to use the "t" flag to list the contents of an archive.
Even more interesting is zsh's ability to provide filetype-aware completions. Suppose you want to extract a compressed file that's located somewhere in your cluttered downloads directory:
> ls
clutter.pdf gohufont-2.0.tar.gz linux-4.6-rc3.tar.xz refuse.flac termboy.tar.xz
garbage.mkv junk.png lua-5.3.2.tar.gz rubbish.iso waste.py
> tar xf <TAB>
gohufont-2.0.tar.gz linux-4.6-rc3.tar.xz lua-5.3.2.tar.gz termboy.tar.xz
Neat, it only lists files that tar knows how to handle. But wait, there's more:
> tar xJf <TAB>
linux-4.6-rc3.tar.xz termboy.tar.xz
Cool! This time zsh noticed that we passed the "J" flag to tar, so it only shows xz archives.
mpv
Have you ever watched a movie where you need to turn up the sound to hear what the characters are saying, only to have your eardrums blown out the next time there's a sudden spike in volume? My favorite media player, mpv, has an option to fix that. If only I could remember the name...
> mpv --af=<TAB>
channels -- Insert or remove channels
delay -- Delay audio filter
drc -- Dynamic range compression filter
equalizer -- Equalizer audio filter
format -- Force audio format
lavcac3enc -- runtime encode to ac3 using libavcodec
lavfi -- libavfilter bridge
lavrresample -- Sample frequency conversion using libavresample
pan -- Panning audio filter
rubberband -- Pitch conversion with librubberband
scaletempo -- Scale audio tempo while maintaining pitch
volume -- Volume control audio filter
zsh to the rescue! In this example zsh lists the arguments available to the audio filter option (--af). Ah, dynamic range compression, that's what I was looking for!
pactl
Sometimes I want to reduce the volume of one application without changing the volume of another—to listen to music while playing a game, for example. PulseAudio provides the pactl
command to modify the volume of a running program, but it can be a little awkward to use since you need to know the numeric index that identifies each application to the PulseAudio server. Not anymore:
> pactl set-sink-input-volume <TAB>
3 -- "ALSA plug-in [mednafen]"
4 -- "mpv Media Player"
zsh shows a nice little description of each program and its ID. That set-sink-input-volume
is a bit of a mouthful, but zsh can complete that as well. This one is especially handy if you don't run a full desktop environment that has a graphical volume mixer.
kill
Got an unruly process you need to take care of?
> kill <TAB>
8266 pts/0 00:00:00 zsh
8341 pts/0 00:00:00 vim
8343 pts/0 00:00:00 python
8366 pts/0 00:00:00 crawl-tiles
You guessed it, zsh can complete that too. Who needs pkill?