読者です 読者をやめる 読者になる 読者になる

鹿スタディーズ

越谷市科学技術体験センター ミラクル友の会 会員

いまさら zsh 入門

zsh… とかいう、 bash より便利なシェルがあるらしい、ってうわさは知ってた。oh-my-zsh とかいうやつが便利らしいよ、って聞いて入れてみたこともあったけど、なにがなんだかわからないまま入れたので、よくわからなくてそのまま bash を使っていた。

MacBook 買ったし、心機一転 bash から zsh に変えてみようと思ってやってみました (macOS Sierra 10.12.1)

$ brew install zsh

Homebrew (1.0.9) で zsh が /usr/local/Cellar/zsh/5.2 にインストールされる。

$ sudo vi /etc/shells

で、最後に 1 行 zsh のパスを追加する。

  /bin/bash
  /bin/csh
  /bin/ksh
  /bin/sh
  /bin/tcsh
  /bin/zsh
+ /usr/local/bin/zsh

保存したら、

$ chsh -s /usr/local/bin/zsh

で、ログインシェルが zsh になった!

起動すると、なんか英語でいわれる。 .zshenv .zprofile .zshrc .zlogin のどれもないときに表示されるらしい。親切〜

zsh-newuser-install に身を委ねて、適当に 1 とか 3 とか入力していく…

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

--- Type one of the keys in parentheses --- 1
Attempting to extract information from manual pages...
Please pick one of the following options:

(1)  Configure settings for history, i.e. command lines remembered
     and saved by the shell.  (Recommended.)

(2)  Configure the new completion system.  (Recommended.)

(3)  Configure how keys behave when editing command lines.  (Recommended.)

(4)  Pick some of the more common shell options.  These are simple "on"
     or "off" switches controlling the shell's features.

(0)  Exit, creating a blank ~/.zshrc file.

(a)  Abort all settings and start from scratch.  Note this will overwrite
     any settings from zsh-newuser-install already in the startup file.
     It will not alter any of your other settings, however.

(q)  Quit and do nothing else.  The function will be run again next time.
--- Type one of the keys in parentheses --- 1
History configuration
=====================

# (1) Number of lines of history kept within the shell.
HISTSIZE=1000                                                   (not yet saved)
# (2) File where history is saved.
HISTFILE=~/.histfile                                            (not yet saved)
# (3) Number of lines of history to save to $HISTFILE.
SAVEHIST=1000                                                   (not yet saved)

# (0)  Remember edits and return to main menu (does not save file yet)
# (q)  Abandon edits and return to main menu

--- Type one of the keys in parentheses --- 1
Variable       HISTSIZE
          The maximum number of events  stored  in  the  internal  history
          list.   If  you  use  the HIST_EXPIRE_DUPS_FIRST option, setting
          this value larger than the SAVEHIST size will give you the  dif-
          ference as a cushion for saving duplicated history events.

Edit a value.  If it is left blank, nothing will be saved:
HISTSIZE> 10000
History configuration
=====================

# (1) Number of lines of history kept within the shell.
HISTSIZE=10000                                              (set but not saved)
# (2) File where history is saved.
HISTFILE=~/.histfile                                            (not yet saved)
# (3) Number of lines of history to save to $HISTFILE.
SAVEHIST=1000                                                   (not yet saved)

# (0)  Remember edits and return to main menu (does not save file yet)
# (q)  Abandon edits and return to main menu

--- Type one of the keys in parentheses --- 3
Variable       SAVEHIST
          The  maximum  number  of  history  events to save in the history
          file.

Edit a value.  If it is left blank, nothing will be saved:
SAVEHIST> 10000
History configuration
=====================

# (1) Number of lines of history kept within the shell.
HISTSIZE=10000                                              (set but not saved)
# (2) File where history is saved.
HISTFILE=~/.histfile                                            (not yet saved)
# (3) Number of lines of history to save to $HISTFILE.
SAVEHIST=10000                                              (set but not saved)

# (0)  Remember edits and return to main menu (does not save file yet)
# (q)  Abandon edits and return to main menu

--- Type one of the keys in parentheses --- 0
Please pick one of the following options:

(1)  Configure settings for history, i.e. command lines remembered
     and saved by the shell.  (Unsaved changes.)

(2)  Configure the new completion system.  (Recommended.)

