Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run tmate on an already existing tmux session #26

Open
Integralist opened this Issue Jan 31, 2014 · 16 comments

Comments

Projects
None yet
@Integralist
Copy link

Integralist commented Jan 31, 2014

How can I run tmate on an already existing tmux session? Haven't been able to figure this out yet

@nviennot

This comment has been minimized.

Copy link
Member

nviennot commented Jan 31, 2014

This is not possible.
What you could do instead is always run tmate.

@stefanpenner

This comment has been minimized.

Copy link

stefanpenner commented Feb 2, 2014

@nviennot i've thought about this, but it appears tmate ls does not make it easy for my to re-attach to my existing local tmate sessions

@nviennot

This comment has been minimized.

Copy link
Member

nviennot commented Feb 3, 2014

I see.

I guess I'll have to implement multi sessions to make it work :)

@mikedfunk

This comment has been minimized.

Copy link

mikedfunk commented Feb 13, 2014

multiple sessions would be nice

@nviennot

This comment has been minimized.

Copy link
Member

nviennot commented Feb 13, 2014

I have some paper deadline to deal with at the moment, I will give tmate some love soon.

@jmgarnier

This comment has been minimized.

Copy link

jmgarnier commented Apr 16, 2014

This feature would be awesome 😸

This is exactly the kind of the feature I am willing to pay a yearly subscription for!

@nviennot

This comment has been minimized.

Copy link
Member

nviennot commented Apr 16, 2014

My paper deadline is in two weeks, so I'll give it some thoughts fairly soon :)

@sighmin

This comment has been minimized.

Copy link

sighmin commented May 5, 2014

Yeah I'd also pay for it.

I always run tmate but then the session times out from time to time then have to close tmate and recreate it which is a bit annoying - would be cool to be able to grab a new session at tmate.io ?

You're doing a great job dude!

@nviennot

This comment has been minimized.

Copy link
Member

nviennot commented May 5, 2014

Before proposing paid offerings, which will most likely be used for production environment, I need to do a bit of work. I need to restructure the whole server-side, to allow reconnections, and fault tolerance (if one of my server dies). The new pipeline will also allow incorporation of websockets (for the HTML5 stuff), and session recording (probably to Cassandra, with fulltext search done with Elasticsearch).
Not to worry, the free pairing feature will never go away.

Thanks for the support :)

@maljub01

This comment has been minimized.

Copy link

maljub01 commented Jun 5, 2014

You can nest tmux & tmate sessions as much as you like as long as you first set TMUX to an empty value.

So inside an existing tmux/tmate session, you can run new ones like this:

TMUX='' tmate
TMUX='' tmux

You can also access pre-existing tmux sessions on the machine you're accessing using tmate by using:

TMUX='' tmux list-sessions
TMUX='' tmux attach-session -t SESSION_NAME

You'll need to be careful though because the tmux prefix will always be captured by the outer tmux/tmate session, and you'll need to press it twice to have tmux forward it one level deeper. This means you'll need to press it 2 ^ depth times in order to reach inner sessions (assuming depth starts at 0).

Basically, when you press Ctrl-B once, you'll be sending commands to the outermost session, pressing it twice will send commands to the one inside it, and to access a session inside that one, you'll need to press it 4 times.

Having to press Ctrl-B Ctrl-B Ctrl-B Ctrl-B Ctrl-N to move between windows is either totally crazy or totally awesome. 😜

However, if you manage your tmux sessions properly you will never need more than 2 levels (ie. max depth = 1).

One way to do this would be to:

  1. Have your main work done in a tmux session.
  2. Whenever you need to add a new person, create a tmate session in a different terminal and have them join it and from there attach to your original tmux session.
  3. You can even setup tmate to use a different prefix so it will stay out of your way while you're using the shared tmux session.
  4. Profit!

I have a few words of advice though:

  1. Make sure you can reattach to your nested tmate sessions if you accidentally detach from them by pressing too few or too many Ctrl-B's. #31 should help you avoid locking yourself out.
  2. Make sure you don't get an infinite recursion of tmux/tmate sessions (ie. attach to a tmux session from within itself). If you do, just use the appropriate combination of Ctrl-B's followed by a Ctrl-D (to detach from the offending session), and then wait a few seconds until tmux sorts the craziness out and returns to normal.

This works for me, and it works pretty well actually. But I cannot guarantee that it will not blow up in your face, so be careful. 👍

@taylor

