From 32d8d5b7cd8d7d1803f527ca8b23373b48f51f3a Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Fri, 17 Nov 2017 14:01:56 -0300 Subject: [PATCH] Allow running inside another tmux session Generally, nesting tmux session can be dangerous because of recursive attachments (e.g.: a session attached to itself, even transitively). Since we create a new session here, this is not an issue. This changeset: * Keeps focus on the first split when splitting (the focused one will be killed later). * Kills the first split by name (otherwise, this might kill the tmux session in which we're running). * Unsets the TMUX env variable (so tmux will allow nesting). --- tmuxssh.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tmuxssh.py b/tmuxssh.py index 354689e..1427dd2 100755 --- a/tmuxssh.py +++ b/tmuxssh.py @@ -29,7 +29,7 @@ def tmux(self, *args): return subprocess.call(['tmux'] + list(args)) def split_window(self, command): - self.tmux('split-window', '-t', self._session_name, command) + self.tmux('split-window', '-dt', self._session_name, command) def select_layout(self, layout): self.tmux('select-layout', '-t', self._session_name, layout) @@ -41,7 +41,7 @@ def set_window_option(self, option, value): self.tmux('set-window-option', '-t', self._session_name, option, value) def kill_pane(self, n): - self.tmux('kill-pane', '-t', str(n)) + self.tmux('kill-pane', '-t', '{}:{}'.format(self._session_name, n)) def kill_session(self): self.tmux('kill-session', '-t', self._session_name) @@ -68,6 +68,9 @@ def main(): hosts = (h for host in args.get('') for h in host.split()) template = args.get('--template') or 'ssh {}' commands = (ssh_command(host, template) for host in hosts) + # Allow nesting inside another tmux session. + # (Should be safe,since we're creating a new session anyway). + os.environ.pop('TMUX') tmux_commands(commands) except EnvironmentError: print('You need to install tmux before using this script.')