04. Session Management tmux

Sessions are used to separate out completed unrelated workspaces. One of the sessions started can be for actual company work, while another session can be for our own personal side project. We can keep track of these sessions by names, and close (dettach) and open (reattach) them with their current states saved.

7 Commands for Session Managemt

1) Creating a new Session

The moment you start Tmux with the tmux command, you're placed in a session. If you'd like to name the session, use the new subcommand:

$ tmux new -s work

Here, we named our session "work". We can also use the keystroke:

2) Renaming a session

To rename our session to "play", simply by using the rename-session subcommand:

$ tmux rename-session play

Or use the keystroke:

<Ctrl-b> ,

3) Show all sessions

To get a listing of all sessions, type Ctrl-b s or type:

$ tmux list-sessions
# Same as:
$ tmux ls
work: 2 windows (created Tue Dec 22 14:00:52 2015) [85x24]

4) Detaching

Detaching means to save the session, unlike just exiting, which doesn't save. This feature is one of the hallmarks of tmux, and is what makes it persistent. To detach the current session, use the keystroke:

<Ctrl-b> d

Or the command:

$ tmux-detach

5) Attaching

Attaching a session is used to return back to a previous session that you detached from. To re-attach a session, use:

$ tmux attach -t work

The name of the session can be found from the list-sessions subcommand as shown above.

6) Switching

To switch to a particular session, use either the keystroke:

$ tmux switch -t session_name

7) Destroying

To get rid of a session, type:

<Ctrl-b> d

Sharing Sessions

Probably the coolest feature of sessions is that you can share it with other users, much like a Google Docs file. This means that if two users are SSH'ed into a remote host, and are using the same session, they can see exactly what the other is typing in real-time.

Sharing sessions on tmux is fun and perfect for paired programming!
Sharing sessions on tmux is fun and perfect for paired programming! Notice you can tell how big the other user has their terminal set with the green lines.

With one tmux session open, try opening up a new terminal window and attaching that same session.

$ tmux list-sessions
base: 1 windows (created Tue Dec 22 13:04:34 2015) [80x24] (attached)
$ tmux attach -t base

You should now have control of both windows! This won't be of any use alone, so go make some friends and perhaps you guys can have a fun paired-programming session!

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources