LoginSignup
1
3

More than 5 years have passed since last update.

iTerm2を使わずzsh + tmuxで4分割画面を手に入れた

Last updated at Posted at 2019-01-20

はじめに

何故かiTerm2というものを食わず嫌いしてしまう。なぜか憎い。iTerm2に親を殺された。iTerm2が元カノに似ている。でも画面分割してモテたいんだ!!!!っていう癖の強い人向けの記事です。とりあえず画面分割をサクッと実装して簡単にカッコよくなれるところを終着点としております。きっと多数派のiTerm2を使わないで、tmuxで画面分割していたほうがモテるだろうという安易な考えから来ております。多分tmuxはssh接続等で便利だと思うのですが、ただカッコつけて画面分割したいだけなので今回はssh接続に関しては触れません。あとあまり触れてませんがpecoいいです。多分モテるので是非調べてみてください。

zsh

  • version 6.2.1

導入

install
$ brew install zsh
ログインシェル変更
$ chsh -s /usr/local/bin/zsh 

~/zshrc

source ~/.zplug/init.zsh

zplug "wbinglee/zsh-wakatime"

# syntax
zplug "chrissicool/zsh-256color"
zplug "Tarrasch/zsh-colors"
zplug "zsh-users/zsh-syntax-highlighting"
zplug "ascii-soup/zsh-url-highlighter"

# program
zplug "voronkovich/mysql.plugin.zsh"

# tools
zplug "marzocchi/zsh-notify"
zplug "oknowton/zsh-dwim"

# =====以下必要に応じて=====
# peco(履歴表示ツール用)
function peco-history-selection() {
    BUFFER=`history -n 1 | tail -r  | awk '!a[$0]++' | peco`
    CURSOR=$#BUFFER
    zle reset-prompt
}

zle -N peco-history-selection
bindkey '^R' peco-history-selection

# rbenv(rubyのversion管理ツール) PATH 
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

# tmux 自動分割 tmuxって打つだけで4分割画面を表示する。後述
alias tmux="tmux -2 attach || tmux -2 new-session \; source-file ~/.tmux/new-session"

# zsh起動時にtmux起動 いつも4分割したいわけじゃないからコメントアウト
# [[ -z "$TMUX" && ! -z "$PS1" ]] && exec tmux

~/zsh_profile

# 補完機能
# 補完機能
autoload -U compinit
compinit

# prompt
PROMPT='%m:%c %n$ '

# コマンド履歴
HISTFILE=~/.zsh_history
HISTSIZE=6000000
SAVEHIST=6000000
setopt hist_ignore_dups     # ignore duplication command history list
setopt share_history        # share command history data

# コマンド履歴検索
autoload history-search-end
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
bindkey "^P" history-beginning-search-backward-end
bindkey "^N" history-beginning-search-forward-end

# alias
alias diff="coloddiff"
alias relogin='exec $SHELL -l'
alias la='ls -a' # dot(.)で始まるディレクトリ、ファイルも表示
alias lal='ls -al' # -a オプションと -l オプションの組み合わせ
alias llv='ls -lav' 
alias ll='ls -l' # ファイルの詳細も表示
alias lla='ls -la' # -a オプションと -l オプションの組み合わせ
alias lsf='ls -F' # ディレクトリ名の末尾にはスラッシュ、シンボリックリンクの末尾には@というように種類ごとの表示をつけてくれる
alias ls='ls -v -G' # Gはアウトプットに色を付けてくれる 

#カレントディレクトリフォルダ名表示
iprecmd () {
  echo -ne "\e]2;${PWD}\a"
  echo -ne "\e]1;${PWD:t}\a"
}

tmux

  • version 2.8

導入

brew install tmux

~/.tmux.conf(設定ファイル)

# Homebrewでいれたzshの場合
set -g default-shell   /usr/local/bin/zsh
set -g default-command /usr/local/bin/zsh

# prefixキーをC-tに変更する(好み)
set -g prefix C-t

# C-bのキーバインドを解除する
unbind C-b

# キーストロークのディレイを減らす
set -sg escape-time 0

# ウィンドウのインデックスを1から始める
set -g base-index 1

# ペインのインデックスを1から始める
setw -g pane-base-index 1

