[Mac]Terminalをカスタマイズする


はじめに

  • 現在使っているMacのTerminal関連のカスタマイズ設定メモ
  • 下記を実施している

    1. Terminalの見た目の変更
    2. Shellの変更(zsh)
    3. Terminalの縦分割を有効化
  • 本記事は、ほぼ下記の先人たちの知恵を借用してます

Before

After

1. Terminalの見た目の変更

  • icebergというテーマがかっこ良さげなのでそれに変更する
  • 公式サイトからダウンロードしてローカルに保存する
  • Terminalを立ち上げ、「環境設定」 > 「プロファイル」からダウンロードしたiceberg.terminalを読み込む
  • 読み込んだicebergをデフォルトテンプレートに設定する
  • ついでに、背景を気持ちばかり透明にしてかっこ良くする

  • ここまでで、こんな感じになる

2. Shellの変更(zsh)

  • 長らくbashを使ってきたが、最近はzshが流行りらしいので変更してみた

2.1. zshのインストール

$ brew install zsh

2.2. zsh-completionのインストール

  • zshの補完機能を強化するzsh-completionをインストールする
$ brew install zsh-completion

2.3. zshを起動する

  • 下記コマンドでzshを起動して設定を行っていく
$ zsh
  • 以下の表示が出た場合は1をタイプする
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 --- 
  • 続けて、以下のような表示になるため、0をタイプする
lease 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 ---
  • そうすると、コメントのみの~/.zshrcが作成される
  • 後ほどPreztoをインストールする際にエラーになるので予め削除しておく)
$ rm ~/.zshrc

2.4. Preztoをインストールする

  • zshの設定フレームワークである、Preztoをインストールする(参考: Prezto)
  • GitHubにあるReadmeを見ながら設定を行っていく

1: git cloneでモジュールをダウンロード

$ git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"

2: 提供されているzsh設定ファイルをコピーして、新しいzsh設定ファイルを作成する

$ setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
  ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done

3: 下記コマンドを実行し、zsh起動時にzsh-completionを有効にする

$ echo 'fpath=(/usr/local/share/zsh-completions $fpath)' >> .zshrc

2.5. zshをログインシェルとして設定する

  • /etc/shells/にエントリを追加し、zshをログインシェルとして利用可能なように設定する
$ sudo vi /etc/shells
  • 一番下の行に、/usr/local/bin/zshを追加する
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/zsh  # <-これを追記
  • その後、下記コマンドを実行してzshをデフォルトシェルとして設定する
$ chsh -s /usr/local/bin/zsh

2.6. .bash_profile,.bashrcの設定を引き継ぐ

  • 下記コマンドを実行して、bashの設定をzshに引き継ぐ
    shell
    $ cat .bash_profile >> .zprofile
    $ cat .bashrc >> .zshrc

  • source コマンドで反映する

$ source 

2.7. zshのプロンプトを変更する

  • デフォルトのプロンプトは不要な情報が多く、かつプロンプトと入力ラインの間に改行が欲しいためプロンプトを変更する
  • Pureというプロンプトがzsh向けに開発されているため、それを利用する
  • Preztoの設定ファイル~/.zpreztorcを変更することでプロンプトをpureに変更することが可能
$ vi ~/.zpreztorc
  • zstyle ':prezto:module:prompt' theme 'sorin' の theme の箇所を pure にする
~/.zpreztorc
・・・
# Set the prompt theme to load.
# Setting it to 'random' loads a random theme.
# Auto set to 'off' on dumb terminals.
zstyle ':prezto:module:prompt' theme 'pure'
・・・

2.8. 補完機能とシンタックスハイライトを有効にする

  • syntax-highlightingと、autosuggestionsを追記して、補完とシンタックスハイライトを有効にする
~/.zpreztprc
# Set the Prezto modules to load (browse modules).
# The order matters.
zstyle ':prezto:load' pmodule \
  'environment' \
  'terminal' \
  'editor' \
  'history' \
  'directory' \
  'spectrum' \
  'utility' \
  'completion' \
  'syntax-highlighting' \
  'autosuggestions' \
  'prompt'

2.9. 設定を有効にする

  • sourceコマンドで設定を有効にしてTerminalを開きなおす
  • ログインシェルとしてzshが起動してプロンプトも変更されていることが確認できる
$ source ~/.zpreztorc

3. Terminalの縦分割を有効化

  • Macデフォルトでは、Command + Dで横分割されるが、個人的には縦分割の方が使いやすい
  • バージョン4.01以降のscreenで実現できるとのことなので、やってみる
  • 初期状態ではバージョンが4.00.03のためbrewでアップデートする
$ screen -v
Screen version 4.00.03 (FAU) 23-Oct-06
  • インストール完了後、下記コマンドを実行してscreenを実行する
$ /usr/local/Cellar/screen/4.8.0/bin/screen
  • screenが起動できたら、下記順番にキー入力を行い画面分割を行う
  • screenでは基本的にctrl + aがコマンド入力開始となる
    • 1: 画面を縦に分割 -> Ctrl + a + |(パイプ)
    • 2: 分割した画面に移動 -> Ctrl + a + Tab
    • 3: 分割した画面側でShellを起動 -> Ctrl + a + c
  • 上記を実施すると下記のように縦分割できる。移動するときは、Ctrl + a + Tabで移動できる

4. screenの諸々設定

  • デフォルトではscreen.zprofileが読み込まれなかったり、不要なメッセージが出たりで使いにくい
  • またデフォルトのEscape Keyが、Ctrl + aとなっており、行頭移動のコマンドと被っているので変更したい
  • ~/.screenrcファイルを作成し必要な設定を書き込む
~/.screenrc
# character-code
defutf8 on
defencoding utf8
encoding utf8 utf8

# Disable undesired message
startup_message off
vbell off

# Chane Escape key
escape ^Bb

# Default shell
shell -$SHELL

# Change preference collor
defbce on
term xterm-256color

# Delete buffer for vim and less
altscreen on

# Status line
hardstatus alwayslastline "%{= cd} %-w%{= wk} %n %t* %{-}%+w %= LoadAVG [%l] "

# Num of scroll line
defscrollback 10000

# Enable mouse scroll
termcapinfo xterm* ti@:te@
  • 以上を行い、次にscreenを立ち上げると上記が有効になった状態でscreenが起動される

参考

テーマ変更関連
- お前らのターミナルはダサい
- 気分転換にTerminal.appの配色を変えてみる?暗青系のテーマ「Iceberg」を移植しました

zsh関連
- Macでbashからzshへお引越しする方法
- Prezto
- Pure

Screen関連
- 黒縁眼鏡は海を飛ぶ - macにてscreenを縦分割する
- MacのTerminalのScreenを縦分割可能にする
- screenで画面を分割する方法(縦,横)と分割後のショートカットキー
- .screenrcにこれだけは設定しとけっていうオススメ設定
- 仮想端末screenを使う