Welcome to the most active Linux Forum on the web.
Go Back > Blogs > Debian, This That and the Other
User Name


Over several years of using Linux distros (Debian happens to be my fave) and BSDs for my primary computing, I've picked up the odd piece of useful info.
Rate this Entry

ZSH Shell Power

Posted 04-08-2009 at 10:56 PM by ofaring
Updated 04-08-2009 at 11:03 PM by ofaring
Tags 2009, e.g.

First of all, credit where credit is due. I shamelessly mixed and matched from my Debian files, the ZSH wiki, and various other bits.

ZSH is a shell with incredible autocompletion. Here's a good intro. If you install ZSH, you'll no doubt want to make a test burn. Copy/paste the following files. Make a few changes; then open a terminal or switch to a console (and login as you normally would). Type:
exec zsh
Once you discover that you've been working for the dark side...
ZSH wiki:
"After that, try: ls -<TAB><TAB> …use your cursor keys." Or type: "cd <TAB><TAB>" ...use arrow keys.
You will no doubt rush to type:
chsh -s <your user name> /bin/zsh
But first check /etc/shells to confirm the installation and location of ZSH.

What follows is my regular user's ~/.zshrc.

# To reconfigure run :: zsh-newuser-install :: But that may erase your edits.

zstyle :compinstall filename '/home/cosmo/.zshrc'

# Setup new style completion system. To see examples of the old style (compctl
# based) programmable completion, check Misc/compctl-examples in the zsh
# distribution.

zmodload zsh/complist
autoload -U compinit && compinit

# Set/unset  shell options
setopt 	 appendhistory nomatch
setopt   notify globdots pushdtohome cdablevars autolist
setopt   autocd recexact longlistjobs
setopt   autoresume histignoredups pushdsilent noclobber
setopt   autopushd pushdminus extendedglob rcquotes mailwarning
unsetopt bgnice autoparamslash correct
unsetopt autocd beep extendedglob notify correctall

# Autoload zsh modules when they are referenced
zmodload -a zsh/stat stat
zmodload -a zsh/zpty zpty
zmodload -a zsh/zprof zprof
zmodload -ap zsh/mapfile mapfile

bindkey -e

# Use hard limits, except for a smaller stack and no core dumps
limit stack 8192
limit core 0
limit -s

umask 022

