Reuse terminal workspaces using tmux sessions

Bonnie Eisenman
InstructorBonnie Eisenman

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 4 years ago

In this lesson, we'll learn how to detach from a running tmux session and leave it running in the background. Then, we can re-attach to it later.

We'll cover:

  • How to exit a session by killing all active panes
  • Detaching explicitly with C-b d
  • Detaching a specific session with C-b D
  • Viewing all running tmux sessions using tmux ls
  • Reattaching with tmux attach -t

[00:00] Right now we have three tmux windows open. You can see this is 0this is 1, and this is 2. I could quit this tmux session by killing all of them, but that seems a little bit like overkill. I want to keep these around so that I can come back to this session later.

[00:16] In order to detach from this session and leave it running in the background, I type Ctrl-b d. That will detach. It says, "Detached from session 0right here. I can start tmux again with tmux. You'll see I'll be in a different session. If I want to get out of this session, I can also do Ctrl-b d. Now it says, "Detached from session 2."

[00:39] I've made a couple of tmux sessions and detached from them. They're still running in the background. In order to get back to them, I can type tmux attach. This will bring me back to my most recent session. If I detach from it again, we're back on my normal command line, not in a tmux session.

[00:55] I can type tmux ls. This will tell me which sessions are currently running. These sessions are currently named session 0and session 2. Tmux ls will give us some useful information which will hopefully help jog our memory so we can remember what was actually going on in those sessions.

[01:12] Session 0has three windows. We can see the date and time at which it was created. Session 2 only has one window. It was created at a different time. Now we can reattach to a specific session by typing tmux attach -t and then the session name. Let's go back to session 0There we go. We've resumed this session.

[01:31] One of the great things about tmux sessions is that they will persist even if you accidentally close out of them. For example, let's close the terminal window here. Now if I open a new terminal window and run tmux ls, you'll see that my sessions are still there. I can reattach and resume working, even though I accidentally closed the session.