In the realm of command-line tools, efficiency and productivity are paramount. Whether you're managing remote servers, developing software, or simply trying to juggle multiple tasks simultaneously, the terminal can sometimes feel limiting. Meet tmux, a powerhouse tool that revolutionizes our command-line workflow. Whether it’s running multiple sessions from one screen, preserving work during disconnections, or organizing complex projects, tmux brings unmatched efficiency and flexibility. Ready to unlock the full potential of your terminal? Let’s dive into the basics of tmux world.
What is tmux?
TMUX, short for Terminal Multiplexer, allows users to access multiple terminal sessions from a single screen.
Seems boring to read this right? But what if i tell you the whole use of tmux is much cooler. The same way we use multiwindow in windows machine or any UI based OS, we can multiply our terminal in the same window and multitask!
It's not only managing multiple programs from one terminal but also we can simply detach from the those instances and attach back remotely from any other ssh instance. Cool right?
Lets deep dive into tmux...
When and Why was it Developed?
tmux emerged as a vital tool for users who require multiple terminal sessions within a single window.
Its development began as an effort to improve upon existing solutions like GNU
screen
, which was the de facto standard for terminal multiplexing for many years. Developed by Nicholas Marriott, tmux was officially released in 2007 and has since evolved to include a robust set of features that cater to a modern workflow.The design of tmux includes a client-server model, allowing the user sessions to be detached from the interface and run independently in the background. This model not only enhances its stability and flexibility but also allows multiple clients to attach to the same session, a feature that is invaluable for collaborative work and remote pair programming sessions.
The ability to customize nearly every aspect of its operation through a
.tmux.conf
file has particularly endeared it to power users who enjoy tailoring their tools to their specific needs.
Why do we need tmux?
Session Management: tmux allows users to create, manage, and switch between multiple sessions from a single terminal window. Each session can contain multiple windows and panes, effectively multiplying the user's workspace and organizing tasks more efficiently.
Persistence: One of the standout features of tmux is its ability to detach and reattach sessions without interrupting running processes. This means you can start a long-running task on a server, detach from the tmux session, and later reconnect to see the task's progress or results. This is particularly useful for remote work on servers where network reliability might be an issue.
Resilience: Since sessions are maintained by the tmux server, they are not affected by user disconnections or network issues.
Customization: tmux is highly customizable, allowing users to modify key bindings, set up automatic window and pane layouts, and script entire workspaces with ease.
Increased Productivity: By using tmux, users can efficiently manage their terminals and simplify their workflows, which boosts productivity.
Collaboration: tmux's session sharing capability allows multiple users to view and interact with the same session, making it an excellent tool for collaborative debugging or teaching.
Let’s first Install tmux
TMUX might come pre-installed in some Linux distributions, especially those tailored for developers or systems administrators.
To verify, please run the following command:
tmux -V
If it’s not installed, please refer to the official tmux Installation Guide.
Now that we have it installed, Lets learn some basic Commands and Usage:
First, let's Recognise a few elements before to fidget with tmux. On any preferred OS, when we launch a terminal session, we see a window with a prompt following the $
symbol. Now, all we have to do is type tmux
into the prompt to launch our tmux window. I understand that things may seem a little unclear right now, but to have a better idea, look at the diagram below.
Command to start tmux
tmux
Now that we are inside a tmux window, it is very simple to spin up windows and panes. Windows would function similarly to tabs and panes, dividing a window to allow for many views of various instances.
easily spin up windows and panes in the tmux terminal. Windows would function similarly to tabs and panes, dividing a window to allow for many views of various instances.
tmux is adds productivity once we learn some hotkeys, dont worry it becomes easy as and when we use it multiple times. Lets understand how to create sessions before we jump into panes and windows in those session.
Plain tmux
commands does open a unamed session but lets understand way to name and organise them.
Create tmux session
tmux new -s <session_name>
For our Example:
Lets create a tmux session with name “Session1”
tmux new -s Session1
Every entity is organised by its index, and your session name will be seen at the bottom end as shown in the picture. Beside the session we see that by default we have bash terminal window opened.
Lets create another window in the same session.
Create a new window in the same session
tmux only listens to our commands once we say it to. How do we say? by pressing Ctrl+b
before passing in our tmux input. To Break Down, tmux will only execute commands when instructed explicitly. To send commands to tmux, you need to first press Ctrl+b
(this is the default prefix combination that tells tmux to listen for the next command).
To create a new window within the same session, press Ctrl+b
, then c
.
We see that names of this windows are by default shell names, but good news is we can rename, press Ctrl+b
and press ,
(Comma). You will be provided with a diaglog box at the bottom to type in names of your choice.
Yaayyy! We Renamed a widow.
Now we have two windows, and we see *
marked on new window, this means “New Window” is active. What if i want to navigate to first window? which would be “bash” in our case.
Command to navigate between windows
We have two best options for it,
Press
Ctrl+b
, thenn
to cycle through the windows.For a more visual approach, press
Ctrl+b
, thenw
to open a list of all windows. You can then use the arrow keys to navigate and press Enter to select the desired window. After we create panes we can use this command to navigate between them too.
Isn’t it Cool??. Now lets move to the coolest part, panes!
Command to create panes in a window
Press Ctrl+b
, then %
to split the window into two vertical terminals. If you want to split it horizontally, then press Ctrl+b
followed by “
(double quotes).
Now, your tmux session should look something like this:
This setup enables you to have multiple terminal instances within the same window, ideal for monitoring different processes simultaneously or comparing files side by side.
Listing tmux Sessions
To see all active tmux sessions along with their details, you can use the following command:
tmux list-sessions
or the shorthand:
tmux ls
This command will output information for each session, including its name, windows, and more, helping you keep track of what’s running.
Closing a Window in tmux
Sometimes, you might need to close a specific window within a session. To do this:
First, ensure you are in the window you want to close.
Press
Ctrl+b
& then&
.
A prompt will appear asking for confirmation to kill the window, usually requiring you to press y
to confirm.
Killing a tmux Session
If you need to end a specific tmux session, you can do so using the kill-session
command. This is useful when you want to clear up unused sessions or after completing a project:
tmux kill-session -t <session_name>
Replace session_name
with the actual name of the session you wish to terminate.
Killing All tmux Sessions
To kill all active tmux sessions, which is equivalent to stopping all tmux operations that you have running, use the following command:
tmux kill-server
This command terminates the tmux server, effectively closing all sessions and windows. This is a powerful command and should be used with caution, as it will stop all ongoing tasks within tmux.
Additional Commands and Tips
Renaming a Session: Sometimes, you might want to rename an existing session for clarity or organizational purposes. You can do this by pressing
Ctrl+b
and then$
, which will prompt you to enter a new name for the current session.Very Importantly, to detach from a session: If you want to leave a session without ending it (so it continues running in the background), you can detach by pressing
Ctrl+b
and thend
. This is particularly useful for long-running processes or tasks that you want to check on later.
Further Learning and Resources
Mastering tmux involves practice and exposure to various commands and scenarios. While this guide covers the basics to get you started, there are many more commands and tricks to explore to become truly proficient. To aid in your journey, here are a couple of resources that I highly recommend:
tmux Cheat Sheet
For a quick reference to tmux commands, visit tmux Cheat Sheet. This website offers a concise and handy overview of the most useful tmux commands, key bindings, and configurations.
Video Tutorial
For a visual guide on using tmux effectively, watch this detailed tutorial on YouTube: Learn tmux - Tutorial for Beginners. It’s great for beginners and those looking to refresh their skills.