tmux-detach-all-but-current-client 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/sh
  2. #
  3. # tmux-detach-all-but-current-client
  4. # Copyright (C) 2013-2014 Dustin Kirkland
  5. #
  6. # Authors: Dustin Kirkland <kirkland@byobu.org>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, version 3 of the License.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. a1=
  20. a2=
  21. tty1=
  22. tty2=
  23. # List all clients, ordered by most recent activity descending
  24. for c in $(tmux list-clients -F "#{client_activity}___#{client_tty}" | sort -n -r); do
  25. if [ -z "$a1" ]; then
  26. a1=${c%%___*}
  27. tty1=${c##*___}
  28. elif [ -z "$a2" ]; then
  29. a2=${c%%___*}
  30. tty2=${c##*___}
  31. fi
  32. if [ -n "$a1" ] && [ -n "$a2" ]; then
  33. if [ "$a1" = "$a2" ]; then
  34. # Activity timestamps match in top 2 attached clients
  35. # Let's not detach anyone here!
  36. tmux display-message "Multiple active attached clients detected, refusing to detach" >/dev/null 2>&1
  37. elif [ -n "$tty1" ]; then
  38. # Detach all but the current client, iterating across each
  39. # Tempting to use detach-client -a -t here, but there's a bug
  40. # in there, keeping that from working properly
  41. tmux detach-client -t "$tty2" >/dev/null 2>&1
  42. a2=
  43. fi
  44. fi
  45. done