LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Blogs > Debian, This That and the Other
User Name
Password

Notices

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:
Code:
exec zsh
Once you discover that you've been working for the dark side...
Quote:
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:
Code:
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.

Code:
# To reconfigure run :: zsh-newuser-install :: But that may erase your edits.
HISTFILE=~/.histfile
HISTSIZE=5000
SAVEHIST=5000

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
unlimit
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`"
fi

# 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
fi

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

PATH="/usr/local/bin:/usr/bin:/bin:/usr/games"
PATH="${PATH}:/opt/bin"
export PATH
MANWIDTH=80; export MANWIDTH
Modify. Make it your own.

There are only a few differences with my root user's version:
Code:
9:23|Wed 8# diff /root/.zshrc /home/cosmo/.zshrc
6c6
< zstyle :compinstall filename '/root/.zshrc'
---
> zstyle :compinstall filename '/home/cosmo/.zshrc'
109c109
< 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:

Code:
# 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.
Code:
# /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
fi
And finally, /etc/zsh/zshrc.
Code:
# /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

READNULLCMD=${PAGER:-/usr/bin/pager}

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
fi

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 5923 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 02:08 AM.

Main Menu
Advertisement

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