DEV Community

Igor Irianto
Igor Irianto

Posted on • Updated on

Tmux Tutorial for Beginners

Intro

Tmux is life. That is not an exaggeration. Once you started using Tmux, you will ask yourself why you have not used it until now.

What Tmux Is

According to man tmux, Tmux stands for "Terminal MUltipleXer". Before you get perPLEXed by the phrase Terminal MUltipleXer, I'll explain what it means.

A terminal multiplexer lets you to leave a terminal in the middle of a process and return to it seamlessly. For example, suppose that you are running a rails server, vim editor, and a mysql console in the middle of a rails workflow. Then someone asks you to debug a completely different node project. Without tmux, I would probably stop the rails server, close vim, and exit the mysql server before starting the node-project task. This context switch causes a grinding halt in your flow. What if instead of killing those rails-related processes, you can "put them aside" (while they're still running), run whatever processes you need to complete your node task, then return to your original rails-project task as if you never left? That would be nice, wouldn't it? Tmux lets you do just that!

To be fair, you could argue that you could just open more tabs for node tasks instead of killing the previous rails processes. Although that would work, this could cause tab chaos. At some point, you'll start asking, "which of these 6 tabs did I just run the jest test?" or "which of these two Vim tabs was my rails codebase?". When you start mixing tabs from completely unrelated tasks without clear boundaries, you can't tell which tab is for what. This also causes a grinding halt to your workflow.

The terminal is the crux of my workflow. I need to organize my terminal. Imagine this: I can contain all my rails processes inside a session named "rails_project" and all my node processes in a session named "node_project". If I need to do node tasks, I'll leave rails_project and visit node_project session. When I'm done, I'll leave node_project and revisit rails_project. Easy!

Tmux allows me to create any number of terminals from a single screen and group them. I can "detach" them and they will still continue running in the background. The best part? I can "attach" them at any time. Tmux is great for long-term running processes and local development.

Don't you want to learn how to do that too?

Why Do I Want to Use Tmux?

Over time, the number of process dependencies you need to get a job done increases. Today, it is often not enough to have just two terminals: one for running rails server and one for running vim to accomplish your task. In reality, you probably need a rails server, a webpacker, a sidekiq, a redis instance, and a growing number of microservices to launch your app. In addition, you may need to have mysql console and rails console to see what's inside your app. You may even need a dedicated terminal to run your tests. Don't forget that SSH window to get the sql dump too! Even if you have docker-compose to host multiple services, if you need to debug them, you'll need multiple terminals to debug each service. My point is, a simple task today would require more than 10 different active processes! It makes it harder to switch context.

Even if you don't need to switch context often, it is still useful to categorize your sub-workflows within your workflow. Maybe today you are focusing on writing tests, so you need to use vim and a free terminal to run your test code; they would need to fit inside a display. The next day, you may need to solve a data migration issue, so you need a vim editor, a mysql server to view your databases, and a rails console to check your models. Within a single project, you can still experience a grinding halt when trying to accomplish a different task.

The point is, you need to reduce the frictions caused by context-switching in today's increasingly complex tasks. Tmux can help with that.

What to Expect From This Guide

Ultimately, you decide whether you want to use tmux or not. You don't have to use tmux. There are alternatives like GNU Screen, Terminator, Byobu, etc. Check them out and you decide whether or not to use tmux.

Personally, I think tmux is a better fit for my workflow. I am writing this guide to share what I understand about tmux. I hope that you will learn one or two useful things.

Here is what you will learn in this guide:

  • Sessions
  • Windows
  • Panes
  • Servers
  • Basic configs

By the end of this article, you will be faster than you would ever be without tmux. Hopefully you will learn enough to continue pursuing learning tmux on your own, just like the proverbs say:

Teach a man tmux, you improve his productivity for a lifetime (someone, probably)

Please read this guide slowly. It is tempting to speed-read through a book, call it done, and move on to the next book. To get the most out of this, take your time understanding each section. For every 5 minutes you spend reading, spend 15 minutes tinkering around with what you just read. Break things.

With that said, let's learn some tmux!

Installation

First, you need to install tmux if you haven't already. A good way to visit the tmux repository.

I am using a Mac, so for me, I just needed to run:

brew install tmux
Enter fullscreen mode Exit fullscreen mode

Cool! Verify that it is installed. Run which tmux. It should return with a path. Now that tmux is installed, let's learn tmux's cornerstone: sessions.

Tmux Sessions

A tmux session is a collection of pseudo-terminals. Imagine running 10 different terminal processes, each doing different things. All these terminals are contained in a neat space that you can leave unattended and revisit at any time.

Creating a New Session

Let's use it. Run the command:

tmux
Enter fullscreen mode Exit fullscreen mode

This will launch a new tmux session.

If you're still unsure about the tmux session, don't worry. I initially had a hard time grasping the concept too. It would become much easier once I started getting immersed in it. So let's continue.

Attaching and Detaching a Session

To exit a session, you need to *d*etach. Press:

Prefix + d
Enter fullscreen mode Exit fullscreen mode

Hold up, what is a Prefix? I don't see any prefix key on my keyboard.

The prefix key can be anything you want. Many tmux shortcuts require you to press a prefix key first. If you're a Vim user, it is similar to the concept of a leader key. Think of it like a key to signal tmux that you are executing a tmux command. By default, tmux uses Ctrl + b as the Prefix key. From now, each time you see Prefix, it means Ctrl + b (hold Ctrl then press b). In this case, Prefix + d means holding Ctrl, then press b, then release them, then quickly press the letter d. Later, you'll learn how to change the prefix key from Ctrl + b to any key you want.

If you do it correctly, you should be taken out of the tmux session back to the original terminal.

When you're detaching from a session, any process running in that session is not terminated. It's still running. You just don't see it. Think of it like a background job.

Let's stop and appreciate how awesome this is. On a regular terminal, if we close the tab, whatever process that we were running would be terminated immediately. If I had rails server running and I close the tab, the rails server process would be killed. But if we detach from a session with rails server running, the rails server is still there. You are hiding the current process so you can focus on a different one. Later when you're ready to resume, you can "unhide" the rails server session by attaching into it.

To attach, from the terminal, run:

tmux attach
Enter fullscreen mode Exit fullscreen mode

Aaannd you're back. Cool!

The command line equivalent to detach (Prefix + d) is:

tmux detach
Enter fullscreen mode Exit fullscreen mode

Named Session

With tmux, you can liberally generate as many sessions as you need. Right now we have only one session running. To generate more sessions, you first need to be on the regular terminal (not inside a session). This time though, let's give our session a name. It's around breakfast time as I type this, so let's name it "breakfast" session (you can give it any name you want :D). Type:

tmux new -s breakfast
Enter fullscreen mode Exit fullscreen mode

The alternative (verbose) command is tmux new-session -s breakfast, but I like to keep it short. With that, you should be in the tmux breakfast session. Yum!

While you're inside the breakfast session, recall that we also have the original session running. To see a list of sessions and travel between sessions, run:

Prefix + s
Enter fullscreen mode Exit fullscreen mode

Now, let's detach from whatever session you are in and go back to the regular terminal (you're not inside any session). We now have two detached sessions: the original session and the breakfast session. So how do we attach to a particular session from the terminal? Earlier we ran the tmux attach to attach to the only available tmux session. Now that there are two sessions, we need to be more specific.

First, if you're forgetful like me and already forgot the name of your sessions, you can list the name of all the sessions by running

tmux ls
Enter fullscreen mode Exit fullscreen mode

If I want to attach to the breakfast session, run the attach command with -t (target) followed by the session name:

tmux attach -t breakfast
Enter fullscreen mode Exit fullscreen mode

Now I'm back to the breakfast session. Tmux is smart enough that you can give it a partial string. So if I run tmux attach -t b, as long as I don't have any session starting with b other than breakfast, tmux knows that I intend to go to breakfast.

Think of sessions like terminal containers. Modern development usually requires you to launch multiple services and jobs just to get the app running. For example, maybe the app that you're working on requires a redis server, a rails server, two node services, and a background job. You can use a session to contain all of those processes. This makes your workflow manageable and identifiable. If you ever need to leave them to work on something else, no need to kill them, just detach it, do whatever else you need to do, and -re-attach.

Killing a Session

Once you're done with the session task, you can kill that session.

There are two ways to kill a session:

  1. Run the terminal command to kill a session
  2. Close all the windows until there is no more

I'll start with method 1. To kill the breakfast session, run this from the terminal:

tmux kill-session -t breakfast
Enter fullscreen mode Exit fullscreen mode

The kill-session command needs a target (-t) and a session name that you want to kill. Note the -t (target) argument is similar to the attach -t command. In tmux, anytime you have multiple targets and you need to pinpoint a single abstraction (like a session name), you probably need to pass it a target (-t) plus the target name.

