X-Git-Url: http://git.grml.org/?a=blobdiff_plain;f=zsh%2Fzsh-lovers.html;h=94d2c647cb2e4a864ef36bbb04421d8c70440631;hb=9dea264f9ed20b651ecc733def55f8db287225a6;hp=a95eb23d0c5f37bd15f9cbaf9d49c53973312e11;hpb=c39f079208e4a8e8a354e19e17d34ee43699e908;p=grml.org.git diff --git a/zsh/zsh-lovers.html b/zsh/zsh-lovers.html index a95eb23..94d2c64 100644 --- a/zsh/zsh-lovers.html +++ b/zsh/zsh-lovers.html @@ -2,15 +2,25 @@ "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> - - + + ZSH-LOVERS(1) - +
+

NAME

zsh-lovers - tips, tricks and examples for the Z shell

+
+

SYNOPSIS

Just read it. ;-)

+
+

OVERVIEW

Whenever we look at the zsh manual we wonder why there are no examples or those @@ -575,6 +762,8 @@ details.

just a just for fun - manpage ;)
For comments, bugreports and feedback take a quick look at the section BUGS.

+
+

SHELL-SCRIPTING

This section provides some examples for often needed shellscript-stuff. Notice @@ -583,7 +772,7 @@ Parse options in shellscripts. Example taken from ZWS by Adam Chodorowski (http://www.chodorowski.com/projects/zws/):

-
parse_options()
+
parse_options()
 {
     o_port=(-p 9999)
     o_root=(-r WWW)
@@ -602,21 +791,24 @@ Parse options in shellscripts. Example taken from ZWS by Adam Chodorowski
     if [[ $root[1] != '/' ]]; then root="$PWD/$root"; fi
 }
 # now use the function:
-parse_options $*
+parse_options $*
+
+

EXAMPLES

Available subsections are Aliases, Completion, Unsorted/Misc examples, (Recursive) Globbing - Examples, Modifiers usage, Redirection-Examples, ZMV-Examples and Module-Examples.

-

ALIASES

+
+

ALIASES

Suffix aliases are supported in zsh since version 4.2.0. Some examples:

-
alias -s tex=vim
+
alias -s tex=vim
 alias -s html=w3m
-alias -s org=w3m
+alias -s org=w3m

Now pressing return-key after entering foobar.tex starts vim with foobar.tex. Calling a html-file runs browser w3m. www.zsh.org and pressing @@ -624,15 +816,15 @@ enter starts w3m with argument www.zsh.org.
Global aliases can be used anywhere in the command line. Example:

-
$ alias -g C='| wc -l'
+
$ alias -g C='| wc -l'
 $ grep alias ~/.zsh/* C
-443
+443

Some more or less useful global aliases (choose whether they are useful or not for you on your own):

-
alias -g ...='../..'
+
alias -g ...='../..'
 alias -g ....='../../..'
 alias -g .....='../../../..'
 alias -g CA="2>&1 | cat -A"
@@ -670,9 +862,11 @@ alias -g VM=/var/log/messages
 alias -g X0G='| xargs -0 egrep'
 alias -g X0='| xargs -0'
 alias -g XG='| xargs egrep'
-alias -g X='| xargs'
+alias -g X='| xargs'
-

COMPLETION

+
+
+

COMPLETION

See also man 1 zshcompctl zshcompsys zshcompwid. zshcompctl is the old style of zsh programmable completion, zshcompsys is the new completion system, zshcompwid are the zsh completion widgets.

@@ -681,69 +875,69 @@ in order to proxy the list of results (like the list of available debian packages) Use a cache:

-
zstyle ':completion:*' use-cache on
-zstyle ':completion:*' cache-path ~/.zsh/cache
+
zstyle ':completion:*' use-cache on
+zstyle ':completion:*' cache-path ~/.zsh/cache

Prevent CVS files/directories from being completed:

-
zstyle ':completion:*:(all-|)files' ignored-patterns '(|*/)CVS'
-zstyle ':completion:*:cd:*' ignored-patterns '(*/)#CVS'
+
zstyle ':completion:*:(all-|)files' ignored-patterns '(|*/)CVS'
+zstyle ':completion:*:cd:*' ignored-patterns '(*/)#CVS'

Fuzzy matching of completions for when you mistype them:

-
zstyle ':completion:*' completer _complete _match _approximate
+
zstyle ':completion:*' completer _complete _match _approximate
 zstyle ':completion:*:match:*' original only
-zstyle ':completion:*:approximate:*' max-errors 1 numeric
+zstyle ':completion:*:approximate:*' max-errors 1 numeric

