48
zshとわたし @第04回 福岡Debian勉強会 しもかわ としひこ @ToshiChan [email protected]

zsh とわたし

Embed Size (px)

DESCRIPTION

第04回目福岡 Debian 勉強会で zsh について軽く喋ってみました。

Citation preview

Page 1: zsh とわたし

zshとわたし@第04回 福岡Debian勉強会

しもかわ としひこ@ToshiChan

[email protected]

Page 2: zsh とわたし

Outline•自己紹介• zsh•初期設定ファイルを眺めながらよもやま話

•まとめ

Page 3: zsh とわたし

自己紹介• しもかわとしひこ(下川俊彦)

• 九州産業大学 情報科学部 情報科学科

• 専門は広域分散処理

• ’89ぐらいからUNIX使い

• Ultrix, SunOS, Solaris, Linux, FreeBSD, OS X

• Debian は使ったことがないです ^^;

• ubuntu は仕事で使ってます

Page 4: zsh とわたし

質問• login shell はなんですか?

• zsh

• bash

• tcsh

• csh

• ksh

• ash

• その他?

Page 5: zsh とわたし

zshとは?Zsh is a UNIX command interpreter (shell) usable as an interactive login shell and as a shell script command processor. Of the standard shells, zsh most closely resembles ksh but includes many enhancements. Zsh has command line editing, builtin spelling correction, programmable command completion, shell functions (with autoloading), a history mechanism, and a host of other features.

Page 6: zsh とわたし

zsh は良い•なんと言っても名前がカッコ良い• Zが付くものはすべからくカッコ良い

Page 7: zsh とわたし

