Zsh

From Gentoo Linux Wiki

(Redirected from ZSH)
Jump to: navigation, search


Contents

[edit] Summary

ZSH, also known as Z-Shell is a command line shell similar in function to the Korn Shell, KSH, and assorted other modern command line interfaces.

This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

[edit] Installation

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

[edit] Configuration

[edit] 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

[edit] My Keys Don't Work!

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!

[edit] 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

[edit] 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.

[edit] 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

[edit] 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

[edit] 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; 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.

[edit] 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
}

[edit] References

Retrieved from "http://gentoo-wiki.com/Zsh"
Personal tools