And if you want the number of errors allowed by _approximate to increase with the length of what you have typed so far:

-
zstyle -e ':completion:*:approximate:*' \
-        max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3))numeric)'
+
zstyle -e ':completion:*:approximate:*' \
+        max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3))numeric)'

Ignore completion functions for commands you don’t have:

-
zstyle ':completion:*:functions' ignored-patterns '_*'
+
zstyle ':completion:*:functions' ignored-patterns '_*'

With helper functions like:

-
xdvi() { command xdvi ${*:-*.dvi(om[1])} }
+
xdvi() { command xdvi ${*:-*.dvi(om[1])} }

you can avoid having to complete at all in many cases, but if you do, you might want to fall into menu selection immediately and to have the words sorted by time:

-
zstyle ':completion:*:*:xdvi:*' menu yes select
-zstyle ':completion:*:*:xdvi:*' file-sort time
+
zstyle ':completion:*:*:xdvi:*' menu yes select
+zstyle ':completion:*:*:xdvi:*' file-sort time

Completing process IDs with menu selection:

-
zstyle ':completion:*:*:kill:*' menu yes select
-zstyle ':completion:*:kill:*'   force-list always
+
zstyle ':completion:*:*:kill:*' menu yes select
+zstyle ':completion:*:kill:*'   force-list always

If you end up using a directory as argument, this will remove the trailing slash (usefull in ln)

-
zstyle ':completion:*' squeeze-slashes true
+
zstyle ':completion:*' squeeze-slashes true

cd will never select the parent directory (e.g.: cd ../<TAB>):

-
zstyle ':completion:*:cd:*' ignore-parents parent pwd
+
zstyle ':completion:*:cd:*' ignore-parents parent pwd

Another method for quick change directories. Add this to your ~/.zshrc, then just enter “cd …./dir”