zsh は良い(続き)• 最近の bash はよく知りませんが…• RPROMPT• global alias• function の autoload• 強力なパターンマッチ• ** による再帰的なディレクトリ検索とか• 強力なコマンドラインエディタ• Esc-q とか• 様々な build in function• [[ とか• 様々な option

Page 8: zsh とわたし

zshの初期化ファイル1. /etc/zshenv

2. $ZDOTDIR/.zshenv

3. /etc/zprofile (login shell 時のみ)

4. $ZDOTDIR/.zprofile (login shell 時のみ)

5. /etc/zshrc (対話モード時のみ)

6. $ZDOTDIR/.zshrc (対話モード時のみ)

7. /etc/zlogin (login shell 時のみ)

8. $ZDOTDIR/.zlogin (login shell 時のみ)

Page 9: zsh とわたし

$ZDOTDIR

•未設定の場合 $HOME が使われる• ZDOTDIR 設定してる人います?

Page 10: zsh とわたし

僕の .z*• 僕が使っているzsh初期化ファイルを紹介

• 決して「良い設定」の例ではないです

• 青はお気に入りの設定

• 赤は不要あるいは僕にも意味不明の設定

• 20年以上使っている設定なので ^^;

• 全て一部略なので心の目で読んでね

• 折り返しがわかりにくいので行番号を入れてます

• 普段 Debian 使ってないので、一部 OS X 用を使いますが許してね

Page 11: zsh とわたし

cvs log -r1.1 .zshrc1. RCS file: /home1/toshi/lib/cvsroot/dots/.zshrc,v2. Working file: .zshrc3. head: 2.184. branch:5. locks: strict6. access list:7. symbolic names:8. keyword substitution: kv9. total revisions: 38; selected revisions: 110. description:11. zsh initialize file.12. ----------------------------13. revision 1.114. date: 1993-03-09 13:54:55 +0900; author: toshi; state: Exp;15. Initial revision16. ===================================================

Page 12: zsh とわたし

zshenv• 常に実行される• 最小限の設定に抑えることが重要• とマニュアルに書いてある

• 基本的には環境変数・umask ぐらい?• 対話モードに関係ある設定は入れない• プロンプトの設定など

Page 13: zsh とわたし

僕の .zshenv (1)1. # $Id: .zshenv,v 2.23 2013/02/15 01:54:40 toshi Exp $2. #3. #setopt XTRACE4. case "$-" in5. *i*) # this is interactive shell6. echo `LANG=C date` : reading .zshenv7. esac8.  9. umask 022

Page 14: zsh とわたし

僕の .zshenv (2)1. PATH="${HOME}/bin:${HOME}/bin/${MACHTYPE}-${OSTYPE}:${HOME}/bin:\

2. /opt/local/sbin:/opt/local/bin:\

3. /usr/pkg/bin:/usr/pkg/sbin:\

4. /usr/local/bin:\

5. /usr/X11R6/bin:/usr/local/X11R6/bin:\

6. /usr/openwin/bin:/usr/openwin/demo:\

7. /usr/ccs/bin:/opt/SUNWspro/bin:\

8. /usr/local/WordPerfect8/wpbin:\

9. /usr/ucb:/usr/bin:/bin:/usr/lib:/usr/5bin:/usr/kvm:\

10. /usr/local/etc:/etc:/usr/etc:/usr/hosts:\

11. /usr/local/sbin:/usr/sbin:/sbin"

12. export PATH

Page 15: zsh とわたし

僕の .zshenv (3)1. LC_TIME=C

2. export LC_TIME

3. EDITOR=vi

4. export EDITOR

5.

6. # application specific environments

7. PAGER=less

8. export PAGER

9. LESS=ciMrf

10. export LESS

11.

12. # architecuture dependent

13. if [ -r ${HOME}/.zshenv-${OSTYPE} ]; then14. . ${HOME}/.zshenv-${OSTYPE}15. fi16. # site dependent17. if [ -r ${HOME}/.zshenv-local ]; then18. . ${HOME}/.zshenv-local19. fi20. 21. # Finish22. EnvironSetup='Ok'23. export EnvironSetup

Page 16: zsh とわたし

僕の .zshenv-darwin (1)1. # $Id: .zshenv-darwin,v 1.1 2013/02/10 12:44:04 toshi Exp $

2. #

3. #setopt XTRACE

4. case "$-" in

5. *i*) # this is interactive shell

6. echo `LANG=C date` : reading .zshenv-darwin

7. esac

Page 17: zsh とわたし

僕の .zshenv-darwin (2)1. PAGER=lv

2.  

3. PATH=\

4. ${HOME}/.rbenv/shims:\

5. /usr/local/bin:\

6. `xcode-select --print-path`/usr/bin:\

7. /Applications/Ghostscript.app/bin:\

8. /Applications/eclipse/android/platform-tools:\

9. ${PATH}

Page 18: zsh とわたし

某サーバマシンでの僕の .zshenv-local1. # $Id: .zshenv,v 2.19 2007/08/28 04:07:50 toshi Exp $

2. #

3. #setopt XTRACE

4. case "$-" in

5. *i*) # this is interactive shell

6. echo `date` : reading .zshenv-local

7. esac

8.  

9. source ${HOME}/.keychain/${HOST}-sh

Page 19: zsh とわたし

zshenv 考察• PATHの設定は悩ましい•環境依存、利用者依存の設定の綺麗な共存

•つーか、そろそろ CVS 捨てたい

Page 20: zsh とわたし

(閑話休題)環境設定ファイルの同期• 2つの shell script を利用

• リポジトリからファイル群を check out (clone?)

• ウィンドウ間の cut & paste で実行できるようにシンプル

• 今のところリポジトリは CVS だけど git にしたいなぁ

• check out した script を、$HOME 他適切な場所へ deploy

• deploy つーても symlink して、最低限必要なディレクトリを掘るだけ

• deploy 用 script はリポジトリ内に入ってるので、多少複雑な処理をしても大丈夫

Page 21: zsh とわたし

$HOME/lib/dots/init1. CVSWORK=${HOME}/lib

2.  

3. CVSROOT=ひ・み・つ♡

4. export CVSROOT

5.  

6. CVS_RSH=ssh

7. export CVS_RSH

8.  

9. mkdir -p ${CVSWORK}

10. cd ${CVSWORK}

11. cvs checkout dots

Page 22: zsh とわたし

$HOME/lib/dots/setup1. #! /bin/sh

