Z Shell – the Last Shell You‟Ll Ever Need
Total Page:16
File Type:pdf, Size:1020Kb
Z Shell – the last shell you‟ll ever need changlp Computer Center, CS, NCTU CS, Computer Center, Introduction - Shell 殼 Fetch → Analyze → Execute • Fork Type Login Non-login Interactive User‟s shell Shell executed after login Non- ? Script interactive 2 Computer Center, CS, NCTU CS, Computer Center, Introduction – ZSH (1/2) Originally by Paul Falstad in 1990 A shell designed for interactive use • Powerful user experience Use sh as underlying syntax • Easy for using / migrating Completion & correction Available in ports: shells/zsh 3 Computer Center, CS, NCTU CS, Computer Center, Introduction – ZSH (2/2) % rsync -<TAB> % cd /usr/lica<TAB> 4 Computer Center, CS, NCTU CS, Computer Center, Startup Files (1/2) For all ZSH • /etc/zshenv • ~/.zshenv If [ login shell ] • /etc/zprofile Login Non-login • ~/.zprofile Interactive User‟s shell Shell executed after login Non- ? Script If [ interactive ] interactive • /etc/zshrc • ~/.zshrc If [ login shell ] • /etc/(z)login • ~/.(z)login 5 Computer Center, CS, NCTU CS, Computer Center, Startup Files (2/2) zshenv • Settings for ALL types of ZSH zprofile • Settings for each login session (path … etc) zshrc • Some interactive settings (prompt … etc) zlogin (zlogout) • Execute some program on login / logout 6 Computer Center, CS, NCTU CS, Computer Center, IO Redirection - Multios % date >file1 >file2 … >fileN % date >file1 | prog1 % cat -n < /var/log/**/*log(R.) • All files under /var/log and its subdir with 0004 permission % cat bar | sort <foo • % cat bar foo | sort # note the order 7 Computer Center, CS, NCTU CS, Computer Center, IO Redirection - Null Redirection % < file • more or $READNULLCMD % > file • cat or $NULLCMD 8 Computer Center, CS, NCTU CS, Computer Center, Filename Gerneration – Extended Globbing Common shells support simple globbing • ls –lh * • ls –lh *.[co] • ls –lh *.[^oh] ZSH supports extended globbing • ls –lh ^*.c • ls –lh ^Makefile • Option: EXTENDEDGLOB (default on) 9 Computer Center, CS, NCTU CS, Computer Center, Filename Generation – Globbing Recursive globbing **/ All dirs and subdirs ***/ Also follow symbolic links • Ex. $ ls –lh **/foo Searches foo under all dir & subdir Form: (flags)pattern,operator …(qualifier) • Ex. $ ls –lh (#i)[[:digit:]]ggyy.[^oh](R.) 10 Computer Center, CS, NCTU CS, Computer Center, Filename Generation – Globbing flags Globbing flags Affect to their right Until end of enclosing group or end of the pattern May interleave with operators $ ls -lh (#i)Make(#I)file • #I/i: case sensitivity • #cM,N: {M,N} in regex • #a: Approximate $ ls 1ggyy.c MakeFile Makefile • … Mekefile Mikefile Mokefile Mukefile Nukefile makefile $ ls (#i)Make(#I)file Makefile makefile 11 Computer Center, CS, NCTU CS, Computer Center, Filename Generation – Globbing operator Globbing operator Cooperate with patterns Like regex • * This is self explanatory • [] Match enclosed characters Named class – [:alpha:], [:digit:]… Use “^” or “!” for negation • () Grouping • … 12 Computer Center, CS, NCTU CS, Computer Center, Filename Generation – Globbing qualifier Globbing qualifier Specify which filenames should be argument list • /: directories • .: plain files • @: links • +cmd: filename will be included if cmd returns a zero status • … 13 Computer Center, CS, NCTU CS, Computer Center, Filename Generation – Reference For further glob information • http://zsh.sourceforge.net/Doc/Release/Expansion.html#Filename- Generatio • Have a look on other types of expansion find(1) 是什麼小朋友? 可以吃嗎?Open! ZSH 愛用者 14 Computer Center, CS, NCTU CS, Computer Center, Script Bourne shell based Globbing Expansion • zshexpn(1) Portability • Use zsh built-ins if possible • Module support 15 Computer Center, CS, NCTU CS, Computer Center, Script – Type System Data Types • Array Slice • Scalar (string) • Float • Integer • Association (hash) % typeset -A blah % typeset -r agent • Constant % typeset -U unique_set • Unique set … 16 Computer Center, CS, NCTU CS, Computer Center, Script - Module zsh/curses zsh/mathfunc zsh/net/tcp zsh/example … Example • http://zshwiki.org/home/code/scripts/zshttpd 17 Computer Center, CS, NCTU CS, Computer Center, Completion <TAB>, and shell tries to fill the rest In (t)csh complete sudo 'p/1/c/' complete chown 'p/1/u/‘ complete cd ‘p/1/d/’ • More in: /usr/share/examples/tcsh/complete.tcsh In bash complete -f -X '!*.@(Z|[gGd]z|t[ag]z)' gunzip zcat unpigz • More in: shells/bash-completion Completion distributed separately 18 Computer Center, CS, NCTU CS, Computer Center, Completion in ZSH Programmable completion Context sensitive • % echo $OS<TAB> • % cd <TAB> • % ssh <TAB> Completion function distributed with ZSH • [FreeBSD] See /usr/local/share/zsh/<version>/functions/Completion/ 19 Computer Center, CS, NCTU CS, Computer Center, Completion Context (1/2) :completion:<func>:<completer>:<command>:<argument>:<tag> • Current context while completing • How completion arrived here so far Func • Blank if called from standard completion system Completer • Completer name („completer‟ in standard completion case) • Correction… etc Command Argument Tag • Things can be completed from this point 20 Computer Center, CS, NCTU CS, Computer Center, Completion Context (2/2) % cd • :completion::complete:cd:: % git add • :completion::complete:git-add:: (+ more arguments) • :completion::complete:git:: (+ more subcommands) Tag: modified-files other-files ignored-modified-files ignored-other-files untracked-files 21 Computer Center, CS, NCTU CS, Computer Center, Completion Styles Control the behavior of completers zstyle ':completion:*' menu select zstyle ':completion:*:kill:*' force-list always zstyle ':completion:*' completer _complete _correct _approximate 22 Computer Center, CS, NCTU CS, Computer Center, Summing Up Pros • Powerful completion system Good for interactive Built-in completion support for most UNIX programs Cons • Bad for • Slow Completion system overhead • Complex configuration 23 Computer Center, CS, NCTU CS, Computer Center, Live Demo 如果有東西壞了請當作沒有看到 24 Computer Center, CS, NCTU CS, Computer Center, Some Plugins & Resourse https://github.com/zsh-users/zsh-syntax-highlighting https://github.com/robbyrussell/oh-my-zsh From Bash to Z Shell: Conquering the Command Line ZSH manual 25 .