ZSH Gem #24: ZSH frameworks

Posted by | Comments (7) | Trackbacks (0)

I have shown you many things about ZSH throughout this series, but there is much more you can do with it than I could cover here. And of course there is also much more to configure, many more options I couldn't tell you about, many more tips and tricks, tweaks and optimizations.

Generally, it's a long way to go before you have your shell set up as you like. Especially ZSH needs a lot of configuration before it becomes very user-friendly. You can do all this configuration by hand or you can use a framework for that. Yes, there are frameworks for ZSH (and for Bash as well, in case you didn't know) and as a completion of this Advent series I'll show you two of them.

oh-my-zsh

Probably the most advanced and mighty ZSH framework is oh-my-zsh. oh-my-zsh provides lots of extra features, special plugins with additional functions and completion definitions for certain command line tools and many, many themes for modifying the appearance of your shell, particularly the prompt.

To use oh-my-zsh, download it to a directory of your choice (e.g. ~/.oh-my-zsh) and load it from within your .zshrc:

# Path to your oh-my-zsh installation (the framework won't work without this setting)
ZSH=$HOME/.oh-my-zsh

# Load oh-my-zsh
source $ZSH/oh-my-zsh.sh

That's basically it. You've successfully loaded the framework. But until now it doesn't do much except changing the default prompt to something a bit more meaningful and configuring some basic stuff such as enabling ls colors. But you can customize the framework. For example to disable ls colors again, write the following in your .zshrc before the line where the framework is loaded:

DISABLE_LS_COLORS="true"

To set another theme modify the parameter $ZSH_THEME:

ZSH_THEME="candy"

Now when you re-source your current shell instance you have the candy theme activated. For a full list of available themes have a look at the themes folder inside your oh-my-zsh folder. An even better overview can be found in the oh-my-zsh wiki on GitHub.

Next let's come to the plugins. By default, no plugins are loaded, but that can be changed. Similar to ZSH's $fpath array, oh-my-zsh has a special $plugins array which contains all the names of all the plugins to load when initializing the framework. For instance:

plugins=(git github perl svn)

for loading the plugins git, github, perl and svn which will provide extra functions and completion features for the corresponding applications. For instance, the git plugins adds many advanced completion features for working with Git repositories and the shell function current_branch as a shorthand for displaying the branch you're currently working on without any additional meta data or formatting stuff.

oh-my-zsh is pretty mighty and you can have a lot of fun with it. Just be aware, that even though ZSH provides an autoloading mechanism for functions, loading too many plugins can slow down your shell. It can be very annoying when you always have to wait 10 seconds after opening a new shell instance.

If you're not quite sure how to start with oh-my-zsh, have a look at the example .zshrc in the templates folder.

zshuery

zshuery is another framework for ZSH which is much smaller. It's more something like a micro-framework. The name is derived from jQuery and the idea behind it is to provide a simple and flexible yet fast framework. zshuery doesn't have lots of plugins. It's just one single file which provides some extra functionality and does the basic ZSH configuration for you. That's it. Compared to oh-my-zsh, zshuery is a stub but that's why I like this framework and prefer it over the more powerful oh-my-zsh. It's just what it wants to be: a simple framework which makes working on the shell easier without blowing you away with feature you probably never need. With some extra configuration in my .zshrc this framework is exactly what I need.

But first things first. Loading zshuery is pretty much straightforward. Just download the framework to an arbitrary folder and reference it in your .zshrc:

# Load zshuery
source ${HOME}/.zshuery/zshuery.sh

That loads the framework. Now to let zshuery set some default options, aliases and autocorrection for you, call the following functions:

load_defaults
load_aliases
load_correction

I told you that zshuery doesn't come with plugins and that is true. However, it provides an easy way to load additional completion functions from the zsh-completions Git repository. These are autocompletion functions which might not yet be in the official ZSH release. To include them, clone the repository (preferably to a directory somwhere inside your zshuery folder) and call the following zshuery function:

load_completion ${HOME}/.zshuery/completion

(provided the path to your copy of the zsh-completion repository is ~/.zshuery/completion)

And finally my Christmas gift for you…

