ZSH: The Z Shell

The order of files we’re using here

/etc/zsh/zhsenv
$ZDOTDIR/.zshenv
/etc/zsh/zshrc
$ZDOTDIR/.zshrc

The zshenv to rule them all

Let’s create our /etc/zsh/zhsenv file. This is where zsh starts, it’s the first file that’s read. Here we will define our XDG (Cross-desktop Group) compatibility directories and define where we’ll put the rest of our ZSH configs.

cat << EOF > /etc/zsh/zshenv
export XDG_CACHE_HOME=${XDG_CACHE_HOME:=~/.cache}
export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:=~/.config}
export XDG_DATA_HOME=${XDG_DATA_HOME:=~/.local/share}
export XDG_STATE_HOME=${XDG_STATE_HOME:=~/.local/state}

export ZDOTDIR=${ZDOTDIR:=$XDG_CONFIG_HOME/zsh}
EOF

The global zshrc, things to execute for everyone

cat << EOF > /etc/zsh/zshrc
autoload colors   && colors
autoload compinit && compinit

setopt ALWAYS_TO_END
setopt AUTO_MENU
setopt CORRECT
setopt EXTENDED_HISTORY
setopt GLOBDOTS
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_SPACE
setopt INC_APPEND_HISTORY_TIME
setopt INTERACTIVE_COMMENTS
setopt NOBEEP

alias grep="grep --color=auto --exclude-dir={.git,.svn}"
alias ip='ip -color=auto'
alias ls="ls -hpv --color=auto --group-directories-first"

bindkey -e

zle_highlight=('paste:none')

# create a zkbd compatible hash;
typeset -g -A key

key[Home]="${terminfo[khome]}"
key[End]="${terminfo[kend]}"
key[Insert]="${terminfo[kich1]}"
key[Backspace]="${terminfo[kbs]}"
key[Delete]="${terminfo[kdch1]}"
key[Up]="${terminfo[kcuu1]}"
key[Down]="${terminfo[kcud1]}"
key[Left]="${terminfo[kcub1]}"
key[Right]="${terminfo[kcuf1]}"
key[PageUp]="${terminfo[kpp]}"
key[PageDown]="${terminfo[knp]}"
key[Shift-Tab]="${terminfo[kcbt]}"
key[Control-Left]="${terminfo[kLFT5]}"
key[Control-Right]="${terminfo[kRIT5]}"
key[Control-Backspace]="${terminfo[cub1]}"

# 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[Backspace]}"         ]] && bindkey -- "${key[Backspace]}"         backward-delete-char
[[ -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
[[ -n "${key[Shift-Tab]}"         ]] && bindkey -- "${key[Shift-Tab]}"         reverse-menu-complete
[[ -n "${key[Control-Left]}"      ]] && bindkey -- "${key[Control-Left]}"      backward-word
[[ -n "${key[Control-Right]}"     ]] && bindkey -- "${key[Control-Right]}"     forward-word
[[ -n "${key[Control-Backspace]}" ]] && bindkey -- "${key[Control-Backspace]}" backward-kill-word

# 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
        autoload -Uz add-zle-hook-widget
        function zle_application_mode_start { echoti smkx }
        function zle_application_mode_stop { echoti rmkx }
        add-zle-hook-widget -Uz zle-line-init zle_application_mode_start
        add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop
fi

# Set a default prompt
PROMPT='[%n@%m] %~ %# '

if [ -d $HOME/.local/bin ]; then                # If $HOME/.local/bin exists
  if [[ ! $PATH =~ $HOME/.local/bin ]]; then    # And if it's not already in PATH
    export PATH=$HOME/.local/bin:$PATH          # then add to PATH
  fi
fi
EOF