Anyway, running tmux kill-session -t breakfast will effectively kill that session. Note: killing a session terminates all processes inside it - make sure that you save all your changes.

The second method is to kill all the panes and windows inside a session until there is nothing left. We will go over panes and windows in a little bit. For now, just follow along. To kill a pane, press:

Prefix + x
Enter fullscreen mode Exit fullscreen mode

If you do that to every single pane and window, upon closing the last pane in the last window in a session, the session is effectively killed.

An alternative shortcut to Prefix + x is:

Ctrl + d
Enter fullscreen mode Exit fullscreen mode

It does the same thing as Prefix + x. Personally, I prefer Ctrl + d because it only requires two keypresses as opposed to Prefix + x.

Tmux Sessions Conclusion

We are done with tmux sessions. Let's recap what we learned!

We learned that a session is like a container to a set of terminals. You can:

  • Create a session in tmux with tmux new -s MY_SESSION
  • Detach from a session with Prefix + d or tmux detach
  • List sessions with tmux ls
  • Attach to a session with tmux attach -t MY_SESSION
  • List sessions and switch to a different session with Prefix + s
  • Kill a session with tmux kill-session -t MY_SESSION
  • Close a pane / window with either Ctrl + d or Prefix + x

If you are new to tmux, let this soak in. Take time to internalize this. Practice, practice, practice. Don't rush to the next one. It's better to go at a slower pace but to retain more than go at full-speed but forget almost everything.

When you're ready, let's learn about tmux windows!

Tmux Windows

Tmux windows are subsets of tmux sessions. When you're creating a new tmux session (tmux new -s MY_SESSION), what you're seeing is technically a tmux window. Earlier I said that a tmux session is like a container of terminals. While that was true, each terminal activity happens inside a window. So technically, a tmux session is a container for tmux windows.

You can have as many windows as you want in a session. Since a window contains a terminal on its own, you can have a window dedicated for running your application server, another window for logging, and another window for text-editing.

Many tmux window commands are conceptually similar to the session commands. You can create, delete, and switch windows, just to name a few. I won't go through every single tmux window operation because in my opinion, you can be very productive just by learning only a few of them.

Creating a New Window

You can only create a new window from inside a tmux session. Since it's almost lunch time at the time I wrote this, let's start a new lunch session. From the regular terminal (not from inside tmux), create a new session. This should look familiar:

tmux new -s lunch
Enter fullscreen mode Exit fullscreen mode

Cool! You're inside a session named lunch. You're looking at a tmux window. When you create a new session, tmux automatically creates a new window.

So right now there is one window - the default window - in the lunch session. To create a new window inside a session, run this command:

tmux new-window -n fried-rice
Enter fullscreen mode Exit fullscreen mode

This creates a new window named fried-rice (I had that earlier this week). If the new-window command is too verbose, you can shorthand it a bit with neww. Let's create a new window named ramen, because I'm craving that right now:

tmux neww -n ramen
Enter fullscreen mode Exit fullscreen mode

Practically speaking though, I almost never use the new-window terminal command. 99% of the time, I use the following new-window keyboard shortcut:

Prefix + c
Enter fullscreen mode Exit fullscreen mode

Try creating two or three new windows! Get this in your muscle memory. Window creation is one of the most often-used commands in my tmux workflow.

Switching Windows

By now you should have multiple windows in a session. Let's learn how to quickly switch windows.

Right now I have three windows:

  • the default window
  • fried-rice window
  • ramen window

I am currently on the first window. If I want to go to the *n*ext window, I can do that with:

