Tmux, the most used tool for multitask(plex)ing in the command line, has one important feature that takes it to the next level: sessions.

You can attach to an ongoing session and detach from it, which is pretty useful in a server environment since the sessions will be preserved for months anyway.

Here’s how you can attach to and detach from sessions in Tmux.

Detaching from a session in tmux

Consider a tmux session, where you have multiple panes and windows. To detach from this session, the shortcut is:

[Ctrl+B] + d

Here, Ctrl+B is the modifier key, and d stands for detach.

This will immediately detach you from the session, while keeping the processes running in the background.

Detach from tmux session
Detach from tmux session

Attaching to session in tmux

You can attach to the previously connected session immediately by running the command:

tmux attach
Attach to a session directly
Attach to a session directly

Managing multiple sessions

Generally, there may be multiple sessions, organised properly for different purposes. That’s when the ls command comes in handy. Use that first to list all the running sessions:

tmux ls
tmux ls
Example of tmux ls

To attach to a particular session, you can use the relevant name or number, as shown here:

tmux attach -t <name>
Attach to session by specifying target
Attach to the session by specifying the target

Conclusion

Tmux can be the ultimate productivity tool when used with all possible splits, such as panes, workspaces and sessions.

You might notice above that the target session has a custom name rather than number indexes. Renaming tmux session is the best method to neatly organize your sessions. The next part of the series deals with the same, see ya there.

Similar Posts