这是用户在 2024-5-31 2:44 为 https://hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/ 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

A Quick and Easy Guide to tmux
一份快速简单的 tmux 指南

I love working with the command line. I think there’s hardly any more productive and versatile tool for a software developer than the terminal. The additional hacker/wizard/neckbeard kind of feeling you get when using a terminal comes for free, what’s not to love?
我喜欢与命令行工作。我认为,对于软件开发人员来说,终端比任何其他工具都更加生产力和多功能。当使用终端时,你会获得一种额外的黑客/魔法师/颈部胡须感觉,这是免费的,什么都不会让人不喜欢?

Over the years I’ve tried to streamline and customize my command line experience to be more convenient, more fun to use or just to look rad. One of the most important tools to drive my daily command line experience is tmux. Check this out:
在过去的几年里,我试图简化和定制我的命令行体验,使其更方便,更有趣或者只是更帅气。驱动我每天的命令行体验的最重要工具之一是 tmux 。看看这个:

tmux in action

This screenshot shows nothing less than the best thing since sliced bread. It’s tmux, a so-called terminal multiplexer. Simply speaking, tmux acts as a window manager within your terminal1 and allows you to create multiple windows and panes within a single terminal window.
这个截图显示了自从有薯片以来最好的事情。它是 tmux,一个称为终端复合器的东西。简单来说,tmux 在你的终端中充当一个窗口管理器,并允许你在一个单一的终端窗口中创建多个窗口和面板。

This post will give you the same quick introduction to tmux and its possibilities, followed by a 10 minute hands-on guide to set up and get to know tmux yourself. If you’ve got 10 minutes to spare and want to earn street cred with your nerd friends: read on and become proficient with tmux!
这篇文章将为您提供关于 tmux 及其可能性的同样简短的介绍,然后是一份 10 分钟的实践指南,教您如何设置和了解 tmux。如果您有 10 分钟的时间,并且想要在您的网络朋友眼中赚取信誉:继续阅读并成为 tmux 的专家!

What’s tmux? tmux 是什么?

tmux’s authors describe it as a terminal multiplexer. Behind this fancy term hides a simple concept: Within one terminal window you can open multiple windows and split-views (called “panes” in tmux lingo). Each pane will contain its own, independently running shell instance (bash, zsh, whatever you’re using). This allows you to have multiple terminal commands and applications running side by side without the need to open multiple terminal emulator windows.
tmux 的作者们描述它为终端复用器。背后这个高级术语隐藏了一个简单的概念:在一个终端窗口中,你可以打开多个窗口和分割视图(在 tmux 术语中称为“面板”)。每个面板都会包含自己独立运行的 shell 实例(bash、zsh 等)。这使得你可以在不需要打开多个终端模拟器窗口的情况下,同时运行多个终端命令和应用程序。

On top of that tmux keeps these windows and panes in a session. You can exit a session at any point. This is called “detaching”. tmux will keep this session alive until you kill the tmux server (e.g. when you reboot)2. This is incredibly useful because at any later point in time you can pick that session up exactly from where you left it by simply “attaching” to that session.
除此之外,tmux 会将这些窗口和面板保存在一个会话中。您可以在任何时候退出会话。这被称为“分离”。tmux 会保持这个会话活动,直到您杀死 tmux 服务器(例如,当您重启计算机)。这对于日后的任何时间点非常有用,因为您可以通过简单地“连接”到该会话来在您离开的地方恰好恢复该会话。

If you’ve ever worked with remote servers or a Raspberry Pi over ssh you can guess where this will be useful: When you lose your ssh connection the tmux session will simply be detached but will keep running on the server in the background including all the processes that run within your session. To continue your session simply ssh to the server again and attach to the running session.
如果你曾经使用过远程服务器或 Raspberry Pi,你可以猜测这将在哪里有用:当你丢失你的 ssh 连接时,tmux 会简单地分离会话,但会在服务器后台继续运行,包括在你的会话中运行的所有进程。要继续你的会话,只需通过 ssh 再次连接到服务器并附加到正在运行的会话。