Prefix + n
Enter fullscreen mode Exit fullscreen mode

Retrospectively, to go to the *p*revious window:

Prefix + p
Enter fullscreen mode Exit fullscreen mode

Another thing to know is that tmux window is indexed like an array data structure. In many programming languages, an array data structure is zero-indexed. Tmux window is also zero-based. The first window is window number 0, the second window window number 1, etc. To go to window number N, you can run:

Prefix + N
Enter fullscreen mode Exit fullscreen mode

Whereas N is the window number. So if I want to go to the first window, Prefix + 0 will do it. What if I want to go to the second window? Prefix + 1. Third window? Prefix + 2. In my regular workflow, I try to keep my window collections small. Normally I don't have more than 5 tmux windows in any session (if I have more, it can quickly get out of control). However, people's workflows are different. You may function better by having dozens of windows in any session. Use whatever works for you.

If that's the case, how can you quickly jump to window number 11? The Prefix + N shortcut only accepts one key. When you try to do Prefix + 10 (the 11th window), tmux thinks you're doing Prefix + 1. To quickly jump to double window digits, you can use:

Prefix + ' + N
Enter fullscreen mode Exit fullscreen mode

Whereas N is the window number. First, press the prefix. Then press ' (a single quote). This lets you enter any window index number, like 10. Then press Return.

I forget things a lot (my wife can vouch for that :D). I can hardly keep track of the 5 windows I have opened in the current session. I definitely can't keep track of all the windows across all my sessions if I have multiple sessions running. Luckily, you can quickly list and switch to any window in any session with:

Prefix + w
Enter fullscreen mode Exit fullscreen mode

When you do Prefix + w, tmux lists all the windows inside all the sessions you have (even the windows from detached sessions). Try it! I also use this command a lot.

Renaming Windows

A window name is used to identify what the window is about. It's good to give it a succinct name. If I'm debugging a controller test, then I'll create a new window named "controller test" (yes, not original, but it's concise!). However, sometimes your task changes. The "controller test" task may morph into "feature test" (shudders). In this case, you may want to rename your window. To rename the current window:

Prefix + ,
Enter fullscreen mode Exit fullscreen mode

Your cursor is now on the lower window area where you can edit the window name.

Deleting Windows

When you're done with a window, you may want to delete it so it doesn't crowd your session. There are two ways to kill a window:

  • kill the window with a terminal command
  • kill all panes inside a window

For method 1, you can run this from inside a window:

tmux kill-window
Enter fullscreen mode Exit fullscreen mode

This kills the current window you're in. If you want to kill a different window, you can pass a target option to the command -t. To kill the "ramen" window:

tmux kill-window -t ramen
Enter fullscreen mode Exit fullscreen mode

For a less verbose option, you can use killw instead of kill-window:

tmux killw -t fried-rice
Enter fullscreen mode Exit fullscreen mode

For the second method, you can run:

Prefix + x
Enter fullscreen mode Exit fullscreen mode

Or

Ctrl + d
Enter fullscreen mode Exit fullscreen mode

Tmux Windows Conclusion

We are done with tmux windows. Let's recap.

A window is where your terminal lives. A session consists of one or more windows. Some window commands that we learned:

  • Create a window with tmux new-window -n MY_WINDOW or Prefix + c
  • Switch to a different window with Prefix + n, Prefix + p, and Prefix + N (where N is the window index number, zero-based)
  • Kill a window with tmux kill-window -t MY_WINDOW
  • Close a pane / window with either Ctrl + d or Prefix + x

As always, take your time to internalize this. Practice what you've learned about windows and sessions. Try to create several windows, navigate between them, and delete them. Create a new session and create some new windows there too.

Don't rush. When you're ready, it's time to learn about tmux panes!

Tmux Panes

Earlier I said that when you started a new session, what you saw was a tmux window. I lied, again. What you saw was actually a tmux pane inside a tmux window inside a tmux session. When you started a new session, tmux automatically created a new window. When a new window was created, tmux generated a new pane. Just like how a tmux session has at least one window, a tmux window always contains at least one tmux pane.