zshuery is a great framework for pimping your Z Shell without overloading it. And because it's my favorite ZSH framework and because Christmas is just around the corner, I give you a commented version of my .zshrc in which I use this framework with some slight modifications:

# Load zshuery
source ${HOME}/.zshuery/zshuery.sh
load_defaults
load_aliases
load_correction

# Colorize ls output
alias ls="ls --color=auto"

# Redefine ls colors and load completion definitions
export LS_COLORS="no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:"
load_completion ${HOME}/.zshuery/completion

# Modify key bindings to make certain keys such as DEL, HOME, END, PAGE UP and PAGE DOWN work
eval "$(sed -n 's/^/bindkey /; s/: / /p' /etc/inputrc)" > /dev/null
bindkey "\e[5~" beginning-of-history    # Page Up
bindkey "\e[6~" end-of-history          # Page Down

# Colorize STDERR (enable if needed, might cause issues with password prompts or escape sequences)
#exec 2>>(while read line; do; print '\e[91m'${(q)line}'\e[0m' > /dev/tty; print -n $'\0'; done)

# Enable menu select
zstyle ':completion:*' menu select

# Enable tree view for kill completion
zstyle ':completion:*:*:kill:*:processes' command 'ps --forest -e -o pid,user,tty,cmd'

# Modify zshuery correction prompt
SPROMPT="Correct $fg[red]%R$reset_color to $fg[green]%r?$reset_color (Yes, No, Abort, Edit) "

# Load fancy prompt (works on Gentoo Linux only)
autoload -U promptinit
promptinit
prompt gentoo

# Non-Gentoo users might configure their prompt manually.
# The default Gentoo prompt from above would look like this:
#PROMPT="%B%{$fg[green]%}%n@%m%k%{$reset_color%} %B%{$fg[blue]%}%1~ %# %b%f%k%{$reset_color%}"

# Set options
setopt complete_in_word
setopt path_dirs

# Load modules
zmodload zsh/regex
zmodload zsh/pcre

# Update terminal CWD once and then on every CWD change
update_terminal_cwd
function chpwd() {
        update_terminal_cwd
}

# Extend $PATH
PATH="${HOME}/.local/bin:${HOME}/.local/opt/bin:${PATH}"

Use it for whatever purpose you want. Modify it, extended it, republish it, whatever comes to your mind. You can also use it together with oh-my-zsh or no framework at all if you like that better. Just alter the lines where the framework is loaded or used.

Trackbacks

No Trackbacks for this entry.

Comments

There have been 7 comments submitted yet. Add one as well!
Tob
Tob wrote on : (permalink)
Thank you very much for this interesting blog series on zsh! Now that it's complete it can serve as a nice feature-by-feature tutorial for zsh, and will be sure to convert lots of users of inferior other shells (It has worked for me :-) Best wishes, Tob
Marcin Kulik
Marcin Kulik wrote on : (permalink)
I see "Colorize STDERR" trick in your .zshrc. This method is simple but breaks in too many cases. You may try stderred ("https://github.com/sickill/stderred":https://github.com/sickill/stderred). I'm using it on daily basis for ~2 months now and everything works flawlessly. And big kudos for whole series, it was pleasure to read and I learned few tricks. Thanks!
Janek Bevendorff
Janek Bevendorff wrote on : (permalink)
Thanks for the link. Personally, I don't use colorized STDERR (for the reason mentioned by you) and therefore I commented it out. But for some people it might indeed be interesting.
Eric Eide
Eric Eide wrote on : (permalink)
Thank you very much for an informative and entertaining Advent calendar series! Although I am an old dog, I definitely learned a few new tricks.
orip
orip wrote on : (permalink)
Good stuff! I recommend trying Sorin Ionescu's fork of oh-my-zsh instead of Robby Russel's. There are a great many issues fixed and it's much more zsh-y. https://github.com/sorin-ionescu/oh-my-zsh
David
David wrote on : (permalink)
h2. *%{color:green}Thanks!%* I'm just starting the move from *bash* to *zsh*. This helps a great deal. I'll keep checking back for _Hints_ and tidbits. -- David

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.