(3)  Configure how keys behave when editing command lines.  (Recommended.)

(4)  Pick some of the more common shell options.  These are simple "on"
     or "off" switches controlling the shell's features.

(0)  Exit, saving the new settings.  They will take effect immediately.

(a)  Abort all settings and start from scratch.  Note this will overwrite
     any settings from zsh-newuser-install already in the startup file.
     It will not alter any of your other settings, however.

(q)  Quit and do nothing else.  The function will be run again next time.
--- Type one of the keys in parentheses --- 2
The new completion system (compsys) allows you to complete
commands, arguments and special shell syntax such as variables.  It provides
completions for a wide range of commonly used commands in most cases simply
by typing the TAB key.  Documentation is in the zshcompsys manual page.
If it is not turned on, only a few simple completions such as filenames
are available but the time to start the shell is slightly shorter.

You can:
  (1)  Turn on completion with the default options.

  (2)  Run the configuration tool (compinstall).  You can also run
       this from the command line with the following commands:
        autoload -Uz compinstall
        compinstall
       if you don't want to configure completion now.

  (0)  Don't turn on completion.

--- Type one of the keys in parentheses --- 1
Please pick one of the following options:

(1)  Configure settings for history, i.e. command lines remembered
     and saved by the shell.  (Unsaved changes.)

(2)  Use the new completion system.  (Unsaved changes.)

(3)  Configure how keys behave when editing command lines.  (Recommended.)

(4)  Pick some of the more common shell options.  These are simple "on"
     or "off" switches controlling the shell's features.

(0)  Exit, saving the new settings.  They will take effect immediately.

(a)  Abort all settings and start from scratch.  Note this will overwrite
     any settings from zsh-newuser-install already in the startup file.
     It will not alter any of your other settings, however.

(q)  Quit and do nothing else.  The function will be run again next time.
--- Type one of the keys in parentheses --- 3
Default editing configuration
=============================

The keys in the shell's line editor can be made to behave either
like Emacs or like Vi, two common Unix editors.  If you have no
experience of either, Emacs is recommended.  If you don't pick one,
the shell will try to guess based on the EDITOR environment variable.
Usually it's better to pick one explicitly.

# (1) Change default editing configuration
bindkey -e                                                      (not yet saved)

# (0)  Remember edits and return to main menu (does not save file yet)
# (q)  Abandon edits and return to main menu

--- Type one of the keys in parentheses --- 1
Pick a keymap (set of keys) to use when editing.
Type:
  (e) for Emacs keymap (recommended unless you are vi user)
  (v) for Vi keymap
  (n) not to set a keymap (allow shell to choose)
  (k) to keep the current setting, (e):
--- Type one of the keys in parentheses --- v
Default editing configuration
=============================

The keys in the shell's line editor can be made to behave either
like Emacs or like Vi, two common Unix editors.  If you have no
experience of either, Emacs is recommended.  If you don't pick one,
the shell will try to guess based on the EDITOR environment variable.
Usually it's better to pick one explicitly.

# (1) Change default editing configuration
bindkey -v                                                  (set but not saved)

# (0)  Remember edits and return to main menu (does not save file yet)
# (q)  Abandon edits and return to main menu

--- Type one of the keys in parentheses --- 0
Please pick one of the following options:

(1)  Configure settings for history, i.e. command lines remembered
     and saved by the shell.  (Unsaved changes.)

(2)  Use the new completion system.  (Unsaved changes.)

(3)  Configure how keys behave when editing command lines.  (Unsaved changes.)

(4)  Pick some of the more common shell options.  These are simple "on"
     or "off" switches controlling the shell's features.

(0)  Exit, saving the new settings.  They will take effect immediately.

(a)  Abort all settings and start from scratch.  Note this will overwrite
     any settings from zsh-newuser-install already in the startup file.
     It will not alter any of your other settings, however.

(q)  Quit and do nothing else.  The function will be run again next time.
--- Type one of the keys in parentheses --- 4
Common shell options
====================

The following are some of the shell options that are most often used.
The descriptions are very brief; if you would like more information,
read the zshoptions manual page (type "man zshoptions").

