Developer Productivity Developer Productivity

Windows & Sessions Management with tmux

Check out a free preview of the full Developer Productivity course:
The "Windows & Sessions Management with tmux" Lesson is part of the full, Developer Productivity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen demonstrates how to view a list of all current sessions and windows, create and close windows, navigate between windows, name sessions and windows, split windows into panes. Student questions regarding when would split windows be used in tmux if you are using i3 and if there is a way to jump between named sessions.

Get Unlimited Access Now

Transcript from the "Windows & Sessions Management with tmux" Lesson

[00:00:00]
>> I know, is moving slowly but it's also moving awesome. So hopefully you're like, okay, okay, I feel things. I feel and I'm kind of excited. So let's create a bunch of windows for a quick more or let's create a bunch of, let's see Han, oopsies, create a bunch of sessions here.

[00:00:14] I don't know why it says create a bunch of windows, I got confused cuz we're only talking about sessions right now. So what we're gonna do is we're gonna go tmux, new-session with that little d. And then I'm gonna do it a bunch of times, right? And now, we're gonna use this little navigation tip right here, prefix w.

[00:00:31] Now, prefix w will show you a kinda list of all of your sessions in Windows. Now notice that there's one window per every single session, and I can just walk through them. Look at me, I'm going up and down. This is pretty inconvenient form of traveling through your windows and sessions, but you could, I'm going through them all.

[00:00:52] Awesome, look at that, I could go select this one, I'm in a completely different one. I am number 1. And so after doing that, I can do that little w, and you can actually see right in here, you can see command I not found. That was the last command, if I zoom it out a little bit, you can see I am number 1 as the last command there.

[00:01:09] Whereas this one shows all those ones, you can kinda get a little preview window of what's happening. I'm really zoomed in for everyone looking so you don't get to have as nearly as much viewing. And so then these ones are all blank. I can kind of walk through all these different ones.

[00:01:22] Awesome, I can see all the different sessions, great. Now, we can do a little killing of each one of those servers, remember Ctrl+D is end of file, prefix D is tmux attached, very, very different. So if I'm in here and I do Ctrl+D, it obviously kicks me out of tmux.

[00:01:39] If I just simply do prefix D, it just backs out, and let's say I'm attached the tmux. If I just type in tmux detach, it's the exact same thing as Ctrl+D. So you can see, this is highly scriptable. You could actually kick people out of tmux sessions if you wanted to.

[00:01:56] So let's do a little tmux kill server again, if you followed along and made a bunch of servers. Let's clean everything up. Let's start again, start again. So, obviously it's very difficult still, I haven't really talked a lot about windows and panes, but it's kind of hard to see the difference between all these sessions, windows and panes.

[00:02:14] And so even with that exploded view where you could walk through each one, you'd have to walk to each one and then look at the input. Not very convenient but it is a way you could do it. So let's start talking about the windows. Obviously, that's kind of the next thing we need to talk about.

[00:02:29] So I already did this, we already cleaned it up, you can execute it again just to really assert your domination over tmux here. You will make it kill the server twice. So let's create a new window. So first let's do tmux, awesome. We're exactly what we've done many, many times here.

[00:02:46] List session, you can see it, one windows attached. Now let's do this, tmux new window. Now Enter, look at what happened. Did anyone see what just happened there? Hopefully what you saw is that all the input on the screen went away. But what you really should have been looking at is right here.

[00:03:03] Now notice what it says, session zero has window 1 and window 2. So now hopefully you're starting to understand what windows are. Now it's starting to kinda make a little bit more sense. Just like a server can have many sessions, a session can have many windows, it's the same principle effectively again.

[00:03:23] I mean, they're effectively identical to each other. This operation tmux new-window is identical to you pressing prefix c. So let's do that right now. I'm gonna go prefix c, prefix c, prefix c, you can just see that number going up. I'm on windows seven now at this point, I have a bunch of different windows just by pressing prefix c, or for you likely Ctrl+B, C.

[00:03:46] And that makes sense, prefix create is how I kinda say it in my head, awesome. Now if you wanna navigate around to these, you have a couple of options. You can do prefix p to walk backwards, you can watch my little asterisk. Now it's on 5, it's gonna go to 4, 3, 2, 1, or I can press prefix n for next.

[00:04:05] So much like how you transfer sessions using prefix open and closed parentheses, whatever they're called, parentheses yeah, you can use p and n to do it on windows. Or what makes it a little bit easier, let me zoom it out a little bit, I can also just press the window number.

[00:04:19] So prefix one will take me to window one. Prefix two will take me to two, prefix seven will take me all the way over to seven, you can see my little asterisk right there, there we go. So now I'm jumping to the window I want by just pressing its number.

[00:04:36] Awesome and when I press Ctrl+D to close it, it happens a little differently this time. Before when I press Ctrl+D, it would not only kill my window which would thus kill my session, which would thus kick me out of tmux even if there was other sessions to be had.

[00:04:51] This one it will close the window but it will go to my previous window so I just pressed it, I killed seven, I'm over here on two. I call these things usefulness tips. I didn't know really what to call it, but a session should contain like items. So if you have a project that requires two terminal things running, have your product, just have a session with those two things in it.

[00:05:12] This will become more clear as we kind of extend to tmux further, but you should really just try to contain like things. For a long time, I'd have a single session and I'd have it auto populate all my projects the exact way I wanted them. And though this was really useful, I just found myself scrolling too much, I didn't really like it as much.

[00:05:32] I've kind of evolved since then and went, okay, I'm gonna find a better way to navigate my sessions. And thus all my projects can have as many windows as they want, but I will know exactly how to navigate them. So sometimes you do want to ask this question, who does number 2 work for?

