GNU/Linux

tmux Notes

2024/04/18

tmux

tmux (Terminal MUltipleXer) is a powerful utility that I often use, particularly when managing GNU-Linux servers and macOS. It’s a great tool for managing multiple terminal sessions within a single SSH connection.

One of the primary reasons I use tmux is it lets perform a variety of tasks simultaneously from a single terminal instance. This include managing web servers, accessing database consoles, monitoring system resources, and even writing code. All of this is accomplished without the need to constantly switch between different terminal windows or sessions, making tmux an indispensable tool in my toolbox.

Basic Usage

Running tmux

Starting tmux is straightforward. To initiate a new tmux session, you just type tmux in your command line. This opens up a tmux-terminal session that operates identically to a regular one. You can run any terminal commands just as you would normally do.

To start a new tmux session, simply type:

tmux 

tmux also gives you the option to create named sessions, which can be useful for better organization and easier navigation. This can be achieved using the new command followed by the -s option.

tmux new -s <name> 

The “<name>” should be replaced with the desired session name.

Getting Out of tmux

If you want to leave tmux and keep the session running, you can detach from your session by pressing the CTRL and b keys simultaneously ( CTRL+b ) followed by the d key. Detaching from a session returns you to the terminal you were in before starting tmux. This is useful for manually starting a server over ssh and keeping it running when you leave. tmux will persist after you terminate your ssh session.

Note that ( CTRL+b ) is called a prefix. The prefix tells tmux that you want to execute a tmux command and not a terminal command. In this case, d is the detach command.

If you want to stop tmux instead of leaving it running in the background, you can terminate your current session by typing CTRL+b, then :, followed by kill-session.

If you’re in a session with a single window and pane you can also terminate the session by typing

exit

Note that exit is not the same as killing a session. If you have multiple windows or panels in a session, exit will close the active panel (or window) and move you to the next one. When there are no more remaining panes or windows, tmux_will terminate the current session and return you to the terminal.

Connecting to an Existing tmux Session

You can connect to a running tmux server by typing

tmux attach   # attach to a tmux session
tmux a        # Same as attach

This works fine if there’s just one tmux session on your machine. You can check for existing sessions with the ls command.

tmux ls

This gives you a list of session currently running under tmux tmux ls screenshot

This will attach you to a session in tmux. You can pick a specific session with the -t option

tmux attach -t <session name>

Windows and Panes

Simply running a single session can be useful, but the real power of tmux is its ability to mimic multiple terminals with windows and panes. A window takes up the entire view in a session and acts like a terminal. You can have multiple windows in a session and switch back and forth between them. Windows can also be subdivided into panes. Each pane also acts as a terminal.

Creating Windows

You can create a new window in an existing tmux session by typing CTRL+b followed by c key Once you create a new window, it’ll be displayed in the tmux status bar at the bottom of your terminal window Create a tmux window In the above example, there are two windows: 0 and 1. The Asterisk to the right of “1:bash” indicates that window 1 is active. You can switch between windows by typing ‘CTRL+b’ followed by the number of the window you want to switch to.

Creating Panes

When you create a pane in a window, you’re splitting the current window (or pane) into two smaller panes. To create a vertical split, press CTRL+b followed by the % (percent) key tmux vertical split If you want to split horizontally press CTRL+b followed by the " (double quote) key tmux horizontal split You can continue to split the panes horizontally and vertically as needed.
tmux multiple splits

Window Navigation

As mentioned above CTRL+b followed by a number key selects a window by number. You can also change windows by displaying a list of windows and their sessions. Type CTRL+b followed by w to show a list of windows and sessions: tmux list of windows and sessions Note that the top pane lists the sessions by name and the windows in each session.

(0)   - node: 3 windows
(1)   ├─> 0: bash (1 panes) "BOOMSTICK"
(2)   ├─> 1: bash- (1 panes) "BOOMSTICK"
(3)   └─> 2: bash* (1 panes) "BOOMSTICK"
(4)   - react: 2 windows
(5)   ├─> 0: bash- (1 panes) "BOOMSTICK"
(6)   └─> 1: bash* (1 panes) "BOOMSTICK"
(7)   - scratch: 2 windows (attached)
(8)   ├─>   0: bash- (1 panes) "BOOMSTICK"
(9)   └─> + 1: man*Z (4 panes)
(M-a) - sql: 1 windows
(M-b) └─> 0: bash* (1 panes) "BOOMSTICK"

The leftmost column of the list has number and letters. You can select the window you want to switch to by pressing the key(s) corresponding to the window you want.

You can also use the Up and Down arrow keys to select a window. Press Enter to switch to the selected window.

Pane Navigation

Pressing CTRL+b followed by o cycles the active panel through the panels in the current window.

You can also select panels by number by pressing CTRL+b followed by q. This numbers the panels: tmux pane selection Press the number key corresponding to the panel you want to select.

Pane Layout

You can resize the active pane by typing CTRL+b and then the colon key :. This lets you type commands in the status bar. Type resize-pane followed by -U, -D, -L, or -R to move the borders of your pane Up, Down, Left, or Right respectively.

:resize-pane -U
:resize-pane -D
:resize-pane -L
:resize-pane -R 
:resize-pane -U 5
:resize-pane -D 5
:resize-pane -L 5
:resize-pane -R 5

You can also resize panes with the arrow keys. Just type

  • CTRL+b followed by ALT+Up Arrow to move the border up
  • CTRL+b follows by ALT+Down Arrow to move the border down
  • CTRL+b follows by ALT+Left Arrow to move the border left
  • CTRL+b follows by ALT+Right Arrow to move the border right

Cut and Paste

You can copy and paste text between panels. This takes several steps:

  1. Switch to the pane you want to copy text from.
  2. Type CTRL+b followed by [ (left square bracket) to put the pane in copy mode
  3. Use the arrow keys to position your cursor where you want to start the selection
  4. Type CTRL+SPACE to start the copy
  5. Use the arrow keys to position your cursor where you want to end the selection
  6. Type ALT+w to copy the selected text to the tmux buffer
  7. Switch to the pane you want to copy to
  8. Type CTRL+b followed by ] (right square bracket) to paste from the tmux buffer.

Styling

set-option -g pane-active-border-style "bg=colour208"
set-option -ag pane-active-border-style "fg=black"

set background of active window.

set -g window-active-style 'fg=colour250,bg=black'

turn off time

set-option -g status-right ""

About Me

Greg Gallardo

I'm a software developer and sys-admin in Iowa. I use C++, C#, Java, Swift, Python, JavaScript and TypeScript in various projects. I also maintain Windows and Linux systems on-premise and in the cloud ( Linode, AWS, and Azure )

Github

Mastodon

YouTube

About you

IP Address: 3.144.227.73

User Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)

Language:

Latest Posts

Iowa City Weather

Overnight

42 ˚F / 46 ˚F

Friday Night

41 ˚F / 50 ˚F

Saturday Night

35 ˚F / 48 ˚F

Sunday Night

32 ˚F / 44 ˚F

Monday Night

30 ˚F / 35 ˚F

Tuesday Night

24 ˚F / 31 ˚F

Wednesday Night

19 ˚F / 28 ˚F