# (1) Change directory given just path.
# unsetopt autocd                                                (no value set)
# (2) Use additional pattern matching features.
# unsetopt extendedglob                                          (no value set)
# (3) Append new history lines instead of overwriting.
# unsetopt appendhistory                                         (no value set)
# (4) Unmatched patterns cause an error.
# setopt nomatch                                                 (no value set)
# (5) Beep on errors.
# setopt beep                                                    (no value set)
# (6) Immediately report changes in background job status.
# unsetopt notify                                                (no value set)

# (0) or (q)  Return to main menu (no changes made yet)

--- Type one of the keys in parentheses --- 0
Please pick one of the following options:

(1)  Configure settings for history, i.e. command lines remembered
     and saved by the shell.  (Unsaved changes.)

(2)  Use the new completion system.  (Unsaved changes.)

(3)  Configure how keys behave when editing command lines.  (Unsaved changes.)

(4)  Pick some of the more common shell options.  These are simple "on"
     or "off" switches controlling the shell's features.

(0)  Exit, saving the new settings.  They will take effect immediately.

(a)  Abort all settings and start from scratch.  Note this will overwrite
     any settings from zsh-newuser-install already in the startup file.
     It will not alter any of your other settings, however.

(q)  Quit and do nothing else.  The function will be run again next time.
--- Type one of the keys in parentheses --- 0
The function will not be run in future, but you can run
it yourself as follows:
  autoload -Uz zsh-newuser-install
  zsh-newuser-install -f

The code added to ~/.zshrc is marked by the lines
# Lines configured by zsh-newuser-install
# End of lines configured by zsh-newuser-install
You should not edit anything between these lines if you intend to
run zsh-newuser-install again.  You may, however, edit any other part
of the file.

ここまで口が半開きでやってると、こんな .zshrc ができた。

# Lines configured by zsh-newuser-install
HISTFILE=~/.histfile
HISTSIZE=10000
SAVEHIST=10000
bindkey -v
# End of lines configured by zsh-newuser-install
# The following lines were added by compinstall
zstyle :compinstall filename '/Users/shikakun/.zshrc'

autoload -Uz compinit
compinit
# End of lines added by compinstall

ぜんぜんよくわからないので、先輩たちが GitHub に公開している dotfiles を拝見する…

github.com

github.com

あと Qiita などを拝見する…

qiita.com

qiita.com

とりあえずこんな感じになりました!

export LANG=ja_JP.UTF-8
export TERM=xterm-256color
export EDITOR=vim
HISTFILE=~/.histfile
HISTSIZE=100000
SAVEHIST=100000

# autoload
#### 自動補完を有効にする
autoload -Uz compinit

#### 色をつける
autoload -Uz colors

#### バージョン管理ツールのステータスを表示する
autoload -Uz vcs_info

#### プロンプトを表示する前に関数を実行する
autoload -Uz add-zsh-hook

# setopt
#### 入力したコマンドが存在せず、かつディレクトリ名と一致するなら、ディレクトリに cd する
setopt auto_cd

#### cd したディレクトリをディレクトリスタック(履歴)に追加する
#### 呼び出すときは `cd +<Tab>`
setopt auto_pushd
setopt pushd_ignore_dups # すでにスタックに含まれていればディレクトリスタックに追加しない

#### 入力したコマンドがすでにコマンド履歴に含まれるとき、履歴から古いほうのコマンドを削除する
setopt hist_ignore_all_dups

#### プロンプト表示ごとに変数を展開する
setopt PROMPT_SUBST

# zstyle
#### 補完で小文字でも大文字にマッチさせる
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'

#### vcs_info
zstyle ':vcs_info:*' formats '%b'

# 色をつける
#### ls
export LSCOLORS=gxfxcxdxbxegedabagacad
export LS_COLORS='di=36:ln=35:so=32:pi=33:ex=31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30'
alias ls='ls -G'
zstyle ':completion:*' list-colors 'di=36' 'ln=35' 'so=32' 'ex=31' 'bd=46;34' 'cd=43;34'

# コマンドプロンプトをカスタマイズする
#### 左
PROMPT='%F{cyan}%~%f %# '

#### 右 (Git)
_vcs_precmd () { vcs_info }
add-zsh-hook precmd _vcs_precmd
RPROMPT='%F{green}${vcs_info_msg_0_}%f'

# エイリアス
alias a='atom'
alias g='git'
alias o='open'
alias z='source ~/.zshrc'