Upload
brett-day
View
217
Download
1
Embed Size (px)
Citation preview
Zsh
- Larry P. Schrof
Purpose• Spark your interest in zsh
• To learn more about zsh’s capabilities
• Expand your productivity
Topics Covered
• Configuration Files
• Prompt Tricks
• Monitoring Logins
• Aliases
• The Directory Stack
• Brace Expansion
• History: Using fc
• History Expansion
• Parameters: Arrays and Subscripting
• Parameters: Subscript Flags
• Parameters: Expansion
• Filename Generation
• Filename Generation: Qualifiers
• Programmable Completion
• Completion Items
• Restricted Mode
• ZLE (Zsh Command Line Editor)
• Where to get more information.
Configuration Files• Order:
– /etc/zshenv, then ~/.zshenv– /etc/zprofile, then ~/.zprofile (L)– /etc/zshrc, then ~/.zshrc (I)– /etc/zlogin, then ~/.zlogin (L)– /etc/zlogout then~/.zlogout (logout)
• (I) = Only if interactive. (L) = Only if login
Prompt Tricks• Arithmetic, command, and parameter
expansion available.
• Escape sequences start with a % (Over 40)
• Conditional Expressions – %(X.true-text.false-text)
• RPROMPT for right-hand side prompt
• Formatting and truncating available
Monitoring Logins• Event format: username@host%line
– root%console– [email protected]
• Put events in watch parameter– watch=(event1 event2 … eventN)
• Set the LOGCHECK parameter
• Formatting available
Aliases• name=value syntax
• Global aliases! Define with alias -g• Functions are usually preferred
The Directory Stack• Used for managing large lists of directories• dirs -v to examine stack• popd removes a directory from stack, and
cd’s to that directory• pushd cd’s to a new directory, and places $OLDPWD on top of the stack
• Can use ~ expansion to reference stack entries
Brace Expansion• Alows you to quickly generate lists in two
ways– ls -l array{5d,MYd,BIG}
– touch logfile.9908{01..31}.tmp
• zsh is smart about zero padding
History: Using fc
• fc loads past commands into editor, then executes them
• fc -l lists previous commands• fc [ -e EDITOR ] [OPTIONS] [old=new …] [first [last ] ]– Options: n l d D r t E i m
• fc is not as direct as history expansion, but a bit easier to use
History Expansion
• Allows you to reference complete or partial past commands
• All expansions start with • Use UP/DOWN arrows in emacs mode• CTRL-R in emacs mode is your friend.• ^old^new trick• designator[:word][:mod1:mod2...]
Parameters: Arrays and Subscripting
• Assign: name=(value1 … valueN)
• Subscripts: name[expr]– negative expr counts from end
• Range: name[expr1,expr2]• Replacement: name[expr]=value
– value can be an array
• Subscripts work on strings, too!
Parameters: Subscript Flags
• $name[(X)expr,(Y)expr2]– w splits by words– s splits on specified characters– f splits on a per-line basis– r treats expr as a pattern; returns first item
matching expr– i returns index of first match– R and I just like r and i, but return last match– n used with r, R, i, or I. Returns nth match.
Parameters: Expansion
• Standard ksh expansions– ${+var} ${var:-word} ${var:=word} ${var:?}
• Adds more expansions– ${name#pattern} ${name%pattern} ${name:#pattern}
• Any of above can be preceded by a # for length of result.
Filename Generation
• Set EXTENDED_GLOB variable
• Operators include *, ?, [], [!], #, ##, <->, |, ^, x~y
• **/ does recursive search– print -l /usr/local/src/**/*.[cho]
Filename Generation: Qualifiers
• Surpasses most other shells
• Limits wildcard matches
• After wildcard, enclose list in ()’s
• Over 40 qualifiers available, including negation
Programmable Completion
• Set with compctl command
• Fully programmable. If zsh doesn’t do what you want, you can write it yourself.
• Customizable. (menu? Cycle through list? Auto-insert?)
• zsh can complete ANYTHING!
Completion Items
• Filenames
• Built-in commands
• External commands
• aliases
• global aliases
• functions
• Shell variables
• Environment variables
• reserved words
• Zsh options
• key bindings
• Named directories
• usernames (files, NIS, etc)
• hosts
• running jobs
• suspended jobs
• Anything else you can imagine.
Restricted Mode
• Serves well for a guest account• http://www.acm.uiuc.edu/workshops/zsh/restricted.html
Command-line editor
• Emacs, vi-insert, and vi-command modes provided
• Build your own keymaps from scratch– 160+ pre-defined widgets– Write your own widgets
• Rebind any or all portions of keymaps• bindkey used to select and modify keymaps
– man zshmodules for details
More Information
• Workshop– http://www.acm.uiuc.edu/workshops/zsh
• Homepage– http://www.zsh.org
• FAQ:– http://sunsite.auc.dk/zsh/FAQ/