tmux is helpful when working on a remote machine but it shines just as much when you’re working locally. Not only for its window management features but also for the session handling. Personally I find myself detaching from sessions when I’m switching context. I’ll just start a new session for my new task and attach to the old session whenever I want to continue with my old task.
tmux 在远程机器上工作时非常有用,但当您在本地工作时,它也表现出同样的优越性。不仅仅是因为它的窗口管理功能,还有会话处理。我个人发现自己在切换上下文时会从会话中分离。我会为新任务开始一个新会话,并在想要继续旧任务时重新附加到旧会话。

You see that tmux basically offers two big features:
你看到 tmux 基本上提供两个大功能:

  1. Window management in your terminal
    终端窗口管理
  2. and session management 和会话管理

If you are familiar with GNU Screen this might all sound familiar. Think of tmux as an easier-to-use and a little more powerful alternative to Screen (obviously I’m being opinionated here).
如果你熟悉 GNU Screen,这些概念可能对你很熟悉。想象一下,tmux 是 Screen 的一个更易于使用且有一些更强大的替代品(当然,我在这里表现出主观看法)。

Enough with the concepts. Let’s get our hands dirty!
够了概念,让我们动手做起来!

Getting Started 开始使用

This hands-on guide will get you up and running with tmux pretty quickly. It will only cover the basic features which should be more than enough to get started and be productive with tmux. Simply open your terminal and follow the instructions.
这本实践指南将让你快速上手 tmux。我们将仅介绍基本功能,这应该足够你开始使用 tmux 并提高生产力。请打开终端并按照指示操作。

Installation 安装

Fortunately installing tmux is pretty straightforward on most operating systems: a simple sudo apt-get install tmux (Ubuntu, WSL and derivatives) or brew install tmux (Mac) should be sufficient.
幸运的是,在大多数操作系统上安装 tmux 非常简单:一个简单的 sudo apt-get install tmux (Ubuntu、WSL 和其他派生系统)或 brew install tmux (Mac)应该足够了。

Starting Your First Session
开始您的第一次会话

For your first session simply start tmux with a fresh session:
对于您的第一次会话,只需简单地开始 tmux

tmux

This will create a new tmux session with a nice all-green status bar at the bottom:
这将创建一个带有美观绿色状态栏的新 tmux 会话:

vanilla tmux on startup

The status bar is an important part of tmux. Apart from the currently opened windows (on the left) it also shows some system information like date and time (on the right). The status bar can also be customized and I’ve seen some really fancy stuff around (upcoming calendar events, battery status, to name a few) but this is something we’ll leave for later.
状态栏是 tmux 的一个重要部分。除了当前打开的窗口(左侧),它还显示一些系统信息,如日期和时间(右侧)。状态栏也可以自定义,我看到了一些真的很妙的东西(即将到来的日历事件,电池状态等等),但这是我们将在后面处理的事情。

Splitting Panes 分割窗格

Now that we’ve created our first session we can get a feeling for panes. When you create a new session, tmux will by default start with one window and a single panel inside. We want a nice split-screen, so let’s split this bad boy.
现在我们创建了我们的第一次会话,我们可以感受到面板的用法。当你创建一个新的会话,tmux 会默认以一个窗口和一个面板开始。我们想要一个美观的分屏,所以让我们来分割这个大叔。

All commands in tmux are triggered by a prefix key followed by a command key (all you emacs weirdos out there will feel right at home). By default, tmux uses C-b as prefix key. This notation might read a little weird if you’re not used to it. In “emacs” notation C- means “press and hold the Ctrl key”3. Thus C-b simply means press the Ctrl and b keys at the same time.
所有的 tmux 命令都是通过一个前缀键后跟一个命令键触发的。默认情况下,tmux 使用 C-b 作为前缀键。这种表示方法可能会让你感到有点奇怪,尤其是如果你不熟悉这种表示方法。在“emacs”表示法中, C- 表示“按住 Ctrl 键”, 3 。因此, C-b 简单地表示按下 Ctrlb 键。

