tmu 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env bash
  2. TERMCMD=${TERMCMD:-urxvto}
  3. PROGNAME=$(basename $0)
  4. OPTSTRING="${PROGNAME} (-x) [ -h | -l | -L host | -z | -c name | -n name | -N host | -e name tab command ]"
  5. if [[ $# -eq 0 ]] ; then
  6. echo ${OPTSTRING} >&2
  7. exit 1
  8. fi
  9. XWINFLAG=false
  10. while getopts "c:n:e:L:N:zlxh" OPT
  11. do
  12. # shift
  13. case "$OPT" in
  14. x)
  15. XWINFLAG=true
  16. ;;
  17. z)
  18. NAME=$(tmux list-sessions -F '#S' | grep '^term' | sort -r | head -n 1)
  19. if [ -n "${NAME}" ] ; then
  20. NUM=$(echo $NAME | cut -d- -f 2)
  21. NUM=$(( NUM + 1 ))
  22. NAME="term-${NUM}"
  23. else
  24. NAME=term-0
  25. fi
  26. ${TERMCMD} -e tmux new -s "${NAME}"
  27. ;;
  28. l)
  29. if [[ $XWINFLAG != "true" ]] ; then
  30. tmux list-sessions -F '#S'
  31. else
  32. if [[ -e `which rofi` ]] ; then
  33. NAME=`tmux list-sessions -F '#S' | rofi -dmenu -p "screen session"`
  34. else
  35. NAME=`tmux list-sessions -F '#S' | dmenu -p "screen session:"`
  36. fi
  37. [[ $NAME != "" ]] && ${TERMCMD} -e tmux attach -t $NAME
  38. fi
  39. ;;
  40. L)
  41. if [[ $XWINFLAG != "true" ]] ; then
  42. tmux list-sessions -F '#S' | grep "${OPTARG}"
  43. else
  44. if [[ -e `which rofi` ]] ; then
  45. NAME=`tmux list-sessions -F '#S' | grep "${OPTARG}" | rofi -dmenu -p "screen session"`
  46. else
  47. NAME=`tmux list-sessions -F '#S' | grep "${OPTARG}" | dmenu -p "screen session:"`
  48. fi
  49. [[ $NAME != "" ]] && ${TERMCMD} -e tmux attach -t $NAME
  50. fi
  51. ;;
  52. c)
  53. if [[ $XWINFLAG != "true" ]] ; then
  54. tmux attach -t "${OPTARG}"
  55. else
  56. urxtvo -e "tmux attach -t \"${OPTARG}\""
  57. fi
  58. ;;
  59. n)
  60. if [[ $XWINFLAG != "true" ]] ; then
  61. tmux new -s "${OPTARG}"
  62. else
  63. ${TERMCMD} -e tmu -n "${OPTARG}"
  64. fi
  65. ;;
  66. N)
  67. NAME=$(tmux list-sessions -F '#S' | grep "^${OPTARG}" | sort -r | head -n 1)
  68. if [ -n "${NAME}" ] ; then
  69. NUM=$(echo $NAME | cut -d- -f 2)
  70. NUM=$(( NUM + 1 ))
  71. NAME="${OPTARG}-${NUM}"
  72. else
  73. NAME="${OPTARG}-0"
  74. fi
  75. tmux new -s "${NAME}"
  76. ;;
  77. e)
  78. OPTIND1=$(( OPTIND + 1 ))
  79. TAB=${!OPTIND}
  80. OPTIND1=$(( OPTIND + 1 ))
  81. COMMAND=${!OPTIND1}
  82. if tmux has-session -t "${OPTARG}" ; then
  83. TMUX='' tmux attach-session -t "${OPTARG}" \; new-window -d -n "${TAB}" "${COMMAND}" \; detach-client
  84. else
  85. TMUX='' tmux new -s "${OPTARG}" -n "${TAB}" -d "${COMMAND}"
  86. fi
  87. exit
  88. ;;
  89. h)
  90. echo ${OPTSTRING} >&2
  91. cat <<HELP >&2
  92. -h Show this help.
  93. -x Turn on X-windows interaction.
  94. -z Create a new, unnamed screen session with X Terminal.
  95. -l List screen sessions (with -x, show in ROFI).
  96. -l HOST List sessions on HOST.
  97. -c NAME Connect (as an additional screen) to session "name".
  98. -n NAME Create and connect to a new screen session "name".
  99. -N HOST Create and connect to a new numbered screen session for HOST.
  100. -e NAME TAB COMMAND
  101. Create a tab named "tab" running the command "command" in session "name".
  102. HELP
  103. exit
  104. ;;
  105. esac
  106. done