Search:  
Gentoo Wiki

Zsh

Contents

About

Zsh is a shell designed for interactive use, although it is also a powerful scripting language. ZSH, also known as Z-Shell is a command line shell similar in function to the Korn Shell, KSH, Bash and assorted other modern command line interfaces. Zsh is a powerful shell with a high amount of configurability.

Install

To install the zsh, run: emerge -av zsh. If you need autocompletion, you also need to emerge zsh-completion (emerge -av zsh-completion).

Configure

The default use flags should be fine for most of users. To use it run:

/bin/zsh

If you want to set it as your default shell, then do

chsh -s /bin/zsh

or as root edit /etc/passwd and change the shell there.

Configuration

Tab completion

To enable tab completion (Portage completions and Gentoo prompt), you need to add the following to your ~/.zshrc

File: ~/.zshrc
  autoload -U compinit promptinit
  compinit
  promptinit; prompt gentoo

It's also recommended to cache the completions. To achieve this, add

 zstyle ':completion::complete:*' use-cache 1

to your ~/.zshrc.

Keys not working

If you find certain keys on your keyboard don't work, try using bindkey. First type bindkey then type <ctrl>-v then the key that you want to bind. Then add the function you want it to bind to ( you can use TAB completion for that!! ).

For example: on my macbook, the delete key is something like this:

bindkey -v '\e[3~' delete-char
            ^      ^-- Here i typed del<TAB> and got a list of possible functions.
            '-- Here I pressed <ctrl>-v-<delete>

I've put this in my ~/.zshrc to have my delete key work. This kind of key binding is incredibly powerfull and zsh has bindings set to make the shell behave as either emacs or vim! Check out their website documentation under keymaps.

When you now start zsh with the command zsh you should be able to do autocompletion with emerge like with dirs before!

Prompt Configuration

Don't like the default Gentoo prompt? No problem! You can make your prompt look like almost anything you want. All that is required is to set the $PS1 variable in your ~/.zshrc. Here are some examples:

# this... in ~/.zshrc
export PS1='(%n @ %m) %#'
# looks like this prompt
(user @ mycomputer) %
export PS1='%n@%m [ %~ ] %#'
user@mycomputer [ /current/working/dir ] %

You can also add colors!

BLACK="%{"$'\033[01;30m'"%}"
GREEN="%{"$'\033[01;32m'"%}"
RED="%{"$'\033[01;31m'"%}"
YELLOW="%{"$'\033[01;33m'"%}"
BLUE="%{"$'\033[01;34m'"%}"
BOLD="%{"$'\033[01;39m'"%}"
NORM="%{"$'\033[00m'"%}"
export PS1="${RED}%n${NORM} AT ${BLUE}%m ${GREEN}(%T)${NORM} %#"
#will look like this (with appropriate colors) %T is the current time
user AT mycomputer (22:33) %

Much more information can be found at the zsh website. Prompt expansion sequences, in particular, are here: http://zsh.sourceforge.net/Doc/Release/zsh_12.html#SEC40

Prompt on the Right side

You can also put a prompt at the RIGHT of the terminal by setting the $RPROMPT variable in the same way.

Shell History

Add something like this to ~/.zshrc to enable shell history.

# number of lines kept in history
export HISTSIZE=1000
# number of lines saved in the history after logout
export SAVEHIST=1000
# location of history
export HISTFILE=~/.zhistory
# append command to history file once executed
setopt inc_append_history

SSH hosts tab completion

To have zsh complete ssh hosts out of your .ssh/known_hosts add this

File: ~/.zshrc
local _myhosts
_myhosts=( ${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[0-9]*}%%\ *}%%,*} )
zstyle ':completion:*' hosts $_myhosts

Colorize STDERR

To color your stderr add the following lines to zshrc

File: ~/.zshrc
exec 2>>(while read line; do
  print '\e[91m'${(q)line}'\e[0m' > /dev/tty; print -n $'\0'; done &)

This will color the stderr line red. You will find that many programs are programmed to use stderr instead of stdout for outputting information, dhclient for instance. For the most part this works fine.

Directory in the title bar

To automatically display the current directory in the xterm title bar add the following lines to zshrc:

File: ~/.zshrc
chpwd() {
  [[ -t 1 ]] || return
  case $TERM in
    sun-cmd) print -Pn "\e]l%~\e\\"
      ;;
    *xterm*|rxvt|(dt|k|E)term) print -Pn "\e]2;%~\a"
      ;;
  esac
}

See also

Retrieved from "http://www.gentoo-wiki.info/Zsh"

Last modified: Wed, 10 Sep 2008 09:15:00 +0000 Hits: 8,046