Tmux
Tags: devtools, linux, tech, tmux
Published: 2021-06-22 05:38:04

---
title: Tmux
published: true
date: 2020-11-30 07:00:19 UTC
tags: devtools,linux,Tech,tmux
canonical_url: https://vuyisile.com/tmux/
---

A part of my job involves connecting to a couple of Amazon EC2 instances(Linux servers) regularly to run commands, build, debug and deploy code. All along I’ve been using tabs terminal tabs to keep track of each server connection and the processes running in it. Using the terminal emulator’s tabs feature works okay for simple use cases but once you start running long running commands over SSH on multiple machines, you need something more powerful. Enter tmux.

![terminal window showing a split terminal](https://i2.wp.com/vuyisile.com/wp-content/uploads/2020/11/tmux_zsh_snap.jpg?resize=632.4%2C382)

Tmux is a terminal multiplexer, it allows you to create windows and panes in your terminal and it offers you the ability to run processes in named sessions.

### Installation

Installing tmux is pretty straightforward on Linux and Mac. On Ubuntu and similar distros, do `sudo apt-get install tmux` or `brew install tmux` in Mac.

 

### Running tmux

tmux follows a server/client model. When you run it, this starts the tmux server that keeps track of processes. To start a new tmux session type` tmux` into the terminal. You’ll know you’re in tmux when you see a green status bar at the bottom. This status bar shows important information about your system and your tmux session.

![tmux terminal window](https://i2.wp.com/vuyisile.com/wp-content/uploads/2020/11/tmux_activated.jpg?resize=632%2C429)

 

Commands in tmux are triggered by a prefix key followed by a command key. The default prefix key is C-b(`Ctrl-b` on Linux or` Cmd-b` on Mac). Pressing these two keys at the same time lets tmux know that you’re about to pass it a command.

### Split screen

To split your screen vertically, the command to run is

`Ctrl-B + %`

This means press and hold down the **Ctrl** key, then press **b** at the same time. Release and press the percent symbol(SHIFT – 5). This will split your screen in half allowing you to run two sets of commands in the same window:

![tmux split screen](https://i0.wp.com/vuyisile.com/wp-content/uploads/2020/11/tmux_split_screen.jpg?resize=632%2C333)

To create a horizontal split, run:

`Cntrl + b "` That’s `Ctrl` and` b` at the same time then SHIFT and the quote symbol(“).

### Sessions

I think the true power of tmux lies in its ability to create sessions. With sessions you can run a long running task, detach from the session and go about your business. tmux makes it easy to reconnect to the session.

To create a new tmux session, do  
`tmux new -s <session-name>`. This creates and starts a session named “session-name”. In the example below, I create a session named “test”. Note that the name of the session appears in the status bar on the bottom left.  
 ![tmux session](https://i1.wp.com/vuyisile.com/wp-content/uploads/2020/11/tmux_test_session.jpg?resize=632.4%2C370)

To detach from a session, do `Ctrl-b d` To re-attach to the named session do  
`tmux attach -t <session-name>` The **t** flag stands for target, but I like to read the command as “attach to <session-name>”.

### Other commands

There is much more you can do with tmux and I barely scratched the surface in this blog post, but the commands above cover the most basic tasks. Below is a quick reference of common tmux commands:

- `Ctrl + b + c`     Create a new window
- `Ctrl + b + w`    List windows
- `Ctrl + b` `+ 0-9`    Switch to window number
- `Ctrl + b` `+ ,`    Rename a window
- `Ctrl + b + %`     Split window vertically
- `Ctrl + b + "`     Split window horizontally
- `Ctrl + b`` + O`    Go to next pane
- `Ctrl + b` `+ ;`     Toggle between current and previous pane
- `Ctrl + b` `+ x`     Close current pane
- `Ctrl + b + ARROW KEY` Move between windows using arrow keys