exit tmux, but retain everything

32 views
Skip to first unread message

Matt Zagrabelny

unread,
Oct 2, 2019, 11:09:41 AM10/2/19
to tmux-users
Greetings,

Is there a way to leave tmux, but somehow seed the environment (history, CWD, etc.) with everything that was part of the tmux environment?

Sort of like:

tmux
# do stuff
tmux --copy-environment-exec-shell
# now the shell is running without tmux

What do you folks think?

Thanks for any help!

-m

Nicholas Marriott

unread,
Oct 2, 2019, 3:45:30 PM10/2/19
to Matt Zagrabelny, tmux-users
Hi

tmux can't change the environment of other processes, including its
parent.

You could possibly do it yourself with a script that detaches tmux,
usese tmux showenv and pane_current_path to update the parent shell,
then kills tmux.

Alternatively it would possible to add a feature where tmux replaced
itself with a new shell with the updated environment but I don't see
much use for this so there is little chance of it being implemented.
> --
> You received this message because you are subscribed to the Google Groups
> "tmux-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to tmux-users+...@googlegroups.com.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/tmux-users/CAOLfK3V%2BVMY4br1fKfvXbjukAcdzV5pCAMjrHoq%2BuWkMqC%3D5nw%40mail.gmail.com.

Matt Zagrabelny

unread,
Oct 2, 2019, 4:26:04 PM10/2/19
to Nicholas Marriott, tmux-users
Thanks for the answers Nicholas!

Perhaps I should just state what I currently do and see if there is a more optimal way of achieving what I think I want.

Right now for all of my computers I have tmux spawn when a shell is exec'ed. It is very nice to always have tmux running whenever I am in a shell and to not have to type: "tmux".

The problem is when I ssh to other systems I then have two tmuxes running and I'd rather just have the one running on the remote system - so when I split a pane, I am doing all of my tmux commands on the "most remote" system and not have "more local" tmuxes interfere with the escape keys and not have to remember how many times I need to hit the escape sequence to get it to reach the "most remote" system.

For instance:

laptop (tmux) $ ssh workstation
laptop (tmux) workstation (tmux) $ ssh home_server
laptop (tmux) workstation (tmux) home_server (tmux) $

So that is three tmuxes I have running when really I just want:

laptop workstation home_server (tmux) $

Or I would like my escape key binding to (default to?) send 3 escape keys if I have three tmuxes running.

Any ideas or suggestions regarding my scenarios?

Thanks!

-m

Matt Zagrabelny

unread,
Oct 2, 2019, 10:35:04 PM10/2/19
to Scott Rochford, tmux-users
Hey Scott,

On Wed, Oct 2, 2019 at 5:44 PM Scott Rochford <annihi...@gmail.com> wrote:
Hi Matt,

Like you, I work on many systems, however I only run tmux on the "most local" system, and have key bindings set up to open fresh windows or panes which connect immediately to remote systems, or even multiple systems (e.g. a cluster) in a split window.

To try to understand your requirement better, can you explain what advantage (s) you find in running tmux on the most remote system? 

Sure!

System upgrades. Database dumps or creating indexes, etc. Rsyncs. Things that take a long time. I have had issues where my workstation would lock up and the controlling terminal would freeze and cause the remote process to die. So, I always like to have a tmux running where the most important process is running. This way the network or the workstation won't interfere with the completion of the job. 
 
Is it because you frequently need multiple windows/panes on the remote system that would be too  inconvenient to spawn more ssh connections for?

If I am working on a remote system, sometimes I want to run another command on that system and it would be nice to split the pane and do that, but the local tmux takes the key sequence to split the pane. Then I need to ssh. It would be nice to have my configuration "just know what I want."

Thanks,

-m
 

Scott

Nicholas Marriott

unread,
Oct 3, 2019, 7:07:17 AM10/3/19
to Matt Zagrabelny, tmux-users
On Wed, Oct 02, 2019 at 03:25:33PM -0500, Matt Zagrabelny wrote:
> Thanks for the answers Nicholas!
> Perhaps I should just state what I currently do and see if there is a more
> optimal way of achieving what I think I want.
> Right now for all of my computers I have tmux spawn when a shell is
> exec'ed. It is very nice to always have tmux running whenever I am in a
> shell and to not have to type: "tmux".
> The problem is when I ssh to other systems I then have two tmuxes running
> and I'd rather just have the one running on the remote system - so when I
> split a pane, I am doing all of my tmux commands on the "most remote"
> system and not have "more local" tmuxes interfere with the escape keys and
> not have to remember how many times I need to hit the escape sequence to
> get it to reach the "most remote" system.
> For instance:
> laptop (tmux) $ ssh workstation
> laptop (tmux) workstation (tmux) $ ssh home_server
> laptop (tmux) workstation (tmux) home_server (tmux) $
> So that is three tmuxes I have running when really I just want:
> laptop workstation home_server (tmux) $
> Or I would like my escape key binding to (default to?) send 3 escape keys
> if I have three tmuxes running.

