Wednesday, December 17, 2008

Color Completion Using Zsh Modules On Linux Or Unix

Hey There,

How's this for a long-overdue follow up. If my powers of site searching don't fail me, we haven't touched on the Z Shell (zsh) since our post, from July 2008 about multiple stream output in zsh which links back to another post on zsh's extended globbing functionality. This post is more of a follow up to that one than the other, but who's counting? ;)

Here's another cool thing you can do with zsh that involves color. Personally, I'm happy with a two-color scheme that makes my eyes sizzle (like blue on red - or red on blue - both of which look somewhat 3 dimensional), but you have to make room for color in your life. It's not all black and white, even though it seems that way most of the time ;)

In order to make use of this functionality, you'll have to use the modular functionality of zsh. Today, we'll be looking at color completion, but this isn't to say that this is the limit of what you can do with the shell. Just the limit of what I can manage to type today... Doing color completion in zsh requires two modules: complist and colors. The first requires you to set the extendedglob option to true and the second isn't really necessary, unless you don't want to remember what number equals what color in the ansi color scheme. Below, some quick examples of how you can set this up at the command line (or put in your .zshrc if you find you like it and dont' want to hassle with this constantly). Note that both options require that you load up additional modules within zsh:

host # setopt extendedglob
host # zmodload -a colors
host # zmodload -a autocomplete
host # zmodload -a complist


The -a option isn't absolutely necessary, but keeps you from having to remember to type:

zmodload zsh/complist

and

autoload colors

which, as shown, require two different forms to enter. Since they may as well both be auto-loaded, zmodload's -a option makes sense (especially since it stands for autoload... kind of).

Here are a few handy aliases for you to use that can make your zsh shell experience more enjoyable (if color does that for you ;). Of course, we didn't make all these up ourselves. Most are standard and can be found in the manpage for zshmodules. If you forget this, since zsh has 500 separate sub-manpages (j.k. ;), you can always find the name for this manpage by just invoking:

host # man zsh

and you'll see the reference to the zshmodules manpage in there.

You'll note the use of "zstyle" (which is explained more-than-fully in the same zshmodules manpage) to set up all these shell color aliases (not all of these will work on all systems) and is not restricted to the use of list-colors and the zsh completion feature. There are a lot more ways, built in to zsh, to make use of color in the shell:

zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors 'reply=( "=(#b)(*$VAR)(?)*=00=$color[green]=$color[bg-green]" )'
zstyle ':completion:*:*:*:*:hosts' list-colors '=*=30;41'
zstyle ':completion:*:*:*:*:users' list-colors '=*=$color[green]=$color[red]'
zstyle ':completion:*' list-colors ''
<-- For when you get sick and tired of all the colors and just want them to go away!

Hope you enjoy these (if you use zsh - we're not trying to get you away from ksh or bash if you like 'em like most of us do) and have fun using colorful language the next time you code ;)

Cheers,

, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.