
Lesson Description
The "Search and Navigate Script" Lesson is part of the full, My Dev Setup Is Better Than Yours course featured in this preview video. Here's what you'd learn in this lesson:
ThePrimeagen demonstrates how to write a script called "Tmux Sessionizer" that allows the user to navigate and switch between different directories using Tmux sessions. The script can create new sessions, switch to existing sessions, and execute other scripts within the sessions.
Transcript from the "Search and Navigate Script" Lesson
[00:00:00]
>> The Primeagen: Let's keep on going right here. So we have that and so that means I have already showed you this little control r, we did this little playing thing. So now let's solve these last problems. So what I wanna do is I want to write a script that's going to allow me to search specific directories, I want to navigate based on the FCF results.
[00:00:21]
I want to do all that. So what we're going to do is we're gonna start with this script, this this MF script, this mother FCF script, and we are going to get all the directories out of some directory that I pointed to, and then we are going to clean the name of that directory so can match tmux requirements.
[00:00:41]
tmux has some special characters, like you're not supposed to use colons in your session name cuz if you did that when you tried to do a target, it would screw everything up. There's all these rules and stuff you shouldn't do, so we need to make sure we kinda clean out the names and all that, so let's do that.
[00:00:53]
So we're gonna get a selected directory, then we're gonna clean it and create a nice tmux name. So we'll start there. So what I'm gonna do is I'm gonna jump in here, whoopsie daisies, I'm just gonna close out Vim. Screw Vim, I never liked it anyways. All right, so now that I like this place, I'm gonna create another thing called tmux-sessionizer, all right?
[00:01:10]
And in here I'll do another user vin fbash, okay, we're looking good. Yet another script, how many times have we typed that? This has to be the most amount of batch scripts if you're following along you've ever created in one day, don't lie to me. Don't lie, you know it's true.
[00:01:27]
All right, so what we want to do is we first want to find the selected directory. So I'm gonna go like this, selected equals, and we're gonna go like this, I'm gonna use find, my favorite command. And how about this one, let's only grab things out of personal.
[00:01:37]
Does that sound good? So only out of my personal stuff. I always do personal and work, even though I don't have work. I'm unemployed, I'm fun employed, and so I just still have always maintained this personal and work is kind of how I did things. Okay, so I have this personal director.
[00:01:52]
I'm gonna grab every single folder out of there, so I'm gonna go mindepth 1 and maxdepth 1. By the way, I just don't work at Netflix anymore, I just don't, it's just too bad. All right, so I've mindepth 1, maxdepth 1 type directory. And what I want to do is I'm gonna pipe that result to FCF.
[00:02:09]
Remember, find gives me every single thing that's inside that directory, then FCF is gonna allow me to fuzzy find over the top of it. So it's pretty fantastic right here. So once we get that, if there is no selected, meaning that if you Ctrl C, it cancels SCF, which means it outputs nothing.
[00:02:23]
So if there is no selected, then we probably just need to exit, right? So I'll just do like a little exit 0, fantastic. So now I want selected name, which is gonna have to be something we need. We need to clean the string that was handed back to us.
[00:02:37]
Well, what we're gonna do here is I'm gonna like this. Base name, so I just want the name of the folder of selected, right, that makes sense, spelled incorrectly and didn't have a dollar sign, base name of folder, and then I wanna pass it through something called TR.
[00:02:50]
If you're not familiar with TR, what does it stand for? I don't know, I don't know what it stands for. But what it allows you to do is you can specify a list of characters and then specify the replacements, we'll call it to character replacement. So I'll go like this, I'll say, hey, you can't have colons.
[00:03:05]
You can't have commas. You can't have periods. You can't have spaces. And instead, I wanna replace that with underscore, underscore, underscore, and underscore. All of them are gonna be underscores. Now, just by simply doing that, we've got selected, we've created its name, and we've cleaned it so it can be a nice valid beautiful tmux name.
[00:03:21]
Okay, so I'm gonna like this just to make sure I have everything correct. I'm gonna go like this, selected and selected name, fantastic, go right here, and I'm gonna run local scripts sessionizer, there we go, so it's gonna do this. Let's just do caleb, and there we go so it shows that I grab this right here, and then this is the name, caleb, fantastic.
[00:03:44]
Okay, so you can see what we're building here. We're building a way for us to be able to say where we want to go, cleaning it, and now all we have to do is just create all the tmux stuff, right? That's actually the easy part, it really is the easy part if you think about it, okay, example code, how close did I get?
[00:03:59]
Whew, almost identical, all right, so now let's make the script good. We need to do if there is a session but if there is I must have not even finished this thought, my brain was moving so fast I was just like if there is a session, but my fingers only got to if there is I moved on.
[00:04:16]
All right, navigate to that session if there is not a session editors notes then we need to create a session and then navigate to it. Okay, everybody ready to do that? Pretty straightforward, okay, cuz what we're recreating is this. This is the thing that I use all the time, absolutely fantastic.
[00:04:32]
Again, reduces a lot of that cognitive load, all right, so we gonna go like this, we're gonna go back to here and let's erase this. We don't need to echo anything out, you don't wanna have your scripts echoing things out. By the way, fun editorial note, if you're gonna echo things out of your script, do it onto the error channel.
[00:04:47]
Reason being is you can always pipe air somewhere else, right?. But if you wanna hook programs up to each other and pipe input in and out, you only want the important stuff going out of standard out. You should do this with Node you should do this with everything.
[00:05:01]
Every time you ever build a CLI tool, always use standard error for any of the stuff. And by the way, if you create a help menu and you put that help menu in standard error, so help me, Linus Torvalds, I will come after you, okay? Do not put that help menu in standard error, that's ridiculous.
[00:05:19]
Anyways, it's the only reason why is cuz then you can't pipe it into Grep and search for an option then you have to do like pipe ampersand into Grep, it's just ridiculous. All right, sorry, that was an odd rant, and I did not prepare. I was not ready to have that rant, but it came out of me, and it's befitting of the course.
[00:05:33]
Okay, so we have our selected name, we're gonna do this. So what I'm gonna do is I'm gonna like this. Let's go like this, tmux has session. Now remember, we wanna use that equal sign. Remember, if tmux has selected name as the session, well then we can just navigate to it, right?
[00:05:51]
So I can go tmux and now we have to make some decisions here. What happens if tmux has that session, but we're not in tmux, right? So there's some things that can happen there. So we've gotta be a little bit careful here. So I'm gonna go like this, I'm gonna create a function, I'm gonna call it switch to, all right?
[00:06:10]
And it's just gonna switch to selected name for me. So I'm gonna go like this, if tmux, let's see, what is it? If tmux exists or if tmux doesn't exist, and I'm sure we could make this into the positive. But either way, this is my bash, I know, so I'm gonna do the bash I know.
[00:06:28]
If tmux does not exist then we want to go tmux attach client. Cuz remember, we now want to attach to that session or is it attach session? Which one is it? Tmux attach session, whoo, it's switch client attach session. It is what it is, okay, there we go, so we're gonna attach session to the targeted, selected name.
[00:06:48]
All right, fantastic, else I want to tmux switch client. So if we're in a tmux session, we want to switch to the new session. We don't want to attach the new session, cuz remember, can you attach to a session in a session? No, we don't like sessions in our sessions, all right, do the exact same thing, selected name, there we go, fantastic.
[00:07:08]
So if we have this, we're just gonna switch to, that's all there is to it. Bada bing, bada boom, everything looks nice. So that means if we have a session that matches the directory that we were looking for, we now can just go right there, and we can have it, everything's fine, we don't do anything.
[00:07:23]
Now this is where things get a little bit more tricky. We now need to go and we need to create a new session because we clearly don't have that session. So we need to create a new session, we need to switch to it. All right, deal, deal, done, let's do it.
[00:07:35]
So whoopsie daisies, what did I just do? I somehow branched my history, there we go, all right. All right, so let's do that. So let's write this, tmux, I feel like I'm missing something that I'm just forgetting but we'll see what happens. Selected, there we go, so what I did is I just created a new session.
[00:07:59]
So tmux isn't running, I just simply created a new session somewhere, or if it's not running, tmux now starts running. If it is currently running, it doesn't matter, cuz we just create a new session. It's somewhere else, and now we can actually attach to that session, so I can just go like that.
[00:08:13]
That alone should be enough for us to be able to do all the right moves here. Deal, deal, yes, we've done it, we've done exactly what we wanted to do. Let's give this a little spin, let's give this a little spin. Let's go to a local, I'm gonna open this up.
[00:08:31]
Let's try to see if we can navigate to caleb. There we go, we navigated to caleb, it just worked. It went, created a new session, if I go control w you can see right here, created that. A little fun thing about tmux, or prefix capital L will take you back to your previous session.
[00:08:45]
This is like alternate filed in Vim, it's like the greatest thing in the universe. So now we're back to where we're at. Prefix capital L, prefix capital L. So we can just switch back and forth between them. So often I'm working in two projects, it's really nice to have that back and forth, it makes it really easy to go back and forth.
[00:09:01]
All right, awesome, so we did a good job. Also notice this right here, can't find sessioncCaleb. Whoopsie daisies, so let's go over here and let's go pipe error output to dev null. We don't want our scripts to be loud, we want it to be quiet, it should be nice.
[00:09:21]
So just to test it one last time, let's go to harpoon. There we go, harpoon did that, we go back, it didn't make any noise, okay? We are really just, lots of success is happening right now. So let's go back to here, and let's see what did I do?
[00:09:36]
How did I do this? I did an if statement. Look at that, that's kind of neat. Okay, pretty neat. All right, so now remember previously, y's in there, scripted tmux, wow, that's not even the right word, I wonder how I did that. It should be ready tmux, but somehow, it's not ready tmux.
[00:09:50]
It just became scripted and then with a y at the end. So remember our previous ready tmux, what that says right there. It'd be nice if we could combine our two scripts, right, correct? You're absolutely right, it's not very hard. I think the script that I have right there also has an error.
[00:10:05]
So this one right here, we just switched to it, it already exists, we don't want to execute that ready tmux. But if it does and we have switched to it, then we want to now execute that and hydrate it, so how do we do that? Well, if we just go like this, let's just do it for fun.
[00:10:20]
We're gonna go ready tmux right here, okay? If I do that, we've switched to our new client, and now we execute ready tmux, what exactly is gonna happen? Well here, let's go like this, let's go to get test. We open that up, look at what executed over here.
[00:10:34]
It should have executed over here cuz it won't execute over here. It should have executed right in here, here let's go like this. Do we have, I should have this right here, right? So that should have opened up a new thing. In other words, it should have executed.
[00:10:50]
Let's just make sure that I'm not crazy, yeah, I'll go like this. Ready tmux, and then in here I'll do, bin user, user bin and bash. Hold on, hold on, we might have ourselves, we were like this, tmux, new window, name, let's call it. This shouldn't happen, great name, right?
[00:11:17]
It's executable, we're looking good, if I go right here, and I go ready tmux, it does that. This shouldn't happen, so okay, we're on the same page, we agree with each other. I call this thing ready tmux, which I don't think it's called ready tmux. I think it's called ready with a dash, that would be why it happened, okay, dashes, turns out, are important.
[00:11:39]
Underscores, less important in this specific situation, well we'll re execute that. Let's go to falcore, you'll notice that did we execute it right here? I don't know, did we execute it right here? We did, okay, we executed it in our dev and we didn't execute it in falcore, why?
[00:11:56]
When we switch a client that doesn't switch the thing that's processing and executing, that process is still running in that session inside that one space, we need to send ready tmux over to the new session, right? So that's where that one thing I showed you earlier, none of this was for fun it was all serious.
[00:12:15]
So we're gonna go like this, we're gonna go target as selected name, and then we're gonna pass over ready tmux, and then we'll pass over enter. So now that's gonna send over to that new session, hey, ready tmux. So now when we navigate it will also ready our environment, pretty exciting, right?
[00:12:35]
So let's go back to this one, right? This is, no, that's not the one, which one is it? It was get test, right? Get test, let me go into here, this shouldn't happen. So what I'm gonna do is I'm gonna go back to this one and I'm gonna destroy get test.
[00:12:48]
Where's get test? There it is, I'm gonna just x that one, yes, there we go. Get test does not work anymore. So now if we go right here and save that, and go right here and we go our new little local execution and we go get test, you'll notice that it did that and it also executed the scripts, but this time on the get test side of things because we sent the keys over.
[00:13:10]
So this is like that cool part, I know you were very excited about the sending keys. You can do a lot with the sending keys, right? You can make a lot of stuff happen. And so I just love this, I think this is super cool. And so now we can ready this environment the exact way we want.
[00:13:23]
So if you have three things you need to run, you could pane them all out. And then pane one you do this, and pane two you do that, and pane three you do this. And everything just gets up and running. And it's not nearly the same kind of dance you have to do every single time.
[00:13:35]
Makes it really easy, you could create tmux, what's it called? You could create tmux commands that do an opposite unready tmux and it will check for an unready and kill all your servers and then re ready them and start them all back up again if you need to refresh them, like how many times a year do you need to delete node modules?
[00:13:54]
Many, many, many, many times a year. And so, it'd be nice if you had delete everything, restart everything. So you could kind of script to your whole environment of doing these kinds of things and make it really, really simple to where you're just going no prefix capital R, just restart.
[00:14:07]
I just want to restart and my restart scripts in this directory. So each directory could have its own restart script. It just kind of makes that really nice process. And so that's kind of the idea or the goal I was hoping to do. And so now if we execute dev end, tmux sessionizer will be on our path, and we can use this new tmux sessionizer.
[00:14:22]
I already have a pretty sweet teamwork sessioniser so I don't need it, but you get the idea. All right, so how do we execute that? We just get it on the path like we said, and then from there you can execute it. I actually have mine set up a couple different ways.
[00:14:32]
So my little fuzzy finding mechanism if I'm in Vim and I look for, yeah, there we go. Whoopsies, there we go, what this does is, when I'm in Vim, if I press Ctrl F, it will actually go out to tmux, create a new window, and then just simply do tmux sessionizer as the action on that window.
[00:14:56]
So Ctrl F, you'll notice it created a new window right here. I can do my fuzzy finding right here. When this process completes on tmux, it automatically closes the window for me cuz it no longer lives. So when I go back, notice I only have one window now, I don't have two windows.
[00:15:12]
So it opens it up for me just for searching and then closes it back down. In tmux, if I'm in the middle of something and I'm not in Vim, I can go prefix Ctrl F or prefix F, or whatever it was. Did I actually take it away? Never mind, I took that away, no, I didn't take it away.
[00:15:28]
I did take it away, never mind, forget that, I had it at one point. And then I also have it in what's called in Zish. If I press Ctrl F, it also brings it up. So I kind of have it kind of pasted throughout. I'm surprised it took away the tmux one.
[00:15:39]
I thought I had that one in there for a while. Wait a second, wait a second, that's because we have the class example one. Let's just, let's see, do I have it in here? Yeah, see I had it in here, okay, so I did actually have it in here.
[00:16:00]
I'm just being a goofball, yeah, it's actually in there. I swear, I swear it's totally in there, okay, so it was in there. I just have the current example one that we were using. So there we go, so that's the way you could use that. So now if you theoretically used something like that, you can now navigate to any project, have it hydrated the way you want, and it only takes a couple keystrokes, and it's really easy to navigate between them.
[00:16:21]
The sessions last for a long time, if you close down your terminal, none of your stuff breaks. For me, this is ten times more convenient than using tabs, have ever had a terminal that has five, ten tabs open each one's in these different directories. They're all running different processes.
[00:16:38]
So then you're like switching between them. And it's always like tick or some bizarre thing to switch in the terminal, it's just super inconvenient. I just can't live that life, honestly, I just get too much anxiety. It's the same thing as like that with Mac, blowing up all the windows and then searching.
[00:16:53]
I never wanna live the searching lifestyle. I never want to have to try to look for the thing I want. I wanna just type it in and make it happen. I have a strong belief that searching fatigue is a real thing. And so don't get search fatigue.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops