Developer Productivity

Basic tmux Commands

ThePrimeagen

ThePrimeagen

terminal
Developer Productivity

Check out a free preview of the full Developer Productivity course

The "Basic tmux Commands" Lesson is part of the full, Developer Productivity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through using various tmux commands including starting and stopping a server, listing sessions, reattaching a server, creating a new detached session, moving between sessions, detaching and reattaching from tmux sessions.

Preview
Close

Transcript from the "Basic tmux Commands" Lesson

[00:00:00]
>> Let's do it, let's start tmuxing, I feel so excited. So the first thing we're gonna do is execute this command, tmux list-sessions. So go tmux list-sessions, so as long as you closed down everything properly, you should see this. No server running, there's nothing here, it's just nothing's happening, all right, awesome.

[00:00:21]
And if you actually got excited and press tmux and you wanted to kill it, do tmux kill- server, it'll kill everything. So just make sure you're in the know, server running item here. All right, so let's just do tmux. I actually jumped ahead and already drew what happened here, so let's do tmux, all right, awesome.

[00:00:38]
And we already did this drawing here, right, yeah, we did this drawing. So if I were to execute tmux again, I think we can probably all guess what would happen, but let's keep on going. Let's not get ahead of ourselves. So let's close the terminal. And when I say close the terminal, what I want to make sure that you do is that you don't do Ctrl+D, literally close the terminal.

[00:00:59]
So I'm gonna just close it right here. It's gonna say, hey, do you wanna close this terminal? Yes, I've pressed x, it's gone, there's nothing left. I'm gonna reopen it back up. Now I have a brand new terminal open, and this does not have tmux running right, it's just a terminal.

[00:01:13]
I've closed off that previous one. And we're gonna do this, tmux a or tmux attach, tmux a. Notice we went back to our previous session, so though I close the program, because tmux runs a server, the server lived on, the server kept on going. And now when I tmux a, I've reattached to that server, which happens to be in a session, and I'm on window 1.

[00:01:40]
All right, awesome. I don't have to have my terminal always open, right? It doesn't have to be open for things to be running. I can literally leave it, close it and never look at it again, right, awesome. Okay, that's pretty neat, that's pretty neat, so let's talk about a little bit of terminology here.

[00:01:58]
The prefix key, prefix key is really important to understand what it is. So if you are using tmux with no tmux configuration, it is 100% likely you have a prefix key of Ctrl+B. You press Ctrl+B and that will be your prefix. Now, you're probably saying you keep saying this term, I don't know what you're trying to tell me, I don't get it.

[00:02:19]
Well let me draw up something like this. So here is tmux server, here's our terminal. I press Ctrl+B, well, what happens? Well, normally what would happen is this would go into your terminal and your terminal would interpret it the way, whatever actions are determined by your terminal, since we're using tmux and all this.

[00:02:38]
Tmux is gonna to take in this key action here and go, all right, what key did we just get set? Let's just say it was Ctrl+B, our prefix key. It's gonna say, it's Ctrl+B, you know what we're gonna do? We're not gonna send it to the current active, actual running item underneath.

[00:02:56]
Instead, we're gonna hold on to this cuz the next key coming is gonna be a command for me to do. So then let's say you press D afterwards, it's gonna send the D through here. And when the D goes to the tmux server, it's gonna go okay, since we're in command mode, I'm now gonna perform action D, and it's gonna look up that action, perform it for you.

[00:03:17]
And D does actually have an action. Whereas if you were to type say just D by itself without Ctrl+B first or your prefix key, it would send it to tmux, tmux would then send it to the active session which would send it all the way down to your focused window.

[00:03:34]
And then D would come out, and look, here am I, right, you would see it. And so you don't realize you're sending it through all this information and all these things are happening, but it is what's actually happening. So there is some level of interpretation slash caching that happens somewhere within all these processes you just don't only realize it.

[00:03:51]
And your key to changing it is your prefix. Now a minus prefix a, why did I choose a? There's historical reasons, blah, blah, blah, blah, blah, I kinda look back at it and I kinda wish I would have kept B. I'm actually considering changing it back to B just so I'm more like all the other ones, so when I jump on, say someone else's tmux, it feels fairly comfortable.

[00:04:11]
I don't have to, I can't use my normal, I don't wanna have to think about it, right? It's kinda silly to change some of these defaults, I probably should have done that. But anyways, that is what's happening with the prefix key. I wanna make sure you understand that.

[00:04:22]
So now we have tmux open, I keep saying this word session/window/pane, it hasn't quite become clear yet on that. I've now talked about this prefix key which is like a command key to be able to send commands to tmux. Let's start bringing this all together. All right, so first, let's come back here and let's just type in here, tmux kill-server.

[00:04:40]
That's just boom, kill tmux, it's gone, you should be exit back out. Now you're just back out in your terminal. Okay, awesome. Next, let's redo lists sessions again just to prove it out, tmux list-sessions. By the way, if you're wondering how I'm kind of completing things so quickly, if you have zsh installed, it's kind of part of zsh's default behavior.

[00:05:01]
If you start typing and then you press literally up arrow, it will complete it based on your history at that point. So tmux l, if I press up, I always usually do tmux list-session. It'll get it all there for me right away, so I can just type part of the command and press up and it'll just jump through the history like that.

