Table of Contents
The z-shell is probably one of the best korn-shell based alternatives to bash.
Aliases
Zsh knows three different types of aliases:
Aliases for Commands
This is the regular alias, acting just like the one of bash:
alias less=/usr/share/vim/vim64/macros/less.sh
call without parameters to display defined aliases:
alias
Global Aliases
Global aliases act just like the ones described before, but with one exception: they don't have to be given at the beginning of the line. So e.g.:
alias -g G='|grep'
can be used like this:
dmesg G eth
and will be expanded to the following:
dmesg |grep eth
Suffix Aliases
This is quite crazy stuff: when entering just an existing filename as “command”, zsh can expand it as an alias by checking for the suffix of the filename. A simple example says more than a thousand words, so here it is:
alias -s pdf=xpdf
basically makes all files ending with “pdf” being opened using xpdf
. Reminds me of the “double-click everything” behaviour of a certain operating system, but is better at the same time. So e.g. specifying the following on the commandline:
- .pdf
will expand to:
xpdf *.pdf
this is because alias expansion is being applied before globbing.
Escape Sequences and Key Combinations
Create them using the builtin bindkey
:
bindkey ^A "beginning-of-line"
to view all defined bindkeys, just call without arguments just like with alias
:
bindkey
zsh
assists you in finding the right name for a given key combination through ^V
:
^V<key combination>
History Expansion
Although the syntax is quite identical to the one bash uses, the expansion may not be specified at the beginning of the line.
Command | Description | ||
---|---|---|---|
'Display ' | |||
history | show the full history | ||
'Expansion ' | |||
!! | expands to the last entered command | ||
!1 | expands to the first entered command | ||
!-2 | expands to the command entered right before the last one | ||
!!:1 | expands to the second word of the last command | ||
'Substitution ' | |||
!!:s | bla | blub | expands to the last command with “bla” being substituted by “blub” |
bla | blub | same as above, but a lot shorter |
Colour Escape Sequences
I created a file defining variables for the relevant colours:
fg_green=$'%{\e[0;32m%}' fg_blue=$'%{\e[0;34m%}' fg_cyan=$'%{\e[0;36m%}' fg_red=$'%{\e[0;31m%}' fg_brown=$'%{\e[0;33m%}' fg_purple=$'%{\e[0;35m%}' fg_light_gray=$'%{\e[0;37m%}' fg_dark_gray=$'%{\e[1;30m%}' fg_light_blue=$'%{\e[1;34m%}' fg_light_green=$'%{\e[1;32m%}' fg_light_cyan=$'%{\e[1;36m%}' fg_light_red=$'%{\e[1;31m%}' fg_light_purple=$'%{\e[1;35m%}' fg_no_colour=$'%{\e[0m%}' fg_white=$'%{\e[1;37m%}' fg_black=$'%{\e[0;30m%}'
and use the builtin source
to include the file just before defining the prompt (or using colour escapes in general).
Killer Features
First things first.
Prompt
There is builtin support for right-aligned prompts in zsh
. So one can have a prompt consisting of a left and a right part:
PROMPT="${fg_light_blue}%d %# ${fg_no_colour}" RPROMPT=" ${fg_light_green}%n@%M${fg_no_colour}"
Which will look like this: <br> Zsh.jpg
String Handling
Addressing inside strings is just like with Python
:
a="ich hab nix gemacht.";a[1]='I';a[-1]='. (Bart Simpson)'
Hashes
Take the following example:
typeset -A ass_array; ass_array=(one 1 two 2 three 3 four 4)
Then
print $ass_array[one]
returns the first element's value, 1.
print ${(k)ass_array}
returns the keys,
print ${(v)ass_array}
the values.
zcompile
The builtin zcompile
can be used to precompile zsh
source files for faster loading.
Option | Description |
---|---|
-R | create a “static” binary, being loaded for each process (recommended for small files) |
-M | create a “shared” binary, being mapped into memory and shared among different processes |
Precompiling All Zsh Functions
cd /usr/share/zsh/<version>/funtions for i in **/*(.) ; do zcompile $i done cd /usr/share/zsh/site/functions for i in *; do zcompile $i done