The shortcut to split panes into a left and a right pane is C-b %. This means: to split your single pane into a left and a right pane you press Ctrl and b at the same time, release both, and then type the % key. Voilà! You’ve just invoked your first tmux command and split your pane in two.
快捷键将窗格分为左侧和右侧窗格是 C-b % 。这意味着:要将你的单个窗格分为左侧和右侧窗格,你需要同时按下 Ctrlb ,然后释放两个按键,并按下 % 键。恭喜你!你刚刚执行了你的第一个 tmux 命令,将你的窗格分为两个。

tmux with two split panes

Where there’s a split into left and right, there’s also a split into top and bottom pane. To split a pane into top and bottom panes use the C-b " shortcut.
当有左右分割时,也会有上下分割。要将面板分割为上下两个面板,请使用 C-b " 快捷键。

Right now we’re trapped in the newly created pane. But we really really want to go back to the left one. Easy peasy: Switching to a different pane uses the C-b <arrow key> shortcut, where <arrow key> is the arrow key pointing to the pane you want to switch to. In our case we want to switch to the panel on the left so it’s C-b left for us. Just once more, so that we fully understand this: This means you press Ctrl and b (your prefix) followed by the left arrow key to get to the pane on the left.
现在我们被锁定在刚刚创建的面板中。但我们真的很想回到左边的那个。很简单:切换到不同的面板使用 C-b <arrow key> 快捷键,其中<箭头键>是指向你想要切换的面板的箭头键。在我们的情况下,我们想要切换到左边的面板,所以它是 C-b left 。再说一遍,以便我们完全理解:这意味着你按下 Ctrlb (你的前缀),然后是 left 箭头键,就可以到达左边的面板。

You can now go ahead and split each of your new panels even further. Feel free to experiment and split your panes like a maniac to get a feeling for it.
现在你可以继续将每个你的新面板进一步拆分。欢迎尝试和拆分你的窗格像疯子一样,以获得一种感觉。

Closing Panes 关闭面板

Closing a pane is as simple as closing a regular terminal session. Either type exit or hit Ctrl-d and it’s gone.
关闭一个面板就像关闭一个普通终端会话一样简单。你可以输入 exit 或按下 Ctrl-d ,然后它就消失了。

Creating Windows 创建 Windows

Windows in tmux can be compared to creating new virtual desktops; if you’ve ever worked with one of the major Linux desktop environments (KDE, Gnome) or a Mac you’ll hopefully find this analogy helpful.
在 tmux 中的窗口可以与创建新的虚拟桌面进行比较;如果你曾经使用过其中一个主要的 Linux 桌面环境(KDE、Gnome)或 Mac,你应该会发现这个比喻对你有帮助。

Creating new windows is as easy as typing C-b c (one last time: that’s Ctrl and b at once, then c). The new window will then be presented to you in tmux’s status bar.
创建新窗口就像输入 C-b c (再次提醒:这是 Ctrlb 同时输入,然后是 c )。新窗口将会在 tmux 的状态栏中显示给你。

tmux with two windows

You can now divide the pane in your new window as you like. Or don’t. That’s up to you.
你现在可以根据自己的喜好将新窗口中的面板分割。或者不要。这取决于你。

To switch to the previous window (according to the order in your status bar) use C-b p, to switch to the next window use C-b n. If you’ve created many windows you might find it useful to go to a window directly by typing its number (the status bar will tell you which window has which number), just use C-b <number> where <number> is the number in front of the window’s name in your status bar.
要切换到上一个窗口(根据你的状态栏中的顺序),请使用 C-b p ,要切换到下一个窗口,请使用 C-b n 。如果你创建了很多窗口,你可能会发现直接通过输入窗口编号(状态栏会告诉你哪个窗口有哪个编号)去到一个窗口是很有用的,只需在 C-b <number> 中输入,其中是窗口名称在你的状态栏前面的数字。

Session Handling 会话处理

If you’re done with your session you can either get rid of it by simply exiting all the panes inside or you can keep the session in the background for later reuse.
如果你完成了你的会话,你可以通过简单地退出所有面板来丢弃它,或者你可以将会话保留在后台以供后续重用。

