Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. # Simple, convenient way of starting and restoring tmux sessions
  2. #
  3. # Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
  4. # GPL licensed (see end of file) * Use at your own risk!
  5. #
  6. # I source this function as part of my .bashrc or .zshrc (actually I do 'source .zsh_library.sh')
  7. # I normally also run 'start_tmux' at the end of my .bashrc or .zshrc in all my servers
  8. #
  9. # Usage:
  10. # start_tmux # instead of just 'tmux'
  11. #
  12. # Notes:
  13. # It attaches to existing orphan tmux sessions upon exiting current tmux session
  14. function start_tmux()
  15. {
  16. test -z ${TMUX} || return # fail if already in a TMUX session
  17. while true; do
  18. tmux ls -F "#{session_id}:#{session_attached}" 2>/dev/null | \
  19. while read l; do # search for unused sessions
  20. local ID=$( echo $l | cut -f1 -d: )
  21. local NUM=$( echo $l | cut -f2 -d: )
  22. test "$NUM" != "0" || break;
  23. ID=""
  24. done
  25. if [[ "$ID" != "" ]]; then # use unused sessions, if they exist
  26. tmux attach -t $( echo $ID | sed 's=\$==' )
  27. local ID=""
  28. else
  29. tmux
  30. break
  31. fi
  32. done
  33. }
  34.  
  35. # License
  36. #
  37. # This script is free software; you can redistribute it and/or modify it
  38. # under the terms of the GNU General Public License as published by
  39. # the Free Software Foundation; either version 2 of the License, or
  40. # (at your option) any later version.
  41. #
  42. # This script is distributed in the hope that it will be useful,
  43. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  45. # GNU General Public License for more details.
  46. #
  47. # You should have received a copy of the GNU General Public License
  48. # along with this script; if not, write to the
  49. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  50. # Boston, MA 02111-1307 USA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement