Boost Productivity with tmux: Master Terminal Sessions

Boost Productivity with tmux: Master Terminal Sessions

·

3 min read

You can find the Chinese version of this article at 終端機 session 管理神器 - tmux.

As a developer, you should be familiar with the Terminal. Whether you want to create git branches, run the scripts, or ssh to the remote machine, you will need to use the Terminal.

I'd like to introduce you to a great tool called tmux. It's a terminal multiplexer with many useful features like multi-windows, window panes, and session saving. Most importantly, tmux can make your terminal stylish and professional even if you're not doing anything.

How to install tmux

On Mac OS, you need to first install Homebrew, and then use it to install tmux with the following command.

brew install tmux

On Debian/Ubuntu Linux, simply run the following command.

sudo apt-get install tmux

Once you've installed tmux, run the command tmux and you will see a green status bar appear at the bottom of the terminal.

Basic Concepts of tmux

session

Every time you run tmux, a new independent session is created.

window

A window refers to the entire screen as shown in the screenshot below, and there can be multiple windows in a session.

pane

As shown in the following screenshot, a window can be split into multiple panes. This functionality is typically used to monitor numerous programs simultaneously.

Basic Keybindings

When using tmux, you must press the prefix key before any keybinding. By default, the prefix is set to <Ctrl+b>.

Pane

Let's try the keybinding together!

  • <Ctrl+b> + ": Split pane with vertical layout

  • <Ctrl+b> + %: Split pane with horizontal layout

  • <Ctrl+b> + <Arrow Key>: Switch to other panes

  • <Ctrl+b> + <Space>: Toggle between pane layouts

  • <Ctrl+b> + x: Close the current pane

Session

  • <Ctrl+b> + d: Detach from the current session

  • $ tmux at: Attach to the last session

Common scenarios

Scenario 1 - Running a program and Monitoring the CPU usage simultaneously

This is the most common scenario for me. I usually use two panes to run a program and display the top/htop result simultaneously.

Scenario 2 - Writing a program in Vim with Real-Time results

This scenario is similar to the previous one. You can create a small pane to run the program while editing the source with Vim. If you're using a tool like nodemon, it will automatically re-run the script when you update it, so you don't need to leave Vim.

Scenario 3 - Keeping remote SSH sessions with tmux

Without tmux, the command will stop running if you run a command on a remote machine using SSH but the connection is lost.

To avoid this problem, you can install tmux on the remote machine. With a tmux session, even if your ssh connection is lost, your program will continue to run because it's in the tmux session.

Summary

The article is a simple introduction to tmux. As a tool I can't develop without, I want to share this super tool with you through this article.

Since this article is only an introduction for beginners, I didn't list all the commands and keybindings here. If you'd like to learn more about tmux, you can check out this cheat sheet. I hope that using tmux can make your development more efficient, and also make your terminal look super-cool!