To detach your current session use C-b d. You can also use C-b D to let tmux give you a choice which of your sessions you want to detach. Detaching from a session will leave everything you’re doing in that session running in the background. You can come back to this session at a later point in time.
要取消当前会话,请使用 C-b d 。您还可以使用 C-b D 让 tmux 为您提供选择您想要取消的会话的选项。从会话中取消将使您在该会话中正在进行的所有活动在后台运行。您可以在后续的某个时间点回到这个会话。

To re-attach to a session and continue where you left you need to figure out which session you want to attach to first. List the currently running sessions by using
要重新连接会话并从您离开的地方继续,您需要先确定您想要连接的会话。使用以下命令列出正在运行的会话:

tmux ls

This will give you a list of all running sessions, which in our example should be something like
这将为您提供所有正在进行的会话列表,在我们的例子中应该是

0: 2 windows (created Sat Aug 15 17:55:34 2015) [199x44] (detached)
0:2 窗口(创建日期:2015 年 8 月 15 日 17:55:34)[199x44](已分离)

To connect to that session you start tmux again but this time tell it which session to attach to:
要连接到该会话,你需要再次启动 tmux,但这次告诉它要连接到哪个会话:

tmux attach -t 0

Note that the -t 0 is the parameter that tells tmux which session to attach to. “0” is the first part of your tmux ls output.
请注意, -t 0 是告诉 tmux 哪个会话要连接的参数。“0”是您 tmux ls 输出的第一部分。

If you prefer to give your sessions a more meaningful name (instead of a numerical one starting with 0) you can create your next session using
如果您希望为您的会话提供一个更有意义的名称(而不是以 0 开头的数字名称),您可以使用

tmux new -s database

This will create a new session with the name “database”.
这将创建一个名为“数据库”的新会话。

You could also rename your existing session:
您还可以重命名您的现有会话:

tmux rename-session -t 0 database

The next time you attach to that session you simply use tmux attach -t database. If you’re using multiple sessions at once this can become an essential feature.
下一次你连接到那个会话时,你只需简单地使用 tmux attach -t database 。如果你同时使用多个会话,这可能会成为一个必要的功能。

And that’s it! Congratulations, you’ve just completed your first tmux session and got your hands dirty with its window and session management. Yes, there’s more stuff tmux can do. But what you’ve just learned should be everything to start using tmux in the future.
好了!恭喜你,你刚刚完成了你的第一次 tmux 会话,并与它的窗口和会话管理打交道。是的,tmux 可以做更多的事情。但是你刚刚学到的应该是开始使用 tmux 的一切。

Why tmux? 为什么使用 tmux?

A response that I get quite often is: “Great, I get it. But why should I use tmux and its weird key combinations instead of just using iTerm2?”
我经常收到的一个回应是:“太棒了,我明白了。但是为什么我应该使用 tmux 和它的奇怪键组合,而不是直接使用 iTerm2?”

And you’re right, when it’s only basic window management, iTerm for Mac supports tabs and panes as well. For Linux there’s Terminator. So why would anyone feel the urge to learn some archaic technology in this day and age?
你说得对,当只是基本的窗口管理时,iTerm 对 Mac 也支持标签和面板。对于 Linux,有 Terminator。那么,为什么在这个时代人们会感到学习一些古老技术的欲望?

Well, obviously there’s the bragging rights. But if that doesn’t convince you, there are a couple of actual reasons why I favor tmux over iTerm et al.:
好吧,显然有谁的夸张权利。但是如果这不能让你相信,我为什么更倾向于 tmux 而不是 iTerm 等:

  • session handling: detaching from and attaching to sessions helps me with context switching and remote working
    会话处理:与会话断开和重新连接有助于我进行上下文切换和远程工作
  • platform independence: I can use tmux on my Macbook, my Linux notebook, servers, Raspberry Pis, you name it.
    平台无关性:我可以在我的 MacBook、我的 Linux 笔记本、服务器、Raspberry Pi 等设备上使用 tmux。
  • customizable: there are many ways I can customize the look and behavior of my tmux environment. And I can sync this across different platforms using a simple dotfile
    可定制:我可以通过多种方式定制我的 tmux 环境的外观和行为。我还可以使用简单的点文件将这些同步到不同的平台。

