shosei

tmx.sh

Mar 12th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.70 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Modified TMUX start script from:
  5. #     http://forums.gentoo.org/viewtopic-t-836006-start-0.html
  6. #
  7. # Store it to `~/bin/tmx` and issue `chmod +x`.
  8. #
  9.  
  10. # Works because bash automatically trims by assigning to variables and by
  11. # passing arguments
  12. trim() { echo $1; }
  13.  
  14. if [[ -z "$1" ]]; then
  15.     echo "Specify session name as the first argument"
  16.     exit
  17. fi
  18.  
  19. # Only because I often issue `ls` to this script by accident
  20. if [[ "$1" == "ls" ]]; then
  21.     tmux ls
  22.     exit
  23. fi
  24.  
  25. base_session="$1"
  26. # This actually works without the trim() on all systems except OSX
  27. tmux_nb=$(trim `tmux ls | grep "^$base_session" | wc -l`)
  28. if [[ "$tmux_nb" == "0" ]]; then
  29.     echo "Launching tmux base session $base_session ..."
  30.     tmux new-session -s $base_session
  31. else
  32.     # Make sure we are not already in a tmux session
  33.     if [[ -z "$TMUX" ]]; then
  34.         # Kill defunct sessions first
  35.         old_sessions=$(tmux ls 2>/dev/null | egrep "^[0-9]{14}.*[0-9]+\)$" | cut -f 1 -d:)
  36.         for old_session_id in $old_sessions; do
  37.             tmux kill-session -t $old_session_id
  38.         done
  39.  
  40.         echo "Launching copy of base session $base_session ..."
  41.         # Session is is date and time to prevent conflict
  42.         session_id=`date +%Y%m%d%H%M%S`
  43.         # Create a new session (without attaching it) and link to base session
  44.         # to share windows
  45.         tmux new-session -d -t $base_session -s $session_id
  46.         if [[ "$2" == "1" ]]; then
  47.         # Create a new window in that session
  48.         tmux new-window
  49.     fi
  50.         # Attach to the new session
  51.         tmux attach-session -t $session_id
  52.         # When we detach from it, kill the session
  53.         tmux kill-session -t $session_id
  54.     fi
  55. fi
Advertisement
Add Comment
Please, Sign In to add comment