# 設定ファイルをリロードする
#bind r source-file ~/.tmux.conf \; display "Reloaded!"

# C-a*2でtmux内のプログラムにC-aを送る
bind C-t send-prefix

# \ でペインを縦に分割する
bind \ split-window -h

# - でペインを横に分割する
bind - split-window -v

# Vimのキーバインドでペインを移動する
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+

# Vimのキーバインドでペインをリサイズする
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

# 256色端末を使用する
set -g default-terminal "screen-256color"

# ステータスバーの色を設定する
set -g status-fg white
set -g status-bg black

# ウィンドウリストの色を設定する
setw -g window-status-fg cyan
setw -g window-status-bg default
setw -g window-status-attr dim
# アクティブなウィンドウを目立たせる
setw -g window-status-current-fg white
setw -g window-status-current-bg red
setw -g window-status-current-attr bright

# ペインボーダーの色を設定する
set -g pane-border-fg green
set -g pane-border-bg black
# アクティブなペインを目立たせる
set -g pane-active-border-fg white
set -g pane-active-border-bg yellow

# コマンドラインの色を設定する
set -g message-fg white
set -g message-bg black
set -g message-attr bright

# ステータスバーを設定する
## 左パネルを設定する
set -g status-left-length 40
set -g status-left "#[fg=green]Session: #S #[fg=yellow]#I #[fg=cyan]#P"
## 右パネルを設定する
set -g status-right "#(tmux show-options -g prefix) #[fg=white][%Y-%m-%d(%a) %H:%M]"
## リフレッシュの間隔を設定する(デフォルト 15秒)
set -g status-interval 60
## ウィンドウリストの位置を中心寄せにする
set -g status-justify centre
## ヴィジュアルノーティフィケーションを有効にする
setw -g monitor-activity on
set -g visual-activity on
# マウススクロールを可能に
set -g mouse on
set -g terminal-overrides 'xterm*:smcup@:rmcup@'
# tpm
set -g @tpm_plugins '              \
  tmux-plugins/tpm                 \
  tmux-plugins/tmux-yank           \
  tmux-plugins/tmux-open           \
  tmux-plugins/tmux-resurrect      \
  tmux-plugins/tmux-battery        \
  tmux-plugins/tmux-pain-control   \
'

run-shell '~/.tmux/plugins/tpm/tpm'

~/.tmux/new-sesiion 4分割用(zshrcで設定したaliasに対応)

# 一番初めのpaneを選択
select-pane -t 0
# 上下にウィンドウを分割する
split-window -v 
split-window -h 
# 1番目(下側)のウィンドウを選択
select-pane -t 1 
# 左右にウィンドウを分割
split-window -h 

# それぞれにzsh設定ファイルを読み込ませる
# tmuxの場合local/.zprofileと.zsh_historyは読み込まないので読み込ませる。
send-keys -t 1 'source ~/.zprofile ~/.zsh_history' C-m
send-keys -t 2 'source ~/.zprofile ~/.zsh_history' C-m
send-keys -t 3 'source ~/.zprofile ~/.zsh_history' C-m
send-keys -t 4 'source ~/.zprofile ~/.zsh_history' C-m

要点

  • 基本的には各設定ファイルを正しく入れて頂けたら上記手順で動くと思います。(versionによる破壊的変更に注意)
  • macにはデフォルトのツールが多くbrewでinstallをしたものを見てくれない場合があるので、pathを気をつけましょう。
  • 設定ファイルを入れ直した場合再読込が必要となりますので、exec $SHELL -lをしてあげてください。※上記設定ファイルでalias reloginでも可能
  • tmuxでzshにログインした場合、zprofileとzsh_historyは読み込まれません。pecoとかaliasで困るので、.tmux/new-sessionで読み込みするように設定してあげてください。
  • tmux上の使用はPrefix + コマンドで操作が基本となります。上記設定ファイルだとPrefixがC+tに設定してあります。現在のPrefixはtmux画面右下に表示されるよう書き換えてます。
  • そもそもPrefix(C+t) + コマンドの押し方ですが、Controlとtを同時に押してControlだけ離してコマンドを押すという入力方法です。お恥ずかしながら僕はここで躓きました。

お願い

  • 間違いがあったらご指摘お願いします。
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3