Check out a free preview of the full Developer Productivity, v2 course
The "Setup Tmux" Lesson is part of the full, Developer Productivity, v2 course featured in this preview video. Here's what you'd learn in this lesson:
ThePrimeagen discusses using Tmux for managing terminal sessions, windows, and panes. He also demonstrates various Tmux commands and shortcuts, such as creating new sessions and windows, navigating between them, and sending commands to specific panes.
Transcript from the "Setup Tmux" Lesson
[00:00:00]
>> The Primeagen: Whenever the last time I did the developer productivity course, I did a version of this, but things have changed a little bit. And I think I've made it a touch more robust, found some kind of funny bugs that exist within the tmux world, which means that if you translate this into Zella J, it's probably a little bit better.
[00:00:15]
Maybe there's some things you don't have to worry about. So if you wish to enjoy some tmux, you can install it with any of these various distributions. Now, remember, if you are going to do that, you might as well make part of your dot N files, the things we already built, create your own little tmux installer script or call it like your dev util script, something in there to do it all.
[00:00:33]
And then I also have this to kind of get up and running. This is like a basic config for tmux that I think is good. There's a couple of weird things about tmux, like one thing, your first window is in position zero, which zero is way over here on this pinky.
[00:00:46]
And then then it starts as one, two, three, four or five, which is just kind of weird to have one, two, three, this just a weird feel. And so maybe TJ is right. Maybe we should start at one for all of our indices. Maybe Lua got it right all this time.
[00:01:00]
Second off, I like Ctrl+A versus Ctrl+B. You can just leave Ctrl+B as your prefix key. We'll talk about that in a moment. And also, whenever I go into copy mode and I want to navigate around in my terminal, I have Vi keys, meaning that when I'm in here, if I go here and we can just do a bunch of these.
[00:01:20]
I can go in here and then navigate through what I've done so far, and I go in here and highlight it and yank it. Now I have that copied. So I like that experience, right? So I want to have kind of your basic Vi keys of being able to do control up, control down.
[00:01:35]
These, be able to highlight areas, navigate around while highlighting and then yank the things I want. I find that very nice rather than going, scrolling up and then finding the one you want, and then going in there and copying it, and I didn't even do right. And then you get the whole idea, I don't like that experience.
[00:01:52]
I just wanna use keys to do it, because I'm faster on keys. There we go. And then, of course, this little nifty one, I think is pretty clever, which is that we're going to be kind of changing our tmux config if you're following along. So binding out R to refresh your config.
[00:02:08]
So that way as you're running, you can just say prefix r and it will refresh your config. And then now you're up to date with your latest version of whatever we're doing. I kind of like that. Makes me feel good. Makes me happy at least. So what I'm gonna do here is I'm gonna just do the following, I'm gonna take this nice little thing right here and I'm gonna copy the whole thing and I'm gonna create a new script right here and I'm gonna, whoopsie daisies here.
[00:02:31]
Let me just go like this. I'm gonna go dev end really quickly, cheat for a quick second, have my kind of vim experience, right? And I'm gonna put this in config I'm gonna create tmux, I'm gonna go in here and I'm gonna go tmux.conf. Notice that we're actually good, the thing we've already built, we're using it, live action, it feels good.
[00:02:50]
I'm also gonna rename this to nail them too, just so I don't own myself every single time I refresh. And so, that way I can keep my current one. Yeah, yeah, I think that's fair. I think that's fair. Good answer. Good answer, right? This is family feud. All right, so there we go.
[00:03:05]
So I'm going to execute dev-env. There we go. I'm still copying over that special one, but you'll notice that I'm now copying over this tmux one. So now my tmux config is this old version. I'm just going to do a quick tmux kill-server. So this is fantastic. So now we have it set up that I have this version down.
[00:03:21]
So if I go in here and I go, cat -/.config/tmux/tmux.conf, this is literally the one I said. Notice that I haven't even updated this part right here. So let's kind of fix that. Let's go over here and I'll go, what is it called? Dev-env, and we'll even build this old navigation here in just one second.
[00:03:40]
You'll notice that this now looks officially different, it's bright blue. We're in a different world. All right, fantastic, and I'm gonna go here, and I want to go XDG_CONFIG_HOME/tmux/- what is it called? It's called Nice. I'm gonna go, I always forget that one. That's such a funny name, right?
[00:04:07]
All right, it didn't even save it. All right, XDG_CONFIG_HOME/tmux, that took me way too long. Wow, wow, and I'm going to go dev-env, there we go. Now we should have that I should be able to space R, no such file. Let me just make sure that this all works.
[00:04:29]
Tmux kill-server. And now if I do a new tmux session, I should be able to go leader R, configuration reloaded. So if I make a change now, we got it all done. We're now running, we're now in the future, things are feeling really good, so I'm going to leave tmux for a second and we're going to talk a little bit about it, okay?
[00:04:46]
So if you've never played around with tmux, it's actually quite fantastic. So you can just execute tmux, it'll just open your current directory you're in, and this creates a new session. A session is comprised of one or more windows, a window can have one or more pane. So if you think about it, the pain is this thing where we're actually doing this in.
[00:05:04]
The window is this right here. Notice that it says one and zsh, zsh is the program that's running, and then 0, this is the session. So session, window, pane. Just kind of intuitively makes sense, like everything's kind of set in that kind of motion of being three deep, and so if I wanna create a second window, I go prefix c.
[00:05:21]
So now depending on whether or not you used my script right there and you have Ctrl+B remapped to Ctrl+A, then Ctrl+A is your prefix key. So in tmux, it intercepts all of your keys and then forwards it on to the underlying terminal. But if you do Ctrl+A, or whatever your prefix is, it holds on to that until you do an action, so it knows okay I'm now in tmux operating mode.
[00:05:45]
I will let you do whatever you need to do and forward on that action to tmux, or if you do it twice, so if I go in vim. If I open up this thing, go test. If I go like this, I go Ctrl+A, so Ctrl+A will automatically increment the next number in vim.
[00:05:59]
So I have to do it twice to pass it through to the underlying terminal. So there you go. That's a prefix. So everyone knows what the prefix is, feeling good. And notice when I did p prefix c, it creates a new window. So I've created two new windows.
[00:06:14]
So now I have 3, I have 2, and I have 1, which means I can go prefix two, and I'll navigate to my second window. Prefix one, I can navigate to my first window. So you can have these nice little navigation. You'll see a nice little thing right there.
[00:06:28]
So these are effectively tabs. So now I'll notice that I have multiple windows in my one session. That's pretty nice, right? So that means I can go like this, tmux d to detach. Let's see Ctrl+AD to detach. And so now that I've detached from the tmux, it's still running.
[00:06:45]
So whatever's going on there, it's still running. It's still living its own life, which means I can see the personal and I can do a new tmux experience. So now I have one tmux running over here in a certain directory, and now I have another one in a different directory in personal that's running.
[00:07:00]
So you're, okay, this is pretty nice. So that can have my own little windows over here and this one in the personal, and I have one in my home directory. It's pretty nice, so I can attach, you can detach. If I detach, I can go tmux a, which is short for attach, and if I attach, it'll attach to the previous last one that was running.
[00:07:16]
So that one's the one that I went to personal and did. Okay, pretty nice. I can also show all running sessions. Prefix w shows me that I have this one right here, and I also have this one right here. So you can kinda see them, you get a nice little view into it.
[00:07:30]
You can see, okay, here's all the things that are happening. Here's all those. So it's kinda nice to see what's available. I can press X which will say, hey do you want to kill session 1? Yes, well I was just In session one, so it booted me out of tmux, of course.
[00:07:42]
So that means I can go tmux attach, and I can attach to that other one, right? And so I can now go that I can do a prefix w, I can see what's running. So you can kind of like work around and play with tmux, it's pretty nice.
[00:07:55]
You can also create panes pretty easily. If I go prefix quote or double quote, it creates a horizontal pane. So for a lot of people, this might be kinda weird, because you'd say, hey, man, that's pretty vertical in my opinion. You're right, it's vertically stacked, horizontally split. So you gotta remember that, that's always just the worst.
[00:08:16]
You can do the same thing with prefix percent sign. That does a vertical split. Again, horizontally stacked, vertically split. Ctrl+D kills the actual underlying terminal because you send the end to file experience. So it also closes the pane. Ctrl+D also closes the pane. I can do it again.
[00:08:34]
And now I've closed that previous window. I can close window two, I can close window one, and now there's just no more tmux. I just end of file, each one of those. So it's just terminals. That's all it is. It's nothing kind of wild is going on here.
[00:08:47]
But I will say this, so let's jump back in and let's create some of these to navigate around in here can be a bit cumbersome. So what I like to do is I like to have vim similar controls where I can do prefix h and it will jump over to the one to my left.
[00:09:05]
There you go, that's that direction left. I can do a prefix k and go upwards, or is k up or down? I can never remember. It just is inherently inside my head. I don't have to think about it. So I want to be able to go prefix k and have it jump up.
[00:09:16]
And so to accomplish that, if I scroll down here, I can actually take these, and I can add those to my my little tmux config, which we should go cd dev-. And if we're doing this correctly, if we're doing this the right way, now, notice this is like crazy right here.
[00:09:32]
I'm pretty sure I could do that, yeah. Prefix z will just zoom in that one pane make it full level. All right, so if you're working in a bunch of panes, you can do that. And so I'll go config/tmux, don't worry, there's a point why I'm showing all this.
[00:09:45]
There we go. Add these nice little things right there. Do a little dev-env to update everything. And then a little prefix r to refresh. And so now if I were to Ctrl+Z, go back here, I should be able to go Ctrl+. I'm now over here, Ctrl+HK, I'm now up here.
[00:10:01]
So now I have the same kind of vim style controls to move around. Feels really easy for me in my very small head to be able to do that, because with vim, it's Ctrl+W, for window, K or J to be able to move around. So it's the exact same idea, except for I'm not just using prefix that to jump around.
[00:10:17]
Okay, fantastic. I want to show you all that because mostly I never use any of those things. I use prefix c, all that. The reason why I tell you all that is that, a, this kind of accomplishes some things that I want. When I close a terminal, I can just kill my terminal right here.
[00:10:37]
And I can say, yes, I can kill that terminal when I bring it back up, and I go, tmux, I'm now back in, right? So it gives me 0.1. This should be 0.3. I have tabs within sessions, right? So I can do that prefix c and create a new tab.
[00:10:52]
So this is two out of my six points, just having tmux. And the reason why I showed you all the rest of it is because everything that I did you can just program it. That means you can have a bash script to do all the things we just did, which means that you can set up and create terminals the way you want, configure them to be a certain size.
[00:11:11]
You can even send over resizing amounts to make it into a certain amount of width characters and do all that. So it's pretty magical. So let's kinda play around with that. So I'm in a session right now. So one big no no with tmux is that if you go like this, you cannot create a session in a session unless if you like mark around with tmux internals, you unset this environment variable.
[00:11:35]
I just recommend you don't need to do that, you know why? Because if you give it the old -d, that will detach it, which means it creates a new session just detached from your current one. So now if I go Ctrl+AW, you can see I have a nice little detached one right there.
[00:11:50]
I can do it again. Do it a bunch of times, Ctrl+W, you can see I have a bunch of them, right? Okay, this is getting a little bit easy. Now we're kind of seeing where things are going. Okay, so now we can build something off of this, right?
[00:12:02]
Maybe we can even build something as good as that, right? That's what we're hoping to build right here. Notice that I navigated to a directory, which is my own dev deck directory, it opened up vim, and it also gave me a scratch window right here. Everything feels ready to go.
[00:12:16]
I like the feel of that. It's fast, it's nice, it's smooth, and I want it for everybody. Okay, so you get the idea. And so there you go. We can also list session, so you can go tmux, list session, the nice first tab auto complete. Just pretty much works all the time.
[00:12:29]
Hey, look at that, does that look familiar? Look dev, dev is at the bottom. We just got done navigating there. It has two windows. All right, that's pretty neat. It also has attached session, so you can just give it a session so I can go like this, tmux, what's it?
[00:12:44]
Attach session and then I go target dev and says, you can't do that 'cause again, you're already in a session. So attached session works when you're outside of tmux and you want to attach to a specific one. You don't want sessions in sessions. We are not exhibit. Yo dog, you don't like sessions.
[00:13:00]
We're not gonna put sessions in sessions. Just remember that your life will be hell if you start doing that, because then all sudden, it just becomes super hard to reason about what's happening. So instead, you can use something like switch-client, and that will switch over to the other one.
[00:13:16]
So now we're in this one. So now you can start seeing how we're gonna be building the script, you can start seeing kind of the little pieces come together. Tmux is a pretty cool thing. You can also do has session. I'm gonna show you something that's pretty annoying.
[00:13:27]
I'm gonna go like this. I'm gonna go tmux new-session, I'm gonna give it a name, let's call it foobar, okay? We're gonna call it foobar. I'm gonna go like this, tmux has-session -target name, foo. Now notice that there's no errors. There's no nothing. So if I go $?, what was the outcome of that previous one?
[00:13:53]
0, it successfully found a session that was called foo. Why? Target with hash session does like a prefix find into it. So it doesn't give you the exact result. This one took me a long time to learn. If you put an equal sign on the other hand, I don't even know how they do this.
[00:14:11]
It sounds magical to me. You put an equal sign, it does an exact match lookup. So just remember that. Just remember how that feels internally, so you never forget that again. That always use Nico when you're using half session, or else, it's just your life will just dramatically worse if you don't do that.
[00:14:29]
Okay, so now we're really feeling it. So we've kind of talked about session. There's also a Windows API. You can do the exact same thing when I go Ctrl+A or prefix c, it creates a new window. Well, so does new tmux new-window, and that also creates a new window.
[00:14:46]
You can even shorthand it for neww, right? Awesome, I can give neww a name, foobar. Now notice that number 6 is called foobar. Pretty neat, right? That means I can also do some cool things. There's also, list windows, select window. You can do that. You can also do things with the, where was it?
[00:15:07]
Where was the switch client? You can also go switch-client dev, I want switch-client dev, number 5. And so that went to window number five. If you've named your windows, tmux switch-client, I'll go dev:foobar. You can go named session to named window. And I did that incorrectly because I forgot the -t.
[00:15:30]
So now you can see that you can navigate around very specifically. I'm showing you all this because we're getting there, don't worry, we're getting there. I just wanna make sure you really get it, you internalize it. There's other fond APIs. This one's super cool and is very, very useful.
[00:15:44]
You can also do something like this. So let's switch back client to number five. This one goes number six. So instead I go like this, send-keys, I'm gonna target foobar, and I'm gonna go, echo hello, I just sent those keys over there. I go, hello. That's kind of neat.
[00:16:05]
But it didn't enter it. It only just sent the keys that I told it. So that means I can go back here and I can send it the old, what is it? I can send it the old Ctrl+M, I can send it or the carrot M, which is the control signal.
[00:16:19]
By the way, if you don't know how to do control signals, Ctrl+V puts you into control signal mode. Then you can do Ctrl+ V, open bracket as an escape. Let's see, I don't know why it's not working. Ctrl+V, open bracket, there you go. That's an escape signal. So you can send all those things.
[00:16:34]
So I can just send this over plus an enter signal so when it goes over here it, it actually executes it and sends it. So now you can even have panes, you can send commands to, to do stuff for you. So you could go probably a little too nuts here really easily and just build the world's ultimate automation system.
[00:16:52]
Me personally, I would just recommend navigating and typing a little bit. Maybe you don't need to spend a hundred hours on greatest thing just to run NPM run dev, maybe you don't need to go that hard. I know, I'm kidding. It's can go run. I get it. You guys, bunch of [INAUDIBLE] devs,
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops