Tmux Basics

Tmux – Manage Multiple Terminal Sessions

 

Installing tmux in Linux

Tmux is available in the official repositories of most Linux distributions.

$ sudo apt-get install tmux

 

Creating sessions

Ways of doing things

tmux
-----
tmux new

 

Detaching from tmux sessions

Pressing Ctrl+a  d

Creating names sessions

tmux new -s webProjectTest01

We can detach from this session , and reatach later it

To detach, simply press Ctrl+b and d.

Creating detached sessions

To attach it at some later point

tmux new -s webProjectLater -d

 

List tmux sessions

To view the list of open Tmux sessions, run:

tmux ls

 

Attaching to Tmux sessions

You can attach to the last created session by running this command:

tmux attach
-----
tmux a

 

 

Kill Tmux sessions

When you’re done and no longer required a Tmux session, you can kill it at any time with command:

tmux kill-session -t ostechnix

You can kill panes within session with

Ctrl+a  x

 

Or if your finished and don’t need to use tmux , just kill them all

tmux kill-server

 

 

 

Extras

 

$ tmux ls
no server running on /tmp/tmux-1000/default

Split Tmux Session Windows

Tmux has an option to split a single Tmux session window into multiple smaller windows called Tmux panes. This way we can run different programs on each pane and interact with all of them simultaneously. Each pane can be resized, moved and closed without affecting the other panes. We can split a Tmux window either horizontally or vertically or both at once.

Split panes horizontally

To split a pane horizontally, press Ctrl+b and ” (single quotation mark).

Split Tmux pane horizontally

Split Tmux pane horizontally

Use the same key combination to split the panes further.

Split panes vertically

To split a pane vertically, press Ctrl+b and %.

Split Tmux panes vertically

Split Tmux panes vertically

Split panes horizontally and vertically

We can also split a pane horizontally and vertically at the same time. Take a look at the following screenshot.

Split Tmux panes

Split Tmux panes

First, I did a horizontal split by pressing Ctrl+b “ and then split the lower pane vertically by pressing Ctrl+b %.

As you see in the above screenshot, I am running three different programs on each pane.

Switch between panes

To switch between panes, press Ctrl+b and Arrow keys (Left, Right, Up, Down).

Send commands to all panes

In the previous example, we run three different commands on each pane. However, it is also possible to run send the same commands to all panes at once.

To do so, press Ctrl+b and type the following command and hit ENTER:

:setw synchronize-panes

Now type any command on any pane. You will see that the same command is reflected on all panes.

Swap panes

To swap panes, press Ctrl+b and o.

Show pane numbers

Press Ctrl+b and q to show pane numbers.

Kill panes

To kill a pane, simply type exit and ENTER key. Alternatively, press Ctrl+b and x. You will see a confirmation message. Just press “y” to close the pane.

Kill Tmux panes

Kill Tmux panes

Zoom in and Zoom out Tmux panes

We can zoom Tmux panes to fit them into the full size of the current Terminal window for better text visibility and for viewing more of its contents. It is useful when you need more space or  focus on a specific task. After finishing that task, you can zoom out (unzoom) the Tmux pane back to its normal position. More details in the following link.

Autostart Tmux Sessions

It is always a good practice to run a long running process inside a Tmux session when working with remote systems via SSH. Because, it prevents you from losing the control of the running process when the network connection suddenly drops. One way to avoid this problem is to autostart Tmux sessions. For more details, refer the following link.

At this stage, you will get a basic idea of Tmux and how to use it to manage multiple Terminal sessions. For more details, refer man pages.

Was this article helpful?

Related Articles

Leave A Comment?