Now that we've been exposed to sessions, windows, and panes, let's get the terminology straight. A tmux window is a container to tmux panes. A tmux session is a container to tmux windows. When you start a session, you have at least one tmux window. Inside a session, you can create many windows (Prefix + c). Inside a tmux window, you have at least one tmux pane. When you split a window for the first time, you are splitting it into two panes. Each boxed terminal is a pane in its own right. The hierarchy goes like this: Session > window > pane.

Let's go through some tmux pane operations.

Creating a New Pane

If you own tablets or large smart phones, they probably have a feature to split the screen in half so you can open two apps simultaneously. Panes are similar such that you can split the screen vertically (or horizontally) so you can have multiple terminals in a single window.

A tmux window can be split many times into smaller panes. The command to split a window horizontally is:

tmux split-window -h
Enter fullscreen mode Exit fullscreen mode

To split it vertically:

tmux split-window -v
Enter fullscreen mode Exit fullscreen mode

They are rather easy to remember: -h for *h*orizontal and -v for *v*ertical.

To split a window horizontally with a keyboard shortcut:

Prefix + %
Enter fullscreen mode Exit fullscreen mode

To split a window vertically:

Prefix + "
Enter fullscreen mode Exit fullscreen mode

Split windows are also one of my most frequently used tmux commands. Commit them to your muscle memory!

Deleting a Pane

To delete a pane is similar to deleting a window:

Prefix + x
Enter fullscreen mode Exit fullscreen mode

Or

Ctrl + d
Enter fullscreen mode Exit fullscreen mode

This is our third time seeing Prefix + x and Ctrl + d. Now you finally understand what this command actually does. It deletes a pane. When the last pane of a window is deleted, that window is automatically removed. When the last window of a session is deleted, that session is automatically killed.

Navigate Between Panes

Let's learn the different ways to navigate between panes. First, create multiple panes in a window. Use the Prefix + % or Prefix + & shortcut to do that.

To switch to the "next" pane, run:

Prefix + o
Enter fullscreen mode Exit fullscreen mode

Tmux focuses on a different pane each time you run that command.

You can also move directionally with:

Prefix + Up
Prefix + Down
Prefix + Left
Prefix + Right
Enter fullscreen mode Exit fullscreen mode

These commands give you slightly more control. Now you can instruct tmux which direction you want to focus on.

But that's not all. Tmux can also display all the pane numbers of the current window for you to select from. Run:

Prefix + q
Enter fullscreen mode Exit fullscreen mode

You should see numbers displayed in the foreground center of each pane. If you type a particular number, tmux will focus on that pane number.

So if I want to go to pane number 1, I just run:

Prefix + q + 1
Enter fullscreen mode Exit fullscreen mode

To go to pane number 5, run:

Prefix + q + 5
Enter fullscreen mode Exit fullscreen mode

Btw, you could also have done this with:

tmux display-panes
tmux select-pane -t N
Enter fullscreen mode Exit fullscreen mode

The first command lists all the panes and the second command jumps to pane number N. I personally would rather to Prefix + q + N. Ain't nobody got time to type all that! :)

Resize a Pane

I usually have about 2-3 panes in a window. But you may need to adjust their sizes. For example, the other day I had to debug a controller spec. I had the tmux window split horizontally into two panes: top and bottom. I had Vim on the top window and the terminal on the bottom for running the specs. By default, tmux splits them equally, but I prefer to have Vim to have more screen real-estate. To resize a pane, you can run one of the followings:

tmux resize-pane -D 10
tmux resize-pane -U 10
tmux resize-pane -L 10
tmux resize-pane -R 10
Enter fullscreen mode Exit fullscreen mode

The number 10 is a measurement unit (tmux "cells"). The -D/U/L/R options stand for Down/Up/Left/Right. Unfortunately, there are no keyboard shortcuts for this. You can create your own shortcuts to make resizing quicker. I'll show you how to do that in a later section.

Zoom a Pane

Another feature I find helpful is pane zooming. If you have four panes displayed in a window, each taking up 25% of that window total area, you can zoom your current pane into 100% and hide the other panes with:

Prefix + z
Enter fullscreen mode Exit fullscreen mode

Now it looks as if you only have a single pane, allowing you to view the current pane at maximum area. This is useful if you need to remove distractions and focus on a specific task at a specific pane. Then once you're done, you can de-zoom and return to the previous layout by running Prefix + z one more time.

Swapping Panes

If you have two panes in a window side-by-side, you can swap them with:

Prefix + {
Prefix + }
Enter fullscreen mode Exit fullscreen mode

You can also rotate the pane positions with:

Prefix + Ctrl + o
Enter fullscreen mode Exit fullscreen mode

Tmux Layouts

While we are discussing panes, let's learn about tmux layouts. because they are closely related.

"Wait a minute, what are tmux layouts?" Good question! Tmux layouts are pane arrangements to organize your panes in a window. Tmux comes with five ready-to-use layouts. If that description is ambiguous and you're still confused, don't worry. Just follow along and you'll understand it soon enough!

Create a new window. Then split it horizontally once (Prefix + %). Then split it vertically once (Prefix + "). Your window should now have three panes: one half on the left, and a quarter on top right and a quarter on bottom right.

Let's go back to layouts. Tmux comes with 5 different layouts:

  • even-horizontal
  • even-vertical
  • main-horizontal
  • main-vertical
  • tiled

Let's try them. Type:

tmux select-layout even-horizontal
Enter fullscreen mode Exit fullscreen mode

Note how your tmux window is now split horizontally evenly. Just like the name says, the even-horizontal layout re-arranges the current panes into equidistant horizontal panes.

Let's try the vertical one:

tmux select-layout even-vertical
Enter fullscreen mode Exit fullscreen mode

Let's try a different flavor:

tmux select-layout main-vertical
Enter fullscreen mode Exit fullscreen mode

Your tmux window is now split horizontally with the "main" pane taking up more space - while the other two panes are split evenly, taking up less window real estate. Let's try the horizontal version:

tmux select-layout main-horizontal
Enter fullscreen mode Exit fullscreen mode

Finally, let's see what the last one looks like:

tmux select-layout tiled
Enter fullscreen mode Exit fullscreen mode

Now your tmux window is split equally. If you had 3 panes, it wouldn't exactly be split evenly, but if you had 4 panes, it would split them perfectly equal.

You can toggle tmux layouts with a keyboard shortcut. Press:

Prefix + Space
Enter fullscreen mode Exit fullscreen mode

The first time you press Prefix + space, tmux goes to even-horizontal layout, then it goes to even-vertical layout, then main-horizontal, etc. It cycles between those five layouts.

Tmux Panes Conclusion

Let's go over what we learned.

You learned about tmux pane. A pane is where your sub-terminals are displayed through. A window can have one or more panes. Here are the commands you learned:

  • Split a window horizontally with tmux split-window -h or Prefix + %
  • Split a window vertically with tmux split-window -v or Prefix + "
  • Delete a pane with Prefix + x or Ctrl + d
  • Navigate to the next pane with Prefix + o
  • Navigate to another pane directionally with Prefix + Up/Down/Left/Right
  • Navigate to pane number N with Prefix + q + N
  • Resize a pane by N unit with tmux resize-pane -D/U/L/R N
  • Zoom/de-zoom a pane with Prefix + z
  • Swap panes with Prefix + { or Prefix + } and rotate panes with Prefix + Ctrl + o
  • Toggle layouts with tmux select-layout even-horizontal/even-vertical/main-horizontal/main-vertical/tiled or Prefix + Space

Tmux pane is a unique feature that differentiates tmux from most regular terminals. Usually, to start a new terminal, you need to open a new tab. But what if you need to have two terminals side-by-side? With tmux, you can easily split a window into two or more panes. You can even organize how the panes are displayed.

As always, take your time to digest what you just learned. Play around! Create multiple panes in a window, then navigate around them. Delete some panes. Resize them. Zoom in and out. Change the layouts.

Combine this lesson with what you've learned. Create a new session. Create a few new windows with multiple panes in them. See how fast you can go from the current pane to another pane in a different window from a different session. Do this until you can navigate around effortlessly. Take as much time as you need.

When you are ready, let's learn about tmux servers!

Tmux servers

By now, you should have a good idea what tmux can do. You should also understand how sessions, windows, and panes relate (otherwise, stop and review the last three sections). There is one more missing piece: tmux servers. Although you can do fine merely knowing about sessions, windows, and panes without knowing about tmux servers, it is good to know the whole picture to have a more accurate mental model. Understanding tmux server will explain why some commands, like tmux kill-server, exist (to kill the server, duh!)

When we run the command tmux, tmux not only creates a new session, but it also creates a new server instance. This whole ecosystem that you have been interacting with - you can think of it like the client in client-server interaction. If it is like a client-server interaction, then there must be a tmux server somewhere. Tmux server is what runs in the background while Tmux client runs in the foreground.

When we create multiple sessions: tmux new -s breakfast, tmux new -s lunch, and tmux new -s dinner, those three are connected to the same tmux server. Since we don't mention a server name, these sessions are connected to a default server.

To launch a named server - let's name it the food server - run this from the regular terminal:

tmux -L food
Enter fullscreen mode Exit fullscreen mode

This creates a new server named food. Running this command also launches a new session by default. Cool! Let's detach from this server (Prefix + d).

So we have a food server running somewhere in your machine, but you are detached from it. What if you want to reconnect to the food server? Run:

tmux -L food attach
Enter fullscreen mode Exit fullscreen mode

You'll be reattached to the food server. Wait a minute, this looks like the attach command from way earlier when we only had one session (tmux attach). It's because it is. The difference is that we are attaching back to a session from the food server instead of the default server.

Running tmux -L food creates a default session inside the food session. What if we want to create a named session? To create a new named session inside the food server, say a roast-duck session, we can run:

tmux -L food new -s roast-duck
Enter fullscreen mode Exit fullscreen mode

Recall that running tmux ls will list the sessions from the default server. But what if we want to view all the sessions in the food server? Easy, run:

tmux -L food ls
Enter fullscreen mode Exit fullscreen mode

If we want to attach to the roast-duck session in the food server, run:

tmux -L food attach -t roast-duck
Enter fullscreen mode Exit fullscreen mode

Finally, to kill this food server, you can run:

tmux -L food kill-server
Enter fullscreen mode Exit fullscreen mode

The kill-server command kills a server. When a server is killed, it kills everything inside it (sessions, windows, and panes) - make sure that you save everything before closing it. By the way, to kill the default (unnamed) server, just run:

tmux kill-server
Enter fullscreen mode Exit fullscreen mode

If you run tmux kill-server when nothing is running, you'll see:

no server running on /private/tmp/tmux-501/default
Enter fullscreen mode Exit fullscreen mode

Your exact path might be different from what I have, but the important ideas are:

  1. The command kill-server is looking in the tmp/ directory, which is usually where linux sockets are. Wait, did you say socket? That sounds like a client-server interaction!
  2. The directory is named "default" because we tried to kill the default server. By the way, if you created multiple servers, you'll find all those server names stored in here. So if you created a food server, you would find /food in there too.

Phew. That's a lot to take in. Let's practice what we just learned to solidify what we just learned. Do the following:

  • Create 2 new tmux servers (give them any name you want)
  • Inside each server, create multiple sessions. Inside each session, create multiple windows. Inside each window, create multiple panes
  • Detach from one server and attach to the other one
  • Delete the servers

Practically speaking, I almost never use more than one server in my workflow. However, your use case might be different. Regardless, it wouldn't hurt to know! The more you know, the less you don't know.

Customizing tmux

In my opinion, tmux could use a better set of keys. For example, I find that using the Ctrl + b keys are not comfortable. Let's change some key shortcuts.

You can modify tmux keys with the tmux config file. When you launch tmux, it looks for a file named .tmux.conf in the HOME path - in my case, it is ~/.tmux.conf. If it isn't there yet, create a file .tmux.conf in your HOME path.

Let's change the tmux prefix. Some of the popular prefix alternatives are:

  • Ctrl + a
  • Ctrl + Space
  • Ctrl + s
  • Ctrl + u
  • Backticks
  • Tab + key variation instead of Ctrl + key
  • etc

Personally, I use Ctrl + Space mainly because my Vim leader key is the Space key. But ultimately, use whatever keys that make the most sense to you.

To change your prefix to Ctrl + Space, add these in the tmux config file:

unbind C-Space
set -g prefix C-Space
bind C-Space send-prefix
Enter fullscreen mode Exit fullscreen mode

The changes above won't take place immediately. You first need to source the file. You can either close tmux (kill your tmux session and start a new one) or source the config file.

To source tmux config file, from inside a tmux session, run:

tmux source-file ~/.tmux.conf
Enter fullscreen mode Exit fullscreen mode

By the way, in the future, I would like to be able to source my configurations quickly with Prefix + r. Add this in the config file:

bind-key r source-file ~/.tmux.conf \; display-message "Tmux config reloaded!"
Enter fullscreen mode Exit fullscreen mode

Don't forget to restart the tmux session. Now you can just run:

Prefix + r
Enter fullscreen mode Exit fullscreen mode

To quickly source your file.

By the way, with some changes, you may notice that even running Prefix + r isn't enough. You need to also restart the tmux server. Detach from the session, kill the server (tmux kill-server), then start a new session. Alternatively, you can also run Prefix + I (note the I is uppercased) to reload the tmux environment.

In short, if running Prefix + r is not sufficient, try running Prefix + I also. If all fails, restart the tmux

Two more shortcuts that I don't find intuitive are the window splits, Prefix + % and Prefix + ". To me, it makes more sense to use | for a vertical split and - for a horizontal split. Add these lines:

bind | split-window -hc "#{pane_current_path}"
bind - split-window -vc "#{pane_current_path}"
Enter fullscreen mode Exit fullscreen mode

Now when I press Prefix + |, tmux does a vertical split and when I press Prefix + -, tmux does a horizontal split.

More configs?

There are many different ways you can configure your tmux config. To address them all would be out of the scope of this article.

If you want to learn more about tmux configs, I am currently working on another article to address tmux configs in more detail, so stay tuned! I will update this post with the link to the said article when it is done.

11/18 Update: Part 2 is out! Check it out here: Useful Tmux Configuration Examples.

If you want to see what my personal configs look like, check out my dotfiles.

Conclusion

Phew, we have now reached the end of this guide. I hope that you learned valuable things about tmux. If there is anything that is unclear, please feel free to drop by the comment section.

Here's the final recap. In this guide, we learned about tmux session, window, pane, and server. We learned about their relationships with each other. We also learned a little about configuring tmux. In the next article, you will learn more about configuring tmux in detail. I will update this guide with the link to the next article when it is ready.

More importantly, take your time reading this guide. It can be tempting to read through the entire guide without doing any practice. Take a day to read about a section and let it digest. Apply what you learn that day. Instead of reading for 15 minutes and practicing for 5 minutes, for every 5 minutes you learn about something, spend 15 minutes. Yes, it is a lot. Yes, you'll read less on average. But you will learn a lot more. In the end, what you learn is what actually matters, not the amount of articles/ books you read.

Happy coding!

Top comments (6)

Collapse
 
waylonwalker profile image
Waylon Walker

I really like the emphasis on using all the default configs here. I probably lean on custom configuration a bit much.

I only use one server, I feel like that is something that I could potentially use to add another layer to my workflow.

Collapse
 
iggredible profile image
Igor Irianto • Edited

Thanks! I believe the best way to learn tmux is to learn what it can do with minimal config.

I used a lot of custom config as well when I started. Turned out that more than half of the configs that I used I didn't even need them - other times I found that I preferred the native behavior. That's why I believe one should customize tmux only after exposure of what tmux is truly capable of.

And yeah, I use one server 95% of the time too. I must be really careful when I add another server because it could get chaotic, fast! But it's always good to know that tmux can use multiple servers.

Collapse
 
omi profile image
Omi

this is my first time learning tmux and im glad that i read your i got a lot of thign

Collapse
 
os3albert profile image
Albert

omg great! I use as you do the C space to able commands. anyway thank you for sharing your workflow

Collapse
 
ojpro profile image
Oussama ELJabbari

Thanks, this article was very useful for me, I learned a lot of things.
I think now I will be able to exploit the power of tmux more effectively.

Collapse
 
h__123 profile image
h__123

By far the most comprehensible tutorial in tech I've read in my life. Thank you to put effort into writing this.