Skip to content

Instantly share code, notes, and snippets.

@yonchu
Created October 23, 2012 00:51
Show Gist options
  • Save yonchu/3935972 to your computer and use it in GitHub Desktop.
Save yonchu/3935972 to your computer and use it in GitHub Desktop.
Macのクリップボード対応tmuxコマンド(tmuxx)とtmuxの自動起動設定
#!/bin/bash
#
# 1. このスクリプトをPATHの通った所(~/bin など)に置き、実行権限を与える(chmod +x tmux-pbcopy)
# 2. ~/.tmux.conf に以下のようにキーバインドを設定して使用
# bind ^y run-shell 'tmux-pbcopy'
#
if ! type pbcopy >/dev/null 2>&1 || ! type reattach-to-user-namespace >/dev/null 2>&1; then
tmux display-message "Error: cannot copy to clipboard."
exit 0
fi
COPY=$(tmux save-buffer -)
echo "$COPY" | reattach-to-user-namespace pbcopy
# メッセージ表示
LINES=$(echo "$COPY" | wc -l | tr -d ' ')
tmux display-message "Copy: $LINES lines"
# tmux/screenの自動起動設定
# Note: .bashrc or .zshrc に設定して使用して下さい。
#
# ログイン時にtmux または screenが起動してない場合は自動的に起動
# デタッチ済みセッションが存在すればアタッチし、なければ新規セッションを生成
# tmuxを優先して起動し、tmuxが使えなければscreenを起動する
#
if [ -z "$TMUX" -a -z "$STY" ]; then
if type tmuxx >/dev/null 2>&1; then
tmuxx
elif type tmux >/dev/null 2>&1; then
if tmux has-session && tmux list-sessions | egrep -q '.*]$'; then
# デタッチ済みセッションが存在する
tmux attach && echo "tmux attached session "
else
tmux new-session && echo "tmux created new session"
fi
elif type screen >/dev/null 2>&1; then
screen -rx || screen -D -RR
fi
fi
#!/bin/bash
# attach to an existing tmux session, or create one if none exist
# also set up access to the system clipboard from within tmux when possible
#
# e.g.
# https://gist.github.com/1462391
# https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard
if ! type tmux >/dev/null 2>&1; then
echo 'Error: tmux command not found' 2>&1
exit 1
fi
if [ -n "$TMUX" ]; then
echo "Error: tmux session has been already attached" 2>&1
exit 1
fi
if tmux has-session >/dev/null 2>&1 && tmux list-sessions | grep -qE '.*]$'; then
# detached session exists
tmux attach && echo "tmux attached session "
else
if [[ ( $OSTYPE == darwin* ) && ( -x $(which reattach-to-user-namespace 2>/dev/null) ) ]]; then
# on OS X force tmux's default command to spawn a shell in the user's namespace
tmux_config=$(cat $HOME/.tmux.conf <(echo 'set-option -g default-command "reattach-to-user-namespace -l $SHELL"'))
tmux -f <(echo "$tmux_config") new-session && echo "tmux created new session supported OS X"
else
tmux new-session && echo "tmux created new session"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment