Extended Brain Storage

Arch Linux: Z Shell (ZSH)

Posted on March 5, 2013

A brief Z shell (zsh) howto…


Why zsh instead of the old bash? Some of the things zsh can do:

Installation and Fine-tuning Generally

Works for Arch Linux:

$ pacman -S zsh

as well as for FreeBSD:

$ pkg install zsh

Highlighting can be installed from GitHub (git needs to be installed of course, skipping that part):

$ mkdir -p /usr/share/zsh/plugins/
$ cd /usr/share/zsh/plugins/
$ git clone git://github.com/zsh-users/zsh-syntax-highlighting.git

or in FreeBSD:

$ mkdir -p /usr/local/share/zsh/plugins/
$ cd /usr/local/share/zsh/plugins/
$ git clone git://github.com/zsh-users/zsh-syntax-highlighting.git

ZSH configuration file needs to be created in each user’s home directory (~/.zshrc):

# this file is: ~/.zshrc
# To prevent history from recording duplicated entries
setopt hist_ignore_all_dups
# A useful trick to prevent particular entries from being recorded into a history by preceding them with at least one space.
setopt hist_ignore_space

bindkey -e
bindkey ";5C" emacs-forward-word
bindkey ";5D" emacs-backward-word

# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
zstyle :compinstall filename '~/.zshrc'

# Default completion style is quite plain and ugly. If you want to improve its appearance, enter the following commands: 
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' completer _expand _complete _expand_alias
zstyle ':completion:*' menu select=1
zstyle ':completion:*' original true
zstyle ':completion:*' remote-access false
zstyle ':completion:*' use-perl true
zstyle ':completion:*' verbose true
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' squeeze-slashes true
autoload -Uz compinit
compinit -D
zmodload zsh/complist

# Available prompts are listed by running the command:
# prompt -l
# To preview all available themes, use this command: 
# prompt -p
autoload -Uz promptinit
prompt redhat

# It is also a good idea to enable the auto-correction of the commands typed: 
setopt correctall

# create a zkbd compatible hash;
# to add other keys to this hash, see: man 5 terminfo
typeset -A key


# setup key accordingly
[[ -n "${key[Home]}"     ]]  && bindkey  "${key[Home]}"     beginning-of-line
[[ -n "${key[End]}"      ]]  && bindkey  "${key[End]}"      end-of-line
[[ -n "${key[Insert]}"   ]]  && bindkey  "${key[Insert]}"   overwrite-mode
[[ -n "${key[Delete]}"   ]]  && bindkey  "${key[Delete]}"   delete-char
[[ -n "${key[Up]}"       ]]  && bindkey  "${key[Up]}"       up-line-or-history
[[ -n "${key[Down]}"     ]]  && bindkey  "${key[Down]}"     down-line-or-history
[[ -n "${key[Left]}"     ]]  && bindkey  "${key[Left]}"     backward-char
[[ -n "${key[Right]}"    ]]  && bindkey  "${key[Right]}"    forward-char
[[ -n "${key[PageUp]}"   ]]  && bindkey  "${key[PageUp]}"   beginning-of-buffer-or-history
[[ -n "${key[PageDown]}" ]]  && bindkey  "${key[PageDown]}" end-of-buffer-or-history

# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
    function zle-line-init () {
        printf '%s' "${terminfo[smkx]}"
    function zle-line-finish () {
        printf '%s' "${terminfo[rmkx]}"
    zle -N zle-line-init
    zle -N zle-line-finish

source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
#source /usr/local/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

autoload -U colors && colors

[[ $- != *i* ]] && return

alias ls='ls --color=auto'
#alias ls='ls -G'
#alias ls='/usr/local/bin/colorls -G'

PROMPT="%{$fg[white]%}[%{$fg[green]%}%n%{$fg[white]%}@%{$fg[white]%}%m %{$fg[white]%}%1~]$ "
#PROMPT="%{$fg[white]%}[%{$fg[red]%}%n%{$fg[white]%}@%{$fg[white]%}%m %{$fg[white]%}%1~]$ "
#PROMPT="%{$fg[white]%}%{$fg[green]%}%n%{$fg[white]%}@%{$fg[white]%}%m:%{$fg[white]%}%1~ %% "
#PROMPT="%{$fg[white]%}%{$fg[red]%}%n%{$fg[white]%}@%{$fg[white]%}%m:%{$fg[white]%}%1~ %# "

# metasploit
# where the quiet option will #Disable the ASCII banner on startup, and the -x command runs the given command right after startup. 
alias msfconsole="msfconsole --quiet -x \"db_connect ${USER}@msf\""

# End of lines added by compinstall


The setup can be tested right from bash by running:

$ zsh

Or changing it directly in /etc/passwd file (Linux) or by running (FreeBSD):

$ chsh -s /usr/local/bin/zsh

Installation in Gentoo Linux

First of all, update the USE flag in /etc/portage/make.conf with zsh-completion.

Second, install the zsh and zsh-completion packages:

$ emerge -av app-shells/zsh app-shells/zsh-completion

Follow, the following instructions (which appeared after the previous installation on screen as well).

 * If you want to enable Portage completions and Gentoo prompt,
 * emerge app-shells/zsh-completion and add
 * 	autoload -U compinit promptinit
 * 	compinit
 * 	promptinit; prompt gentoo
 * to your ~/.zshrc
 * Also, if you want to enable cache for the completions, add
 * 	zstyle ':completion::complete:*' use-cache 1
 * to your ~/.zshrc

Or, you can just edit the configuration file ~/.zshrc:

export HISTSIZE=1000
export SAVEHIST=1000
export HISTFILE=~/.zhistory
export EDITOR="vim"
setopt inc_append_history
setopt correctall #autocorrect misspelled commands
setopt hist_ignore_all_dups #ignore dups in history
setopt autocd # % /etc = cd /etc

bindkey "e[H" beginning-of-line #home (xorg)
bindkey "e[1~" beginning-of-line #home (console)
bindkey "e[F" end-of-line #end (xorg)
bindkey "e[4~" end-of-line #end(console)
bindkey "e[3~" delete-char #delete key

#fpath=($fpath ~/.zsh/functions)
#autoload -U ~/.zsh/functions/*(:t)

#Used for git stuff
typeset -ga preexec_functions
typeset -ga precmd_functions
typeset -ga chpwd_functions

# Append git functions needed for prompt.

alias ls='ls --color=auto'

#Load zsh tab completion
autoload -U compinit

autoload -U colors

#Use the Gentoo prompt style
autoload -U promptinit
prompt gentoo
#PROMPT=$'%B%F{green}%n@%m%k %B%F{blue}%1~ $(prompt_git_info)%B%F{blue}%# %b%f%k'


Tags: #Arch Linux #Gentoo Linux #zsh #shell #FreeBSD #OpenBSD

⏴ Previous Post Next Post ⏵