Boost Productivity with tmux: Master Terminal Sessions

Search for a command to run...

No comments yet. Be the first to comment.
You can find the Chinese version at 從 IEEE 754 標準來看為什麼浮點誤差是無法避免的 Introduction When people first learn to code, they often encounter floating-point errors. If you haven't experienced this yet, you're very lucky! For example, in Python, 0.1 + 0.2 does...

You can find the Chinese version at 為什麼你應該幫 Rust Vector 加上初始容量 Introduction High-level programming languages usually offer growable arrays, like C++ and Rust's Vectors, Go's Slices, or JavaScript's Arrays. These can change in size and offer push and...

You can find the Chinese version at 分而治之 - 在 Golang 使用 Goroutine 進行平行運算 In Golang, starting a goroutine is very simple; you just need one line: go task(). This makes it easy to write concurrent programs. This article will explain how to use goroutin...

You can find the Chinese version at 用一個小例子談談 Golang 中的 Race Condition. Goroutine is one of the most important features of Go. It allows developers to easily achieve concurrency. Also, It is lightweight, making it feasible to create a substantial num...

You can find the Chinese version at Docker 實戰:使用 Volume 保存容器內的數據. This is the third article in the Docker Series. If you haven't read the previous article, "Pushing Your Docker Images to Docker Hub: A Step-by-Step Guide", I recommend taking a look a...

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.

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.

Every time you run tmux, a new independent session is created.
A window refers to the entire screen as shown in the screenshot below, and there can be multiple windows in a session.
As shown in the following screenshot, a window can be split into multiple panes. This functionality is typically used to monitor numerous programs simultaneously.

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

<Ctrl+b> + d: Detach from the current session
$ tmux at: Attach to the last session

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

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.

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.
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!