21
Zsh - Larry P. Schrof

Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

Embed Size (px)

Citation preview

Page 1: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

Zsh

- Larry P. Schrof

Page 2: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

Purpose• Spark your interest in zsh

• To learn more about zsh’s capabilities

• Expand your productivity

Page 3: 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.

Page 4: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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

Page 5: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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

Page 6: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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

Page 7: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

Aliases• name=value syntax

• Global aliases! Define with alias -g• Functions are usually preferred

Page 8: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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

Page 9: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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

Page 10: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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

Page 11: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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

Page 12: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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!

Page 13: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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.

Page 14: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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.

Page 15: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

Filename Generation

• Set EXTENDED_GLOB variable

• Operators include *, ?, [], [!], #, ##, <->, |, ^, x~y

• **/ does recursive search– print -l /usr/local/src/**/*.[cho]

Page 16: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

Filename Generation: Qualifiers

• Surpasses most other shells

• Limits wildcard matches

• After wildcard, enclose list in ()’s

• Over 40 qualifiers available, including negation

Page 17: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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!

Page 18: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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.

Page 19: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

Restricted Mode

• Serves well for a guest account• http://www.acm.uiuc.edu/workshops/zsh/restricted.html

Page 20: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

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

Page 21: Zsh - Larry P. Schrof. Purpose Spark your interest in zsh To learn more about zsh’s capabilities Expand your productivity

More Information

• Workshop– http://www.acm.uiuc.edu/workshops/zsh

• Homepage– http://www.zsh.org

• FAQ:– http://sunsite.auc.dk/zsh/FAQ/