-
rationalise-dot() {
+
rationalise-dot() {
   if [[ $LBUFFER = *.. ]]; then
     LBUFFER+=/..
   else
@@ -751,14 +945,16 @@ trailing slash (usefull in ln)

fi } zle -N rationalise-dot -bindkey . rationalise-dot +bindkey . rationalise-dot
-

UNSORTED/MISC examples

+
+
+

UNSORTED/MISC examples

Hint: A list of valid glob Qualifiers can be found in zshexpn(1). See “man 1 zshexpn | less -p” Qualifiers for details.

-
# Get the names of all files that *don't* match a pattern *anywhere* on the
+
# Get the names of all files that *don't* match a pattern *anywhere* on the
 # file (and without ``-L'' because its GNUish)
   $ print -rl -- *(.^e{'grep -q pattern $REPLY'})
   # or
@@ -1080,21 +1276,23 @@ See “man 1 zshexpn | less -p” Qualifiers for details.

> echo "Penguin on board." > else > echo "Not a Linux." - > fi + > fi
-

(Recursive) Globbing - Examples

+ +
+

(Recursive) Globbing - Examples

A list of valid glob Qualifiers can be found in zshexpn(1). Note: **/ is equivalent to (*/)#! For example:

-
$ print (*/)#zsh_us.ps
+
$ print (*/)#zsh_us.ps
 zsh-4.2.3/Doc/zsh_us.ps
 $ print **/zsh_us.ps
-zsh-4.2.3/Doc/zsh_us.ps
+zsh-4.2.3/Doc/zsh_us.ps
-
# Search for `README' in all Subdirectories
+
# Search for `README' in all Subdirectories
   $ ls -l **/README
 
 # find directories that contain both "index.php" and "index.html", or in
@@ -1240,15 +1438,17 @@ zsh-4.2.3/Doc/zsh_us.ps
$ rmdir ./**/*(/od) 2> /dev/null # Show only files which are owned by group `users'. - $ ls -l *(G[users])
+ $ ls -l *(G[users])
-

Modifiers usage

+
+
+

Modifiers usage

Modifiers are a powerful mechanism that let you modify the results returned by parameter, filename and history expansion. See zshexpn(1) for details.

-
# NOTE: Zsh 4.3.4 needed!
+
# NOTE: Zsh 4.3.4 needed!
   $ autoload -U age
 # files modified today
   $ print *(e:age today now:)
@@ -1328,13 +1528,15 @@ for details.

# convert 1st char of a word to uppercase $ foo="one two three four" $ print -r -- "${(C)foo}" - One Two Three Four + One Two Three Four
-

Redirection-Examples

+ +
+

Redirection-Examples

See zshmisc(1) for more informations (or less ${^fpath}/zmv(N))

-
# Append `exit 1' at the end of all *.sh - files
+
# Append `exit 1' at the end of all *.sh - files
   $ echo "exit 1" >> *.sh
 
 # adding files to foobar.tar.gz
@@ -1380,13 +1582,15 @@ for details.

# More fun with STDERR ;) $ ./my-script.sh 2> >(grep -v moron >error.log)|process-output >output.log - $ echo "Thats STDOUT" >>(sed 's/stdout/another example/' > foobar) + $ echo "Thats STDOUT" >>(sed 's/stdout/another example/' > foobar)
-

ZMV-Examples (require autoload zmv)

+ +
+

ZMV-Examples (require autoload zmv)

Note: -n means no execution (just print what would happen). At

-
# Remove illegal characters in a fat32 file system. Illegal characters are
+
# Remove illegal characters in a fat32 file system. Illegal characters are
 #   / :  ;  *  ?  "  <  >  |
 # NOTE: ``-Q'' and (D) is to include hidden files.
   $ unwanted='[:;*?\"<>|]'
@@ -1543,32 +1747,39 @@ for details.

# Rename pic1.jpg, pic2.jpg, .. to pic0001.jpg, pic0002.jpg, .. $ autoload zmv $ zmv 'pic(*).jpg' 'pic${(l:4::0:)1}.jpg' - $ zmv '(**/)pic(*).jpg' '$1/pic${(l:4::0:)2}.jpg' # recursively + $ zmv '(**/)pic(*).jpg' '$1/pic${(l:4::0:)2}.jpg' # recursively
-

Module-Examples

+ +
+

Module-Examples

Please read zshmodules(1) first!

+

zsh/pcre (require zmodload zsh/pcre)

-
# Copy files of a certain period (date indicated in the filenames)
+
# Copy files of a certain period (date indicated in the filenames)
   $ zmodload zsh/pcre
   $ ls -d -- *(e:'[[ $REPLY -pcre-match pcre-regexp ]]':)
   # or
   $ m() { [[ $1 -pcre-match pcre-regexp ]] }
-  $ ls -d -- *(+m)
+ $ ls -d -- *(+m)
+
+

zsh/clone (require zmodload zsh/clone)

-
# Creates a forked instance of the current shell ($! is set to zero) and
+
# Creates a forked instance of the current shell ($! is set to zero) and
 # execute ``command'' on /dev/tty8 (for this example).
   $ zmodload zsh/clone
-  $ clone /dev/tty8 && (($! == 0)) && exec command
+ $ clone /dev/tty8 && (($! == 0)) && exec command
+
+

zsh/datetime (require zmodload zsh/datetime)

-
  $ zmodload zsh/datetime
+
  $ zmodload zsh/datetime
   $ alias datereplacement='strftime "%Y-%m-%d" $EPOCHSECONDS'
   $ export DATE=`datereplacement`
   $ echo $DATE
@@ -1590,12 +1801,14 @@ for details.

$ strftime -s file "abc_de_%m%d%Y.dat" $((EPOCHSECONDS - 24 * 60 * 60 )) $ files=(**/$file(N.L0)) $ (( $#files > 0 )) && print -rl -- $files | \ - mailx -s "empty files" foo@bar.tdl + mailx -s "empty files" foo@bar.tdl
+
+

zsh/stat (require zmodload zsh/stat)

-
# test if a symbolic link links to a certain file
+
# test if a symbolic link links to a certain file
   $ zmodload -i zsh/stat
   $ ! stat -LH s foo.ln || [[ $s[link] != "foo.exe" ]] || ln -sf foo.exe foo.ln
 
@@ -1667,21 +1880,25 @@ for details.

$ stat -F '%d %m %Y' +mtime ~/.zshrc 30 06 2004 $ stat -F '%D' +mtime ~/.zshrc - 06/30/04 + 06/30/04
+ +

zsh/files (require zmodload zsh/files)

-
# search a directory for files containing a certain string then copy those
+
# search a directory for files containing a certain string then copy those
 # files to another directory.
   $ zmodload zsh/files
   $ IFS=$'\0'
-  $ cp $(grep -lZr foobar .) otherdirectory
+ $ cp $(grep -lZr foobar .) otherdirectory
+
+

zsh/mapfile (require zmodload zsh/mapfile)

-
# grepping for two patterns
+
# grepping for two patterns
   $ zmodload zsh/mapfile
   $ pattern1="foo"
   $ pattern2="bar foo"
@@ -1721,12 +1938,14 @@ for details.

# end of the filename $ autoload -U zmv $ zmodload zsh/mapfile - $ zmv '(*).ps' '$1-${${${mapfile[$f]##*%%Title: }%% *}//[^a-zA-Z0-9_]/}.ps' + $ zmv '(*).ps' '$1-${${${mapfile[$f]##*%%Title: }%% *}//[^a-zA-Z0-9_]/}.ps'
+ +

zsh/mathfunc (require zmodload zsh/mathfunc)

-
$ zmodload zsh/mathfunc
+
$ zmodload zsh/mathfunc
 $ echo $(( sin(1/4.0)**2 + cos(1/4.0)**2 - 1 ))
   -1.1102230246251565e-16
 $ echo $(( pi = 4.0 * atan(1.0) ))
@@ -1736,22 +1955,26 @@ $ echo $(( f = sin(0.3) ))
 $ print $((1e12 * rand48()))
   847909677310.23413
 $ print $(( rand48(seed) ))
-  0.01043488334700271
+ 0.01043488334700271
+
+

zsh/termcap (require zmodload zsh/termcap)

-
 $ zmodload -ab zsh/termcap echotc
+
 $ zmodload -ab zsh/termcap echotc
  $ GREEN=`echotc AF 2`
  $ YELLOW=`echotc AF 3`
  $ RED=`echotc AF 1`
  $ BRIGHTRED=`echotc md ; echotc AF 1`
- $ print -l ${GREEN}green ${YELLOW}yellow ${RED}red ${BRIGHTRED}brightred
+ $ print -l ${GREEN}green ${YELLOW}yellow ${RED}red ${BRIGHTRED}brightred
+
+

zsh/zpty (require zmodload zsh/zpty)

-
  $ zmodload zsh/zpty
+
  $ zmodload zsh/zpty
   $ zpty PW passwd $1
   $ zpty PW passwd $1
 # ``-r'': read the output of the command name.
@@ -1765,12 +1988,14 @@ $ print $(( rand48(seed) ))
 # previously started, by supplying a list of their names. If no names
 # are given, all commands are deleted. Deleting a command causes the HUP
 # signal to be sent to the corresponding process.
-  $ zpty -d PW
+ $ zpty -d PW
+
+

zsh/net/socket (require zmodload zsh/net/socket)

-
# ``-l'': open a socket listening on filename
+
# ``-l'': open a socket listening on filename
 # ``-d'': argument will be taken as the target file descriptor for the
 #         connection
 # ``3'' : file descriptor. See ``A User's Guide to the Z-Shell''
@@ -1782,12 +2007,14 @@ $ print $(( rand48(seed) ))
   $ zsocket -a -d 5 3 # accept a connection
   $ echo foobar >&4
   $ echo barfoo >&5
-  $ 4>&- 5>&- 3>&
+ $ 4>&- 5>&- 3>&
+
+

zsh/zftp (require zmodload zsh/zftp)

-
 $ autoload -U zfinit
+
 $ autoload -U zfinit
  $ zfinit
  $ zfparams www.example.invalid myuserid mypassword
  $ zfopen
@@ -1833,12 +2060,14 @@ $ print $(( rand48(seed) ))
 # If the list.txt is located on the remote host, change to
 # zfget ${(f)"$(zftp get /path/to/remote/list.txt)"}
   $ zfget ${(f)"$(cat list.txt)"}
-  $ zfclose
+ $ zfclose
+
+

zsh/zselect (require zmodload zsh/zselect)

-
# It's similar to
+
# It's similar to
  ,----
  | $ sg=$(stty -g)
  | $ stty -icanon min 0 time 50
@@ -1854,55 +2083,71 @@ $ if zselect -t 500 -r 0 && read yesno && [ yes = "$yesno" ]; th
 >    command1
 > else
 >    command1
-> fi
+> fi
+ + + +

OPTIONS

-

Navigation options

+
+

Navigation options

auto_cd (allow one to change to a directory by entering it as a command). auto_pushd (automatically append dirs to the push/pop list) pushd_ignore_dups (and don’t duplicate them).

-

Misc

+
+
+

Misc

no_hup (don’t send HUP signal to background jobs when exiting ZSH). print_exit_value (show a message with the exit code when a command returns with a non-zero exit code)

+

History options

hist_verify (let the user edit the command line after history expansion (e.g. !ls) instead of immediately running it)
Use the same history file for all sessions :
setopt SHARE_HISTORY

+
+

Privacy / Security

no_clobber (or set -C; prevent > redirection from truncating the given file if it already exists)

+
+

Spelling correction

correct (automatically correct the spelling of commands). correct_all (automatically correct the spelling of each word on the command line) dvorak (dvorak layout)

+
+
+
+

UNSORTED/MISC

Mailpath: simple multiple mailpath:

-
mailpath=($HOME/Mail/mbox'?new mail in mbox'
+
mailpath=($HOME/Mail/mbox'?new mail in mbox'
           $HOME/Mail/tux.u-strasbg'?new mail in tux'
           $HOME/Mail/lilo'?new mail in lilo'
-          $HOME/Mail/ldap-fr'?new mail in ldap-fr')
+ $HOME/Mail/ldap-fr'?new mail in ldap-fr')

Mailpath: dynamic mailpath:

-
typeset -a mailpath
+
typeset -a mailpath
 for i in ~/Mail/Lists/*(.); do
    mailpath[$#mailpath+1]="${i}?You have new mail in ${i:t}."
-done
+done

Avoid globbing on special commands:

-
for com in alias expr find mattrib mcopy mdir mdel which;
-alias $com="noglob $com"
+
for com in alias expr find mattrib mcopy mdir mdel which;
+alias $com="noglob $com"

For migrating your bashprompt to zsh use the script bash2zshprompt located in the zsh source distribution under Misc.

@@ -1917,7 +2162,7 @@ displayed, and to the command name and rest of the command line, respectively, when a command is executed:

-
function title {
+
function title {
       if [[ $TERM == "screen" ]]; then
         # Use these two for GNU Screen:
         print -nR $' 33k'$1$' 33'\
@@ -1932,20 +2177,22 @@ function preexec {
     emulate -L zsh
     local -a cmd; cmd=(${(z)1})
     title $cmd[1]:t "$cmd[2,-1]"
-}
+}

Put the following line into your ~/.screenrc to see this fancy hardstatus:

-
caption always "%3n %t%? (%u)%?%?: %h%?"
+
caption always "%3n %t%? (%u)%?%?: %h%?"

Special variables which are assigned:

-
$LINENO $RANDOM $SECONDS $COLUMNS $HISTCHARS $UID
-$EUID $GID $EGID $USERNAME $fignore $mailpath $cdpath
+
$LINENO $RANDOM $SECONDS $COLUMNS $HISTCHARS $UID
+$EUID $GID $EGID $USERNAME $fignore $mailpath $cdpath
+ +
@@ -2127,6 +2374,8 @@ The Z shell reference-card (included in the zsh-lovers debian-package)
+
+

AUTHORS

This manpage was written by Michael Prokop, Christian strcat @@ -2137,7 +2386,7 @@ Thanks for your cool and incredible tips. We learned much from you!

In alphabetic order:

-
Andrew 'zefram' Main  - http://www.fysh.org/~zefram/
+
Andrew 'zefram' Main  - http://www.fysh.org/~zefram/
 Barton E. Schaefer    - http://www.well.com/user/barts/
 Matthias Kopfermann   - http://www.infodrom.north.de/~matthi/
 Oliver Kiddle         - http://people.freenet.de/opk/
@@ -2146,15 +2395,17 @@ Peter Stephenson      - http://homepage.ntlworld.com/p.w.stephenson/
 Richard Coleman
 Stephane Chazelas     - http://stephane.chazelas.free.fr/
 Sven Guckes           - http://www.guckes.net/
-Sven Wischnowsky      - http://w9y.de/zsh/zshrc
+Sven Wischnowsky - http://w9y.de/zsh/zshrc
+
+

SEE ALSO

Manpages of zsh:

-
       zsh          Zsh overview
+
       zsh          Zsh overview
        zshall       Tthe Z shell meta-man page
        zshbuiltins  Zsh built-in commands
        zshcalsys    zsh calendar system
@@ -2171,29 +2422,34 @@ Sven Wischnowsky      - http://w9y.de/zsh/zshrc
zshtcpsys Zsh tcp system zshzle Zsh command line editing zshzftpsys Zsh built-in FTP client - zshall Meta-man page containing all of the above
+ zshall Meta-man page containing all of the above

Note: especially man zshcontrib covers very useful topics!
Book: From Bash to Z Shell by Oliver Kiddle, Jerry Peck and Peter Stephenson. ISBN: 1590593766. - bash2zsh.com
Also take a look at the section LINKS in this manpage.

+
+

BUGS

Probably. This manpage might be never complete. So please report bugs, feedback and suggestions to <zsh-lovers@michael-prokop.at>. Thank you!

+
+

Copyright (C) Michael Prokop, Christian Schneider and Matthias Kopfermann.

+