You could do this but it would take some work.

The easiest way is to have a key binding to swap your local tmux prefix
to a different prefix and back, something like:

set -g prefix2 C-a
bind F1 if -F '#{==:#{prefix},C-b}' 'set -g prefix None' 'set -g prefix C-b'

So normally you can use C-a or C-b for your local tmux, but when you
know you have an inner tmux, you press C-b F1 to remove C-b from the
local tmux so you can use it for the inner tmux. When you are done you
use the second prefix and press C-a F1 to toggle it back.

It is more difficult to do it automatically, because the local tmux
doesn't know you are running tmux remotely, so it will need some way to
work it out.

If you are always running tmux every time you ssh somewhere you could
just make the prefix key pass through if pane_current_command is ssh, so
something like:

set -g prefix None
bind -n C-b if -F '#{==:#{pane_current_command},ssh}' 'send C-b' 'switchc -Tprefix'

(It is probably worth also setting prefix2 to some other key so you have
a way to control the inner tmux if it gets confused and C-b doesn't
work.)

If you are not always running tmux when you ssh, you need the inner tmux
to tell the local tmux. The only possible ways to do this are using the
pane title or window name.

The pane title is probably better because it will work even if you have
multiple panes in the local tmux, some with tmux and some without. You
can make tmux set the title itself (with set-titles/set-titles-string)
but since it can't clear it when it detachs you will need a shell
wrapper to do that and it might as well set the title as well, something
like this on the remote host:

xtmux {
printf "\033]2;TMUX\007'
tmux "$@"
printf "\033]2;\007"
}
alias tmux=xtmux

Then in the local tmux:

set -g prefix None
bind -n C-b if -F '#{==:#{pane_title},TMUX}' 'send C-b' 'switchc -Tprefix'

If you want to make the title more useful than "TMUX" you could use the
m format modifier instead of == to make it detect whatever pattern means
"tmux running".

I didn't test any of this but it should give you the idea.

> Any ideas or suggestions regarding my scenarios?
> Thanks!
> -m
> On Wed, Oct 2, 2019 at 2:46 PM Nicholas Marriott
> <nicholas...@gmail.com> wrote:
>
> Hi
>
> tmux can't change the environment of other processes, including its
> parent.
>
> You could possibly do it yourself with a script that detaches tmux,
> usese tmux showenv and pane_current_path to update the parent shell,
> then kills tmux.
>
> Alternatively it would possible to add a feature where tmux replaced
> itself with a new shell with the updated environment but I don't see
> much use for this so there is little chance of it being implemented.
>
> On Wed, Oct 02, 2019 at 10:09:23AM -0500, Matt Zagrabelny wrote:
> >A A Greetings,
> >A A Is there a way to leave tmux, but somehow seed the environment
> (history,
> >A A CWD, etc.) with everything that was part of the tmux environment?
> >A A Sort of like:
> >A A tmux
> >A A # do stuff
> >A A tmux --copy-environment-exec-shell
> >A A # now the shell is running without tmux
> >A A What do you folks think?
> >A A Thanks for any help!
> >A A -m
> >
> >A A --
> >A A You received this message because you are subscribed to the
> Google Groups
> >A A "tmux-users" group.
> >A A To unsubscribe from this group and stop receiving emails from it,
> send an
> >A A email to tmux-users+...@googlegroups.com.
> >A A To view this discussion on the web, visit
> >A A
> https://groups.google.com/d/msgid/tmux-users/CAOLfK3V%2BVMY4br1fKfvXbjukAcdzV5pCAMjrHoq%2BuWkMqC%3D5nw%40mail.gmail.com.

Matt Zagrabelny

unread,
Oct 3, 2019, 9:56:05 AM10/3/19
to Nicholas Marriott, tmux-users
Hey Nicholas,

Thank you for the detailed write up. I've read it a couple times, and will need to read it a couple times more to fully comprehend. I'm still a tmux noob.

I'll posts any useful results to the list.

Thanks again for all the help!

-m
Reply all
Reply to author
Forward
0 new messages