This comment has been minimized.

Copy link

taylor commented Jun 17, 2014

Until attach/detach work with tmate I recommend nesting tmux as mentioned by @maljub01. To deal with the nested command key I suggest setting tmate to something other than what is used by tmux. I can recommend using ctrl-] which is telnet's escape sequence.

Basically treat tmate as a sharing container for whatever work you are doing and not for any of it's tmux capabilities.

@sukima

This comment has been minimized.

Copy link

sukima commented Feb 2, 2015

I to find this solution better, Mainly because tmux is at version 1.9 and when I run tmate I loose many features in the tmux binary that tmate doesn't have.

@juandazapata

This comment has been minimized.

Copy link

juandazapata commented Apr 4, 2016

Ping

@nviennot

This comment has been minimized.

Copy link
Member

nviennot commented Apr 4, 2016

I don't see how to share an existing tmux session properly :/
Any ideas?

@jaspervanbrian

This comment has been minimized.

Copy link

jaspervanbrian commented Dec 7, 2018

Hello, I just implemented a solution but it is quite different. I set up my ~/.bashrc/~/.zshrc to open tmux initially. Here is my initial setup:

        # Launch TMUX
	# If not running interactively, do not do anything
	[[ $- != *i* ]] && return
	[[ -z "$TMUX" ]] && exec tmux
	tmux_count=$(tmux list-panes | wc -l)
	if [[ $tmux_count == 1 ]] ; then
	#	neofetch
		((tmux_count++))
	fi

Now I installed tmate, but I also wanted to execute tmate inside a tmux pane. Here's what I did:

# Check if user has opened tmate
if [[ -z "$ZSH_TMATE" ]]
then
	# Launch TMUX
	# If not running interactively, do not do anything
	[[ $- != *i* ]] && return
	[[ -z "$TMUX" ]] && exec tmux
	tmux_count=$(tmux list-panes | wc -l)
	if [[ $tmux_count == 1 ]] ; then
	#	neofetch
		((tmux_count++))
	fi

	# Alias tmate
	export ZSH_TMATE=tmate
	alias tmate="gnome-terminal"
else
	unset TMUX
	unset ZSH_TMATE
	exec tmate
fi

First, it checks if a ZSH_TMATE variable has been set. If no variable is detected, I will set the variable and will alias the tmate command to open a new gnome-terminal instance.

It will open a new gnome-terminal, and on the gnome-terminal instance, it will check if a ZSH_TMATE variable is set. It will pass on the else case because you have set it earlier.

I needed to unset the ZSH_TMATE variable, because it causes an infinite loop that nests a tmate instance with another tmate instance. Not exactly sure why it happens tho, I just experimented it a little.

Working demo:

ezgif com-video-to-gif

jaspervanbrian added a commit to jaspervanbrian/dotfiles that referenced this issue Dec 7, 2018

Install tmate, vim-surround
- Make a script on ~/.zshrc that lets the user to execute tmate on an
existing tmux instance, but the tmate is opened on a new terminal.
My solution:
tmate-io/tmate#26 (comment)
- Set relative line numbers on vim
@sukima

This comment has been minimized.

Copy link

sukima commented Dec 10, 2018

The solution is simple, nest TMUX inside TMate. Unfortunately the information above on how to do this seem rather complicated. Here is my take to add to the confusion and maybe it will help someone.

First understand your TMUX prefix key. By default this is CTRL-B In the following examples I have choosen to use CTRL-Space by use of this command in my .tmux.conf file:

set-option -g prefix C-Space

Next in your .tmate.conf file you have to change this. I choose CTRL-y in these examples. First unbind the prefix you set in .tmux.conf and redefine the prefix in .tmate.conf To make things even better I've taken the extra step of unbinding keys that I want to pass directly through to TMUX and turning the status off (as nesting would normally produce two status bars). Here is my full .tmate.conf file:

unbind-key C-Space
set-option -g prefix C-y
bind-key C-y send-prefix
unbind -n C-h
unbind -n C-j
unbind -n C-k
unbind -n C-l
set-option -g status off

Lastly, when I launch I do so in two steps. Since I live in TMUX normally I typically will detach and reattach inside tmate when I need. Alternatively I could also launch a new session inside tmate if I wish. The trick is when inside tmate to always prefix my TMUX commands with TMUX=. Here is an example:

$ tmate
------------------- Inside tmate ---------------------
$ TMUX= tmux attach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.