# Shell functions
setenv() { typeset -x "${1}${1:+=}${(@)argv[2,$#]}" }  # csh compatibility
freload() { while (( $# )); do; unfunction $1; autoload -U $1; shift; done }

# automatically remove duplicates from these arrays
typeset -U path cdpath fpath manpath

# Set prompts
#PROMPT='%m%# '    # default prompt
PROMPT='%(t.Ding!.%D{%L:%M})|%w%# '
RPROMPT=' %~'     # prompt for right side of screen

### If you want zsh's completion to pick up new commands in $path automatically
### comment out the next line and un-comment the following 5 lines
#zstyle ':completion:::::' completer _complete _approximate
_force_rehash() {
  (( CURRENT == 1 )) && rehash
  return 1	# Because we didn't really complete anything
zstyle ':completion:::::' completer _force_rehash _complete _approximate
zstyle -e ':completion:*:approximate:*' max-errors 'reply=( $(( ($#PREFIX + $#SUFFIX) / 3 )) )'
zstyle ':completion:*:descriptions' format "- %d -"
zstyle ':completion:*:corrections' format "- %d - (errors %e})"
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
zstyle ':completion:*' group-name ''
zstyle ':completion:*:manuals' separate-sections true
zstyle ':completion:*:manuals.(^1*)' insert-sections true
zstyle ':completion:*' menu select
zstyle ':completion:*' verbose yes
# match uppercase from lowercase
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
# command for process lists, the local web server details and host completion
zstyle ':completion:*:processes' command 'ps -o pid,s,nice,stime,args'
#zstyle ':completion:*:urls' local 'www' '/var/www/htdocs' 'public_html'
zstyle '*' hosts $hosts

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"

# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
    eval "`dircolors -b`"

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f /etc/zsh/zaliases ]; then
        . /etc/zsh/zaliases

export CFLAGS='-s -pipe -O1 -march=i686 -fomit-frame-pointer'
export MAKEOPTS='-j2'

export PATH
Modify. Make it your own.

There are only a few differences with my root user's version:
9:23|Wed 8# diff /root/.zshrc /home/cosmo/.zshrc
< zstyle :compinstall filename '/root/.zshrc'
> zstyle :compinstall filename '/home/cosmo/.zshrc'
< PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
> PATH="/usr/local/bin:/usr/bin:/bin:/usr/games"
This is all well and good, but it's a bit incomplete without the /etc/zsh/zaliases file:

# Misc
alias mv='nocorrect mv'       # no spelling correction on mv
alias cp='nocorrect cp'       # no spelling correction on cp
alias mkdir='nocorrect mkdir' # no spelling correction on mkdir
alias j=jobs
alias d='dirs -v'
alias h=history
#alias grep=egrep
alias less='less -M'

# Navigation
alias ls='ls --color=auto'
alias dir='ls --format-vertical'
alias vdir='ls --format-long'
alias ll='ls -lah'
# List only file beginning with "."
alias la='ls -ad .*'
alias lsa='ls -ld .*'
alias l='ls -l'
# List only directories and symbolic links that point to directories
alias lsd='ls -d *(-/DN)' # Run this by itself.

# Hardware
alias acpi='acpi -V; sudo hddtemp /dev/hda'

# Management
alias pkg='dpkg -l | grep -i'
alias duu='du -sh *'

# Multimedia
# The following works in Lenny, but not the Etch version. REBUILD!
#alias mpg123='mpg123 --cpu MMX -C -b 1024 --smooth -o alsa --title'
alias mpg123='mpg123 -C -b 1024 --title'
alias mplayer='mplayer -af equalizer=6:9:6:-2:0:-4:-6:-7:5:2 -cache 1024'
No doubt you will modify this, but you get the picture. Suffice to say that I not only enjoy using the command line, I barely touch a mouse while doing my work.

When working on consoles I find it annoying to log out and have the work still staring me down. Hence /etc/zsh/zlogout.
# /etc/zsh/zlogout: system-wide .zlogout file for zsh(1).
# when leaving the console clear the screen to increase privacy

if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
And finally, /etc/zsh/zshrc.
# /etc/zsh/zshrc: system-wide .zshrc file for zsh(1).
# This file is sourced only for interactive shells. It
# should contain commands to set up aliases, functions,
# options, key bindings, etc.
# Global Order: zshenv, zprofile, zshrc, zlogin


if [[ "$TERM" != emacs ]]; then
[[ -z "$terminfo[kdch1]" ]] || bindkey -M emacs "$terminfo[kdch1]" delete-char
[[ -z "$terminfo[khome]" ]] || bindkey -M emacs "$terminfo[khome]" beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M emacs "$terminfo[kend]" end-of-line
[[ -z "$terminfo[kich1]" ]] || bindkey -M emacs "$terminfo[kich1]" overwrite-mode
[[ -z "$terminfo[kdch1]" ]] || bindkey -M vicmd "$terminfo[kdch1]" vi-delete-char
[[ -z "$terminfo[khome]" ]] || bindkey -M vicmd "$terminfo[khome]" vi-beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M vicmd "$terminfo[kend]" vi-end-of-line
[[ -z "$terminfo[kich1]" ]] || bindkey -M vicmd "$terminfo[kich1]" overwrite-mode

[[ -z "$terminfo[cuu1]" ]] || bindkey -M viins "$terminfo[cuu1]" vi-up-line-or-history
[[ -z "$terminfo[cuf1]" ]] || bindkey -M viins "$terminfo[cuf1]" vi-forward-char
[[ -z "$terminfo[kcuu1]" ]] || bindkey -M viins "$terminfo[kcuu1]" vi-up-line-or-history
[[ -z "$terminfo[kcud1]" ]] || bindkey -M viins "$terminfo[kcud1]" vi-down-line-or-history
[[ -z "$terminfo[kcuf1]" ]] || bindkey -M viins "$terminfo[kcuf1]" vi-forward-char
[[ -z "$terminfo[kcub1]" ]] || bindkey -M viins "$terminfo[kcub1]" vi-backward-char

# ncurses fogyatekos
[[ "$terminfo[kcuu1]" == ""* ]] && bindkey -M viins "${terminfo[kcuu1]/O/[}" vi-up-line-or-history
[[ "$terminfo[kcud1]" == ""* ]] && bindkey -M viins "${terminfo[kcud1]/O/[}" vi-down-line-or-history
[[ "$terminfo[kcuf1]" == ""* ]] && bindkey -M viins "${terminfo[kcuf1]/O/[}" vi-forward-char
[[ "$terminfo[kcub1]" == ""* ]] && bindkey -M viins "${terminfo[kcub1]/O/[}" vi-backward-char
[[ "$terminfo[khome]" == ""* ]] && bindkey -M viins "${terminfo[khome]/O/[}" beginning-of-line
[[ "$terminfo[kend]" == ""* ]] && bindkey -M viins "${terminfo[kend]/O/[}" end-of-line
[[ "$terminfo[khome]" == ""* ]] && bindkey -M emacs "${terminfo[khome]/O/[}" beginning-of-line
[[ "$terminfo[kend]" == ""* ]] && bindkey -M emacs "${terminfo[kend]/O/[}" end-of-line

zstyle ':completion:*:sudo:*' command-path /usr/local/sbin /usr/local/bin \
                             /usr/sbin /usr/bin /sbin /bin /usr/X11R6/bin

unalias run-help
autoload run-help
There are several other files in /etc/zsh/ which I don't use: zlogin zprofile zshenv
If you are using Debian (and likely, Ubuntu etc.), you will only need to add "zaliases" and modify "zlogout".

You do have another option for the setup of ZSH. Make sure that your user's home directory - regular user, not root - does not have a .zshrc file, and start the shell. It will toss you into a config screen. Also, the first time you run ZSH, don't be frightened by an initial lag. It creates a .zcompdump file and will be nicer the next time.

Hope this is clear. If you choose to use ZSH, enjoy!
Posted in Examples
Views 7160 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 01:37 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration