Usage
Instead of tmux just type
1 |
start_tmux |
Installation
Get the code from github, and append it to your .zshrc or .bashrc.
To do it in one step, paste in zsh
1 |
wget https://raw.githubusercontent.com/nachoparker/smart-tmux-sessions/master/start_tmux.sh -O - >> ~/.zshrc |
, or in bash
1 |
wget https://raw.githubusercontent.com/nachoparker/smart-tmux-sessions/master/start_tmux.sh -O - >> ~/.bashrc |
New shell sessions will automatically start or resume a tmux session. Comment last line of .zshrc or .bashrc after installation if you do not want this.
Details
The following shell function is my preferred way of starting a shell tmux session.
Tmux consists of a server-client architecture. This is great, because if your terminal is closed, you can later come back and resume exactly where you left off.
One catch of this is that if you are not careful, you can end up with numerous orphan tmux sessions where work is halfway through, vim sessions are still open and so on, and you inadvertedly just open yet another one.
This function simply makes sure that there are no orphan tmux sessions running. If so, it attaches to them.
Upon exiting the shell session, other orphan sessions will be restored, until aren’t any left.
Should you want to leave a session “as is” to come back later, just close the terminal ( or tmux kill-pane ^Ax ). I find this useful for SSH if one includes start_tmux in your .zshrc or .bashrc script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# Simple, convenient way of starting and restoring tmux sessions # # Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com> # GPL licensed (see end of file) * Use at your own risk! # # I source this function as part of my .bashrc or .zshrc # I also run 'start_tmux' at the end of my .bashrc/.zshrc in all my servers # # Usage: # start_tmux # instead of just 'tmux' # # Notes: # It attaches to existing orphan tmux sessions upon exiting current tmux session function start_tmux() { test -z ${TMUX} || return # fail if already in a TMUX session while true; do tmux ls -F "#{session_id}:#{session_attached}" 2>/dev/null | \ while read l; do # search for unused sessions local ID=$( echo $l | cut -f1 -d: ) local NUM=$( echo $l | cut -f2 -d: ) test "$NUM" != "0" || break; ID="" done if [[ "$ID" != "" ]]; then # use unused sessions,if they exist tmux attach -t $( echo $ID | sed 's=\$==' ) local ID="" else tmux break fi done } # License # # This script is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This script is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this script; if not, write to the # Free Software Foundation, Inc., 59 Temple Place, Suite 330, # Boston, MA 02111-1307 USA |