Moving on 继续前进

If you’re curious to learn what else tmux can do that’s a great thing. And luckily most of the stuff is quite simple to discover. Just type C-b ? to see a list of all available commands and start experimenting.
如果你对 tmux 的其他功能感兴趣,那是一件很棒的事情。幸运的是,大多数功能很容易发现。只需输入 C-b ? 以查看所有可用命令并开始实验。

Some of the commands that I’m using myself quite often are:
我自己使用的一些命令有时会使用得相当频繁,包括:

  • C-b z: make a pane go full screen. Hit C-b z again to shrink it back to its previous size
    0:使窗格全屏。再次点击 1 以恢复到之前的大小。
  • C-b C-<arrow key>: Resize pane in direction of <arrow key>4
    调整面板大小:向 C-b C-<arrow key> 方向滚动 4
  • C-b ,: Rename the current window
    重命名当前窗口

On top of that there are plenty of resources out there that help you getting further with tmux. People like me are blogging about their tmux experience and share what they’ve discovered. You can find people sharing their tmux configurations in their dotfiles repos on Github. There’s even a book by Brian Hogan dedicated to tmux.
除此之外,有很多资源可以帮助你更深入地了解 tmux。像我这样的人会在博客中分享他们的 tmux 体验,并分享他们发现的内容。你可以在 Github 上找到人们分享他们 tmux 配置的 dotfiles 仓库。甚至还有 Brian Hogan 专门为 tmux 写了一本书。

For now it’s best to discover some stuff on your own. Experiment, fool around and maybe try to use tmux for your daily work. It takes a while to get used to and you’ll probably feel slow when you start out. I encourage you to keep using it. Get a feeling for its functionality and in no time you’ll find out that your work with the terminal will be pure bliss and refreshingly fast.
目前最好自己发现一些东西。实验,玩耍,可能尝试使用 tmux 进行日常工作。这需要一段时间才能适应,你可能会觉得自己很慢。我鼓励你继续使用它。了解它的功能,不久你就会发现你的终端工作将变得纯粹的快乐和快速的刷新。

If you’re interested in customizing your tmux experience I recommend that you read my Guide to Customizing your tmux.conf.
如果你对自定义你的 tmux 体验感兴趣,我推荐你阅读我的指南:如何自定义你的 tmux.conf。

Footnotes 脚注

  1. If you’re coming from a Linux background you’re probably familiar with the concept of window managers like Openbox, i3 or awesome
    如果你来自 Linux 背景,你可能熟悉窗口管理器的概念,如 Openbox、i3 或 awesome

  2. tmux is based on a client-server architecture. While this is rarely relevant, it’s helpful to understand how stuff works. The tmux server keeps track of all the running sessions. You will only ever work with the tmux client and create new sessions or connect to existing ones.
    tmux 基于客户端-服务器架构。虽然这对于大多数情况来说很少有关系,但了解如何让事情工作是有帮助的。tmux 服务器跟踪所有正在运行的会话。你只会与 tmux 客户端工作,创建新会话或连接到现有的。

  3. there could also be M- which is the same only for the Meta key (i.e. Alt on most keyboards).
    也可能有 M- ,仅适用于 Meta 键(即大多数键盘上的 Alt )。 ↩

  4. This is an interesting one. Yes, you need Ctrl in your prefix and also your command key. I find it easiest to only release the b key after the prefix key and keep Ctrl pressed all the time while I hit the arrow key repeatedly. Experiment a little bit to get a feeling for it.
    这是一个有趣的一个。是的,你需要在前缀中包含 Ctrl ,并且你的命令键也需要。我发现最容易的方法是只在前缀键释放后释放 b 键,并且一直保持 Ctrl 按下,而我在箭头键上重复按下。尝试一下,以获得一种感觉。 ↩