[00:05:17]
Awesome, okay. Nothing is running, okay, perfect. So then let's type in tmux and let's create this. So tmux, all right, we're back in tmux, awesome. Now let's do that tmux list server again. Now notice what happens this time, a little bit different. Let me kind of walk you through what's happening cuz there's two things that are happening right now and they're both saying the same information, I just wanna make sure you see it.

[00:05:41]
One, it says, I am session 0, this little 0 right here, look straight downwards, you will probably see down here a 0, meaning hey, this is session 0. Okay, 0, 0 awesome, I can see that right here. Then it says 1 windows. The first thing you're probably thinking is wow, pluralization is hard.

[00:06:00]
Pluralization is really, really hard, give them a break, okay? It's just windows, why? I don't know, and notice down here mine says 1, yours probably says 0 if you have no config. It starts at 0, I hate that word, starts at 0 then 1, so I just made it start at 1, makes more sense.

[00:06:18]
Then lastly over here, it says attached. This says hey, you're attached to this session currently, which also implies you can detach from sessions, right? So that's kind of what is happening in this little list sessions. So let's try executing tmux, again, tmux. Now, if this behaves like this, I would assume that it's gonna send a command here, say do I have a server?

[00:06:44]
I already have a server, let's create a new session with a new window with a new pane, right? That's what we would probably all think would happen. So let's do it, tmux, run, that's not what happens. It's trying to start a new tmux server in a tmux instance, that's no good.

[00:07:02]
You cannot do that, I mean, technically you can, but don't do that, okay? It's bad news bears, you don't wanna be a part of it, so don't do that. It prevents you from doing it, which is good, awesome. And the same thing happens if you go tmux new- session, it will also say, hey, you cannot nest tmux sessions in themselves.

[00:07:22]
It's very confusing, it would be kind of a bizarre experience to be able to do this, so it's good that you don't do that. And so that errors out, but you know what we can do? new- session -d, so let's try that again. Let's try new session but this time let's put a -d.

[00:07:38]
Now before I hit Enter, what do you think is gonna happen? I want you to think to yourself and try to come up with something, all right? Did that do what you thought it did, which is apparently changed nothing? It didn't give me any errors? I don't think so.

[00:07:56]
Well let's do this, let's do another tmux list-sessions. Now look at what we see, we are still attached to session 0, but now there is a session 1 with 1 windows. We're not even looking at it, but it does exist, we just don't see it. All right, what was the question?

[00:08:15]
>> Several people said, detached or creates a new detach session.
>> Yes, it creates a new detach session, that is precisely correct. That is exactly what's happening and you could see that, right?. You can definitely see, okay, I'm still in the same session, but there is a new session somewhere else.

[00:08:31]
Now, how do you get to that new session? Well, currently, those are mysteries, so let's not worry about that quite yet. All right, navigation tip, let's worry about that right now. Now that we've done that, you can go to the next or previous session by pressing prefix. Now remember, yours is likely Ctrl+B, mine is Ctrl+A, so I'm gonna go Ctrl+A or prefix and open bracket, or open parentheses, the sorta curvy ones, and it'll go to the next one.

[00:08:57]
Notice in here, there's no text, so now let's do tmux list-sessions again, you'll see, look, I'm in 1 and I'm attached to it, you can see the attaching. All right, I can see these things, I can, okay, we just moved forward. So I'm gonna do it again, tmux close parenthesis, I went back to that one, and now you can see I can go back and forth between them.

[00:09:19]
I can even just press open parenthesis over and over and over again, and it's just going back and forth between them. Make sure you know how to do that, that's pretty cool, right? It's pretty cool, but don't worry. This feels not that cool. A, if you don't use the terminal that much, and you're like, that's not that cool.

[00:09:33]
Trust me, it's gonna get a lot better, this is just, I wanna make sure you start seeing, okay. So now you should see, all right, I know about the server, the server made sense, but now I see there's actually sessions. Sessions are different places where I can put input into, okay.

[00:09:48]
You're starting to see it, right, it's starting to come into hopefully better understanding. All right, so let's close the session and let's create a new session. So let's go tmux kill-server. [SOUND] That's too bad. Let's go tmux new-session, all right. So now we have a brand new one, it's just the way it was.

[00:10:08]
We can go tmux list-sessions just to look at it. Notice now we're back down to only a single session, we're attached to that one session. All right, good, we're brand new. So this time let's close our tmux, but let's use prefix D instead of doing Ctrl+D, so prefix D.

[00:10:26]
Remember prefix D will be Ctrl+B D if you're on an unconfigured tmux, for me it's Ctrl+A D. Now notice what happened, it's like I did tmux kill-server except it simply said detached. And when I'm on the outside, I can do tmux list-sessions. And you'll notice that it shows me the sessions that are currently running, but nothing has the word attached to it.

[00:10:49]
It's because I'm now detached from tmux, this really strongly shows the server nature of tmux, how it is truly two different things right here. And now you can kind of really feel the difference between tmux sessions and server, you can kinda see that interplay. I'm trying to draw a picture here and we can reattach to it with tmux a, or you can say attach all the way.

[00:11:10]
If you're kinda more of a, I like to get force push to master. I don't like to get push -f, I like to really get the full words in there. It feels better, feels more powerful. And so I'll attach with using the full spelt out version, there we go, we're back in.

[00:11:25]
You can see my previous command that I executed there a little bit ago. And if we were to re-execute it, says the same thing, awesome.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now