SETTINGS
Appearance
Language
About

Settings

Select a category to the left.

Appearance

Theme

Light or dark? Choose how the site looks to you by clicking an image below.

Light Dark AMOLED

Language

Preferred Language

All content on blog.claranguyen.me is originally in UK English. However, if content exists in your preferred language, it will display as that instead. Feel free to choose that below. This will require a page refresh to take effect.

About

"blog.claranguyen.me" details

Domain Name: claranguyen.me
Site Version: 1.0.0
Last Updated: 2020/12/29
How Tmux made me more productive on the Linux Terminal
Wednesday, July 19, 2017

Introduction

So I'm willing to bet that a lot of Linux power-users out there use terminal multiplexers to increase their productivity.

The terminal, at first, can be quite intimidating. I like to expose my students to it whenever I am teaching them how to program in C or C++. This is because UTK Computer Science major students are forced to use it to do their lab assignments eventually (usually in their second year).

The terminal, put quite simply, is just a single session for a user to input commands. However, there are so many ways to change this that it is ridiculous. Let's look at a general terminal running zsh:

Obviously, yours might not look the same. Zsh alone provides the ability to customise the interface to look like this. But that's not the point of why I show this. Notice that there is only one spot to type text in. Not a lot of people like that since they can only do one task at a time. That's no fun, is it? :(

Introducing Terminal Multiplexer: Tmux

Tmux allows you to do some pretty crazy stuff to enhance your terminal experience, and has a lot of cool features too. I'll cover a few of them. Here's what Tmux can allow you to do:

So there's a lot going on in the screenshot above (As with all images, click the image to see its full resolution). I'll break down a lot of the features covered here to show you what the hell is going on.

Before we get started

Tmux, like a lot of Linux software, is heavily customisable. Whenever you are going to input a command into Tmux, generally (unless changed) you will press "Ctrl + b". I've changed mine to "Ctrl + a" since it is a lot easier. If you want my config file, it is here.

I am probably going to get some kid complaining about this, but I will discuss commands using my configuration (Which is close to the original anyways). If you want the standard configuration, you can try looking at this cheat sheet (Which also doesn't use Ctrl+b, by the way... no one likes it).

Feature #1: Split Panes

The very first feature I am going to cover is splitting panes. In my opinion, this is also the second most useful feature of Tmux, because it allows you to have 2 terminal sessions side-by-side purely and directly in the terminal (without GUI assistance).

Controls

  • "Ctrl + a", then "|"                   -  Split a pane into 2 Horizontally
  • "Ctrl + a", then "-"                   -  Split a pane into 2 Vertically
  • "Ctrl + a", then "Up/Down/Left/Right"  -  Navigate Panes
  • Hold "Alt", then "Up/Down/Left/Right"  -  Navigate Panes (Alt)

Why this is useful to me

I can have 2 terminal tasks going on side-by-size. Sometimes I could be writing a C program on the left pane with vim, and then compile it with gcc on the right pane. That way, I never have to exit vim to compile. Also, a lot of the time, I run a 2x2 terminal pane setup to do 4 things at once (with "top" probably occupying one of the slots...)

Feature #2: Windows

The second feature I am going to cover is windows... actually, we will call them "tabs" because it just makes better sense. I showed you how to split panes as the first feature of tmux. However, you can also have multiple tabs which have different configurations of split panes if you wanted. You can also just have a lot of single pane tabs and switch between them easily.

Controls

  • "Ctrl + a", then "c"            -  Create a new window/tab
  • "Ctrl + a", then "x"            -  Close a window/tab
  • "Ctrl + a", then a number       -  Go to a specific page
  • "Ctrl + a", then ","            -  Give window/tab a custom name
  • Hold "Ctrl", then "Left/Right"  -  Navigate Windows/Tabs

Why this is useful to me

I like to categorise my work in tabs. I can have one tab just for developing a C program, and another for doing homework. At home, I have a single tmux session on my Raspberry Pi that has various tabs for various devices that I am SSH'd into (One tab for sshing into my server, one for UTK's computer science server, etc).

Feature #3: Remote Sessions

Here is where things get to be really fun. I mentioned earlier that I have a session on my Raspberry Pi that has multiple tabs, which are SSH'd into multiple devices. But how do I access my Raspberry Pi's tmux session without being directly on the Pi?

Turns out, you can control local tmux sessions remotely through SSH. SSH in, and then "attach" the tmux session. This means that I can run a Tmux session on my Pi locally, open vim, and start editing a file. Then I can remote into my Pi from a distant location, "attach" that same session remotely, and then have access to vim from there. This also means that all tabs and panes in that session show up on the remote end as well. To me, this is the most useful and valuable feature of tmux.

This is really simple. When starting a tmux session in the terminal, type this:

UNIX Command
tmux new-session -s SESSION_NAME

This will create a session under the ID "SESSION_NAME" (feel free to change it to something else). This means that anyone remoted in to your device via ssh can access the same session via this command:

UNIX Command
tmux attach -t SESSION_NAME

Controls

  • "Ctrl + a", then "d"  -  Deattach tmux session (Keep it in background)
  • "Ctrl + a", then "D"  -  Choose a tmux session to deattach (Yes, you can force a device remotely to deattach)

Why this is useful to me

My Raspberry Pi has a constant Tmux session up that I can remote into at any time (it should be noted that this is all local). This means that I can access that tmux session from any computer that has a terminal and ssh. Furthermore, if I forwarded a port, I could access the tmux session while at university and control my Raspberry Pi from a long distance.

Conclusion

I'm a terminal power user. As such, I go out of my way to try to try to make it easier for me to do things on it. The reason for that is because a lot of my work involves remoting into different machines and sometimes (most of the time) a GUI just isn't available. Plus, I see that knowing your way around the terminal is a valuable skill in the case where a GUI isn't available (Linux Emergency Mode... *cough cough*). Anyways, I just thought I would share one of many secrets of how I make my life on the Linux terminal much more enjoyable with tmux.




Clara Nguyễn
Hi! I am a Vietnamese/Italian mix with a Master's Degree in Computer Science from UTK. I have been programming since I was 6 and love to write apps and tools to make people's lives easier. I also love to do photography and media production. Nice to meet you!


Blog Links
Post Archive
Affiliates/Cool People
Nigoli's Blog
Raas's Blog