tmux

2024-10-07

Getting Started with tmux

Before diving into commands, make sure tmux is installed on your system. Most Linux distributions include it in their package managers. For example:

Once installed, simply type tmux in your terminal to launch it. You’ll now be in a tmux session.

Tmux employs a simple keybinding system using the prefix key, usually Ctrl+b (configurable). Let’s look at essential pane commands:

Creating Panes:

Switching Panes:

Resizing Panes:

Example: Setting up a development environment

Let’s imagine you’re working on a project requiring multiple terminal sessions. You might use tmux to set this up:

  1. Start tmux: tmux
  2. Create a vertical split: Ctrl+b "
  3. In the left pane, run your web server: python3 -m http.server
  4. In the right pane, run your application code: python3 my_app.py

Now you have both your server and application running side-by-side, easily managed within a single tmux session.

Managing Windows and Sessions

Tmux allows you to work with multiple windows within a session, further enhancing organization.

Creating Windows:

Switching Windows:

Killing Windows:

Example: Organizing tasks

Suppose you have many different tasks: database management, code editing, and system monitoring. You could use different windows for each:

  1. Start tmux
  2. Create a new window with Ctrl+b c and name it “Database”
  3. In the “Database” window, connect to your database using psql
  4. Create a new window with Ctrl+b c and name it “Code”
  5. In the “Code” window, start your code editor.
  6. Create a new window with Ctrl+b c and name it “Monitoring”
  7. In the “Monitoring” window, run top or another system monitoring tool.

Now you have a clearly organized workspace with each task in its own window.

Detaching and Reattaching

One of tmux’s most powerful features is the ability to detach from a session and reattach later. This saves your work even if you close your terminal.

Detaching:

Reattaching:

Listing sessions:

Customizing tmux

Tmux offers extensive configuration options through the ~/.tmux.conf file. You can customize your prefix key, keybindings, and much more. For example, to change the prefix key to Ctrl+a:

set -g prefix C-a

This file allows for powerful customization of your tmux experience. Exploring the possibilities within this configuration file can improve your productivity.