Developer Productivity

Basic tmux Commands



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.


Transcript from the "Basic tmux Commands" Lesson

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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?

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.

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.

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.

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.

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?

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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