Join my Laravel for REST API's course on Udemy 👀

Reattach to tmux sessions

March 30, 2020  ‐ 2 min read

When you detached from tmux and want to reattach to it again you use the tmux attach command. Without any argument this command reattaches tmux session that was the latest one to be detached.

$ tmux attach

attach is a shorthand actually for attach-session, but both do the same thing so you can use them interchangeably.

When attaching to the latest detached session isn't enough you need to specify a target session to attach to. For this you need to know the session name. The session name is either a number that tmux automatically assigned to it or the name you gave with when creating the session with tmux new -s <session>

If you don't know the session ID you can find a session by listing all the sessions with the tmux list-sessions command.

$ tmux list-sessions
first_project: 4 windows (created Wed Mar  4 18:17:14 2020)
second_project: 2 windows (created Wed Mar  4 19:00:55 2020)

Now, to reattach to the session called first_project use the attach command with the -t option.

$ tmux attach -t first_project

Attaching to a session while you're in tmux doesn't work. If you try it you will see the following error message: sessions should be nested with care, unset $TMUX to force. Inside of tmux you can switch between sessions with tmux choose-tree for example.