2. # $Id: setup,v 2.15 2008/08/14 12:58:29 toshi Exp $

3. FILES='.z* .cvsrc .cvsignore .screenrc'

4. LN='ln -sf'

5. CP='cp'

6. CHMOD='chmod'

7. cd ${HOME}/lib/dots/

8. for i in ${FILES}

9. do

10. ${LN} lib/dots/${i} ${HOME}

11. done

12. if [ ! -d ${HOME}/.ssh ]; then

13. mkdir ${HOME}/.ssh

14. fi

15. if [ -f ${HOME}/.ssh/authorized_keys2 ]; then

16. mv ${HOME}/.ssh/authorized_keys2 ${HOME}/.ssh/authorized_keys2.old

17. fi

18. ${CP} ${HOME}/lib/dots/authorized_keys2 ${HOME}/.ssh

19. ${CHMOD} 600 ${HOME}/.ssh/authorized_keys2

20.  

21. if [ ! -d ${HOME}/tmp ]; then

22. mkdir ${HOME}/tmp

23. fi

Page 23: zsh とわたし

zprofile• login shell 時に zshrc より前に実行•何に使ってます?•昔は Window System の起動に使ってたけど…

Page 24: zsh とわたし

僕の .zprofile1. # $Id: .zprofile,v 1.9 2006/09/12 01:19:57 toshi Exp $

2. #

3. #setopt XTRACE

4. echo `LANG=C date` : reading .zprofile

5. if [ -e ~/.todo ]; then

6. todo=`cat ~/.todo`

7. rm ~/.todo

8. $todo

9. exit

10. fi

Page 25: zsh とわたし

zprofile 考察

•何に使うとおしゃれなんだろう?

Page 26: zsh とわたし

zshrc

•対話shellで使う設定はここ•プロンプト, alias, function, キーバインド, オプション, shell変数…

Page 27: zsh とわたし

僕の .zshrc (1)1. # $Id: .zshrc,v 2.18 2012/06/13 08:53:44

toshi Exp $

2. #setopt XTRACE

3. echo `date` : reading .zshrc

4. # This is interactive shell

5. stty erase ^h

6. # set zsh options

7. setopt append_history auto_cd auto_menu auto_pushd

8. setopt auto_param_keys auto_remove_slash auto_resume

9. setopt cdable_vars correct extended_history hash_cmds

10. setopt hist_ignore_dups hist_ignore_space

11. setopt hist_no_store ignore_eof list_types long_list_jobs notify

12. setopt numeric_glob_sort print_exit_value

13. setopt pushd_silent pushd_ignore_dups pushd_to_home

14. setopt no_prompt_cr always_last_prompt no_flow_control

15. setopt share_history prompt_subst

Page 28: zsh とわたし

僕の .zshrc (2)1. # set zsh original variables

2. CDPATH=~

3. fignore=( .o .fasl .aux .dvi .toc .class)

4. _myfpath=${HOME}/lib/dots/zfunc

5. fpath=(/usr/local/share/zsh/site-functions $fpath)

6. [[ -z $fpath[(r)$_myfpath] ]] && fpath=($_myfpath $fpath)

7. host=`echo $HOST | sed -e 's/\([^.]*\).*/\1/'`

8. DIRSTACKSIZE=10

9. HISTSIZE=100000

10. HISTFILE=~/.zhistory

11. SAVEHIST=100000

12. if [[ -d /var/mail ]]; then

13. MAILSPOOL=/var/mail

14. else

15. MAILSPOOL=/usr/spool/mail

16. fi

17. MAIL=${MAIL:-${MAILSPOOL}/${LOGNAME}}

18. MAILCHECK=1

19. WATCH=notme

20. WATCHFMT='%n has %a %l from %M at %T.'

21. HELPDIR=/usr/local/lib/zsh/help

22. REPORTTIME=1

Page 29: zsh とわたし

僕の .zshrc (3)1. if [[ ! ($TERM = "screen" || $TERM = "screen-bce") ]]; then

2. PROMPT=$'%{\e[$[32+$RANDOM % 5]m%}''%S%h%s%n@%U%m%u[%T] %c %# '$'%{\e[m%

3. }'

4. RPROMPT=$'%{\e[33m%}%~%{\e[m%}'

5. else

6. PROMPT=$'%{\e[$[32+$RANDOM % 5]m%}''%S%h%s%n@%U%m%u($WINDOW)[%T] %c %# '

7. $'%{\e[m%}'

8. RPROMPT='[%U%~%u]'

9. fi

Page 30: zsh とわたし

僕の .zshrc (4)1. if [[ -z "$ZSH_VERSION" ]]; then

2. ZSH=`echo $VERSION | cut -d\ -f2`

3. else

4. ZSH=${ZSH_VERSION:s/-/./}

5. fi

6. rev=$ZSH:r:r

7. rel=$ZSH:r:e

8. pat=$ZSH:e

9. _zsh_version=`expr $rev \* 1000000 + $rel \* 1000 + $pat`

Page 31: zsh とわたし

僕の .zshrc (4)1. autoload acx cx harden mere namedir proto randline run-

help yp yu zed

2. autoload inc-and-show lhal mman my-rm lstmp unzip-v

3. autoload whichls up unload mk-dist ps-tty delpath settermgw

4. autoload cdmatch multicomp tail-f

5. [[ $TERM = "xterm-color" || $TERM = "xterm" || $TERM = "kterm" || $TERM = "vt100" || $TERM = "screen" || $TERM = "screen-bce" || $TERM = "cons25" || $TERM = "cygwin" ]] && stty pass8 && bindkey -e

Page 32: zsh とわたし

僕の .zshrc (5)1. bindkey ^X^I reverse-menu-complete

2. bindkey ^X^P reverse-menu-complete

3. bindkey ^X^N menu-complete

4. bindkey ^X= expand-cmd-path

5. if [[ $_zsh_version -lt 3001000 ]]; then

6. bindkey ^P up-line-or-search

7. bindkey ^N down-line-or-search

8. fi

Page 33: zsh とわたし

僕の .zshrc (6)1. safe_source() {2. if [[ $pat = pre2 ]]; then3. return4. fi5. if [[ -r ~/$1 ]]; then6. . ~/$17. elif [[ -r ~$2/$1 ]]; then8. . ~$2/$19. fi10.}11.[[ -z "$EnvironSetup" ]] && safe_source .zshenv toshi

Page 34: zsh とわたし

僕の .zshrc (7)1. if [[ $_zsh_version -gt 3001007 ]]; then

2. zstyle ':completion:*' completer '_complete'

3. zstyle :compinstall filename "${HOME}/.zcompsys"

4. autoload -Uz compinit

5. compinit -u

6. if [ -f ~/.ssh/known_hosts ]; then

7. hosts() { reply=(`sed -e 's/[\ ,].*//' ~/.ssh/known_hosts`) }

8. else

9. hosts() { reply=(sh.example.jp. \

10. sh.example.com. ) }

11. fi

12. fi

13. safe_source .zaliases toshi

14. safe_source .zshrc-${OSTYPE} toshi

15. safe_source .zshrc-${host} toshi

16. safe_source .zshrc-local toshi

Page 35: zsh とわたし

僕の .zaliases (1)1. # $Id: .zaliases,v 2.17 2009/07/05 02:29:59 toshi Exp $

2. #

3. #setopt XTRACE

4. echo `date` : reading .zaliases

5.  

6. alias rcd='cd'

7. alias back='cd -'

8. alias .='dirs -v'

9. alias ..='cd ..'

10. alias ,='back'

11. alias ,,='cd +2'

12. alias ,,,='cd +3'

13. alias ,,,,='cd +4'

14. alias ,,,,,='cd +5'

15. alias ,,,,,,='cd +6'

16. alias ,,,,,,,='cd +7'

17. alias ,,,,,,,,='cd +8'

18. alias ,,,,,,,,,='cd +9'

Page 36: zsh とわたし

