0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ログイン時にtmuxセッションを選択する

Posted at

動機

Windowsからrloginで画面分割して同じサーバで作業するときに、楽にtmuxのセッションを選択したい。bashで

要件

  • ログイン時に、セッション一覧を選択できる
  • セッションがない場合、新しくセッションを作成する
  • セッションがある場合でも新しくセッションを作成する
  • セッションを作成するときには名前を付ける

成果物

create_tmux_session.sh
create_new_session(){
  echo "session name>"
  read session_name
  tmux new-session -s $session_name
}

select_tmux_session(){
if [ -z $TMUX ]
then
  IFS_BACKUP=$IFS
  IFS=$'\n'
  ID=$(tmux ls)
  if [ -z "$ID" ]
  then
    create_new_session
    exit 0
  fi
  select item in $ID
  do
    case "$REPLY" in
      n )
        create_new_session
        break;;
      * ) echo "you select $item ($REPLY)"
        tmux attach -t $(echo $item | awk -F: '{print $1}')
        IFS=$IFS_BACKUP
        break;;
    esac
  done
fi
}

素直に書いた。これを.bashrcに書いておくだけ

結果

便利

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?