tmx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/sh
  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. function check_session() {
  11. local postfix="$1"
  12. shift
  13. "${tmux[@]}" -L "session-$postfix" list-sessions -F "#S" 2>/dev/null | grep -q "$base_session-$postfix"
  14. }
  15. tmux=( "tmux" )
  16. base_session="$USER"
  17. if [[ "$XDG_SESSION_ID" ]] && check_session "$XDG_SESSION_ID"; then
  18. tmux+=( "-L" "session-$XDG_SESSION_ID" )
  19. base_session+="-$XDG_SESSION_ID"
  20. fi
  21. # ------------------------------------------------------------------------------
  22. new_session="${base_session}-$$"
  23. if (( $("${tmux[@]}" ls | wc -l) == 0 )); then
  24. exec "${tmux[@]}" new -s "$new_session"
  25. else
  26. # Count tmux sessions (1 master + N slaves).
  27. # If there are no slave sessions, do not create new windows.
  28. if (( $("${tmux[@]}" ls | wc -l) > 1 )); then
  29. NEW_WINDOW_COMMAND=("new-window" "$*" ";")
  30. fi
  31. # Create a new session (without attaching it) and link to base session to share windows
  32. # Attach to the new session & kill it once orphaned
  33. exec "${tmux[@]}" new-session -d -t "$base_session" -s "$new_session" \; \
  34. attach-session -t "$new_session" \; \
  35. "${NEW_WINDOW_COMMAND[@]}" \
  36. set-option destroy-unattached
  37. fi