僕の .zaliases (2)1. alias l=ls2. case ${OSTYPE} in3. freebsd*)4. alias ls='ls -CFw'5. ;;6. cygwin*)7. alias ls='ls -CF --show-control-chars'8. ;;9. *)10. alias ls='ls -CF'11. ;;12. esac13. alias ll='ls -l'14. alias lL='ls -L'

15. alias la='ls -a'16. alias lc='ls -c'17. alias lt='ls -t'18. alias lcr='lc -r'19. alias ltr='lt -r'20. alias lla='ll -a'21. alias llc='ll -c'22. alias lls='ll -s'23. alias llt='ll -t'24. alias llL='ll -L'25. alias llcr='llc -r'26. alias lltr='llt -r'27. alias lss='ls -s'28. alias dir='ls'

Page 37: zsh とわたし

僕の .zaliases (3)1. alias diff='diff -u'

2.  

3. alias rm='rm -i'

4. for i in $fpath; do [[ -f $i/my-rm ]] && alias rm='my-rm' && break; done

5. alias cp='cp -i'

6. alias mv='mv -i'

7.  

8. alias lo='exit'

9. alias cleartmp='/bin/rm -rf ~/tmp/*(ND) &'

10.alias so='source'

11.alias screen='screen -h 200'

12.alias resume='screen -x'

13.alias D='/bin/pwd'

14.alias -g LL='|& L'

Page 38: zsh とわたし

僕の .zaliases (4)1. case "${TERM}" in

2. kterm|xterm|screen|screen-bce|cygwin)

3. function precmd() {

4. # title

5. echo -n "\\e]2;${host} : $PWD\\007"

6. # icon

7. echo -n "\\e]1;${host} : $PWD\\007"

8. }

9. ;;

10. screen)

11. function precmd() {

12. echo -ne "\033_${WinNum}${host}(${WINDOW}) : $PWD";

13. echo -ne "\033\\"

14. }

15. ;;

16. esac

Page 39: zsh とわたし

僕の .zshrc-darwin1. # $Id: .zshenv-darwin,v 1.1 2013/02/10

12:44:04 toshi Exp $

2. #

3. #setopt XTRACE

4. case "$-" in

5. *i*) # this is interactive shell

6. echo `LANG=C date` : reading .zshenv-darwin

7. esac

8.  

9. PAGER=lv

10. PATH=\

11. ${HOME}/.rbenv/shims:\

12. /usr/local/bin:\

13. `xcode-select --print-path`/usr/bin:\

14. /Applications/Ghostscript.app/bin:\

15. /Applications/eclipse/android/platform-tools:\

16. ${PATH}

Page 40: zsh とわたし

zshrc 考察•環境依存性の切り分けが悩ましい• OS依存の設定•ホスト依存の設定•今や使わなくなった多数の alias, function その他をどうするべきか

Page 41: zsh とわたし

zlogin• login shell 時に zshrc より後に実行•何に使ってます?•昔はパスワード記憶系エージェントの起動に使ってたけど…

Page 42: zsh とわたし

僕の .zlogin1. # $Id: .zlogin,v 1.13 2009/07/05 02:33:55 toshi Exp $

2. #

3. #setopt XTRACE

4. echo `LANG=C date` : reading .zlogin

5.

6. MOTD=~/etc/motd

7. if [ -r ${MOTD} ]; then

8. echo -=-=-=- motd -=-=-

9. cat ${MOTD}

10. echo -=-=-=- motd -=-=-

11. fi

Page 43: zsh とわたし

zlogin 考察

•今どき login shell と、非 login shell を区別する意味はあるの?

•教えて偉い人

Page 44: zsh とわたし

なにか忘れてる気が

Page 45: zsh とわたし

zlogout !!

• login shell 終了時

Page 46: zsh とわたし

僕の .zlogout1. # $Id: .zlogout,v 1.1 1993/03/09 04:56:17 toshi Exp $

2. #

3. LANG=

4. echo "`hostname`: `whoami` logged out at `date`"

Page 47: zsh とわたし

zlogout

•唯一、最初の check in から全く変更なし

•ステキな使い方募集中

Page 48: zsh とわたし

まとまらないまとめ

• zsh はステキ