[00:05:49] It's a very important question to be asked. And you don't know, because you're just looking at these windows, none of them are making sense. There's just a bunch of numbers by them and all the sessions this just have a bunch of numbers by them. So let's try something a little bit different.

[00:06:04] Let's go back here and go tmux. Now I'm currently in the second window of my first session, let me zoom back in. I'm gonna go new session, now remember we need to do dash d for detach. Now I'm gonna go like this, dash s and I'm gonna give it a name.

[00:06:17] Let's give it the name, foo. So now when I press Enter, I created the session, detached. Let's press prefix w to look at everything, here's our first session with six windows. And then we have our second session, it's named foo, it's not named one, it's named foo as a single window.

[00:06:36] Now when I open it, instead of saying the number one right here, it says foo. I can start naming my sessions, this is really, really good. And this is kinda where things get a lot more exciting. And you'll see why here shortly. And so now they're a little bit more meaningful, little bit easier.

[00:06:54] And you can do the exact same thing with windows. I can go tmux new window, and then I can give it a name, foofoo. And now look, window number 2 is named foofoo. I can actually give them the names that I want. You can even create a tmux new session.

[00:07:12] And you can also give the initial name to the first window, so that way you don't have something silly. Let's call this one bar, there we go, let's bring this back up. And you'll notice that if we go to bar, bar has one windows and its windows' name is aoeuaoeu.

[00:07:27] Which of course is the first four characters of Dvorak. By the way I used the Dvorak, have I by the way that one yet? All right, it's good to know, got always by the way people. So we're back here, this is awesome, usability tip and really this is not just a usability tip.

[00:07:42] This is actually something that's pretty exciting. Let's jump back here and go like this, new session. Let's just re execute this, duplicate session bar. So hopefully, right away your little programmer brain's going, okay, okay, we can do something special with session names. They're like key values, I can now access kind of a map of things that are running.

[00:08:02] Very important to remember that, put that up here for now. So obviously sessions cannot have the same name, very, very good. There are pains, I don't like them, I personally like to say pains or pain in the ass. I just am not a huge fan of them, but I will say this much, you can open them pretty easily.

[00:08:20] Prefix percent sign will split your window in half. So look at what I just did here. So I'm in window foofoo, but I actually have two things running. So I have already have this setup inside my tmux RC to do this is I can press prefix h, prefix l to walk between these two, vim command, vim movements, right there.

[00:08:39] And so I can actually walk between the splits. And I can be over here and I can walk over here and do this for people that like that split lifestyle, which I don't. You can have it right here, me, not so much for me, so if I press Ctrl+D, it'll get rid of one of them.

[00:08:54] I'm still in window foofoo. I haven't changed at all, I just had multiple splits there or panes. So hopefully at this point, this whole notion of server window pane makes a lot more sense at this point, because you saw that I had a single window with multiple panes.

[00:09:09] And panes really were the thing that are running the shell. This is two shell instances side by side in a single window, the window being what is being displayed right here. It is my terminal. And then of course it lives within the session foo, which is one of many sessions.

[00:09:27] I think at this point it's probably a good idea to say, hey, is there any questions kind of about tmux in its basicness? I just wanna make sure that we don't have any confusion out there. And ultimately, I think what is really important is that, if you have a question, somebody else has the same question.

[00:09:43] They're just not willing to ask it, so, ask it now and we can just get it over with, and hopefully this little diagram I have right here makes a lot of sense now. When I first showed it to you, probably made no sense. Now you're like, okay, I get it, maybe I don't remember all the keys I have to press, I don't remember all the commands I have to type in.

[00:10:01] But I get the idea and that's the ultimate goal here. But now here's the question, I gotta ask this question cuz remember we want to solve problems, I don't want to show you technology for the mere sake that it's neat. I'm not into neat technology, I'm into useful technology.

[00:10:18] And I wanna be really specific about that. We got a question, William, what is it?
>> Two questions actually, when would you ever use windows in TeamHooks if you're using i3?
>> So that's a good question. So I mean, that's kinda the reason why I don't, some people like things side by side.

[00:10:35] And the reality, for those that don't know when he said i3, he's talking about the window manager. So here let me close that, I can actually just open up two windows side by side right here. And look at that, they're kind of next to each other. Personally, I don't use splits.

[00:10:49] So I don't have a strong answer for why you would ever want to use splits, let alone why would you use one over the other? I personally would rather use i3 to do a split than tmux. But tmux also has a lot of really great programmaticness to it that's really simple.

[00:11:05] And so I think you could probably do cooler things with tmux with less effort than you could with i3. At least as far as my experience goes, i3 is not as beginner friendly as tmux is. Tmux is fairly beginner friendly, it has a lot of easy pass, a lot of cool plugins, that just kind of work.

[00:11:24] Here's one of the things, never mind, I don't wanna show quiet, I almost got ahead of myself, I get so just excited. But yes, I virtually don't use splits for anything. My whole life has been driven around trying to create navigation which I don't need splits to begin with.

[00:11:39] So I'm probably not the best person honestly to ask that question, I'm sure someone else would probably have a much better answer than me, why someone would or would not want splits. Cuz even when I'm copying code and doing that, I don't still use splits. I still hop between two windows really, really fast.

[00:11:54] So anyways, my question really, wait, you had a second question, William. What's the second question?
>> So the other question I had is when you have named sessions or windows, is there an easy way to jump to it by name like a search?
>> I love that question, and here's the answer.

[00:12:16] Absolutely, and it's so good that right after this, we're gonna make some things in tmux that are gonna be pretty awesome. And so I wanted to first just kinda set up that and I'm very happy you asked that question cuz that's exactly what you should be thinking. You saw that you can't have duplicate session names, that should immediately trigger in your head, okay, there's some sort of programmatic nature to this whole thing.