Developer Productivity, v2

Creating Our First Script

ThePrimeagen
terminal
Developer Productivity, v2

Check out a free preview of the full Developer Productivity, v2 course

The "Creating Our First Script" 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 demonstrates how to write a script that will execute a specific file when navigating to a directory. He also introduces a fuzzy finder tool and shows how it can be used for quick navigation and selection of files and directories.

Preview
Close

Transcript from the "Creating Our First Script" Lesson

[00:00:00]
>> ThePrimeagen: So let's start with our first kind of script here. We still have four points left to address for my perfect terminal navigation system, which is gonna be multiple sessions running based on directory. Navigate to sessions with a name instantly. Fuzzy find for those directories and run a script when I create new sessions.

[00:00:16]
Those are what I consider the ultimate experience of directories. So we're gonna start with 0.6 cuz I think 0.6 is the easiest, run a script or whatever programs I want when I navigate to a directory. Whenever I create a new session, I have now created a new session that's gonna point to say, I don't know, dev.

[00:00:34]
When it runs here, I want it to do two things, one, I want it to check for some special dot file, what we can call this ready or ready-tmux. I spelled that way wrong, just go with it, all right? Spelling is not a sign of intelligence, trust me, I'm right.

[00:00:49]
Or it goes and checks in the home directory for the exact same thing. I'm gonna keep misspelling it, that way if I land into a folder, it checks for a specific startup script, or it attempts to run, say, a generalized startup script. So that way I have this nice kind of motion, and this is shockingly easy.

[00:01:09]
So we're just gonna write the script really, really easily. I'm gonna go to dev-en, and we're gonna be right here, and I'm gonna write a nice little script right here. And first off, let's just create, what do we wanna call this? I wanna call this thing, I'm gonna create a directory called local, and inside of local, I'm gonna create something called bin.

[00:01:27]
And inside of bin, I'm gonna create something called ready-tmux. And so notice that I did local bin ready, this is a place where a lot of times people put all their kind of local executables. It probably would be better to create something called local, I just thought of this this morning.

[00:01:45]
I should just probably have local scripts, and those are my scripts as opposed to bin being executable that may be installed there. So that way I don't wipe out a directory with executables cuz you don't wanna store executables in your get, you wanna store the scripts. Probably better ways to do this, but here we are, okay, people.

[00:02:02]
We're right here, and we're just gonna live with all the consequences of our life. We are building out a script that's going to look for a file that's in the directory that we're currently in, and attempt to execute it. Or it's gonna look for a global script that's in the home directory and execute that.

[00:02:18]
That way with tmux, as we navigate to a new directory and create a new session, we can then also execute this. So that way, if you like to have a couple panes, if you like to have things organized in such a way, it will just automatically do that for you.

[00:02:32]
All right, sounds good, it's a relatively simple script to write. The reason why I write all these things and kinda we're going over all this like painful, excruciating detail. I think a lot of people think these things are complicated, they're really just not all that complicated. They're actually rather simple activities, it's just taking advantage of path and a bit of bash.

[00:02:51]
So we're gonna go like this. So if you do a -x, that will actually say, is this thing executable? -x, you get it? Yeah, looks pretty good. So what I'll do is, if inside of this directory there's something called ready-tmux and it's executable, well, then we will execute ready-tmux.

[00:03:09]
Yeah, else we can actually do, what is it, else if, is that how you do that? Is that how you do that in crazy language? Elif, there is an executable in my home that's called ready-tmux. We will also execute that, or we will execute that. We won't also, we will execute that, and then I'll just do a quick clear.

[00:03:33]
So anything afterwards that has happened, any extra printouts, anything like that, boom, get rid of everything. You have a nice little screen, everything looks pretty good. Now, I have this in my local bin, I'm actually gonna rename this one to scripts, I'm gonna start this whole scripts thing.

[00:03:50]
And I've never done this we're doing this live. So I'm just gonna do a quick little look in my adult local, cuz I don't wanna destroy everything. Do I have something called scripts? No, I don't, so this should theoretically work out just fine. So I'm gonna go to my dev-env, I'm gonna take this, I'm gonna take local, and I'm just gonna simply go, home/ .local.

[00:04:12]
There we go, so I'm gonna copy the directory from local to my local here. So that should just create a script directory. Now, just to make sure that I'm not completely terrible at programming, let's throw a quick draw in there. What do we got in here? Do you see the local?

[00:04:26]
All right, here we go, so I'm going to delete the local folder right here, and then I'm going to run, or then I'm gonna copy over this right here. There's some extra periods, but that's not a big deal. All right, do it all, make it happen, fantastic, so now we have that.

[00:04:41]
This is not something I was planning on keeping so for now I'm just gonna do a little hand edit right now. And I'm just gonna add to path really quickly. So don't worry about that local Scripts there we go, right, I'm gonna get rid of that. Cuz I don't know what to do yet, I still gotta think about how I want to organize that.

[00:05:00]
But for now, that means I should be able to go echo $PATH grep -o -local/scripts, right? There it is, okay, so we do have it in there, yay, which means also, I should be able to go in here. And whoopsie daisies, let's go right here and go, let's go ls into here.

[00:05:20]
There you go, ready-tmux, which means that.ready-tmux, let's just make sure I might actually have multiple of these. So let me just make sure I don't have multiple of those. So we have the same experience which ready-tmux, that's gonna be in scripts, fantastic, okay. So there we go, we can see that we have my ready-tmux right here.

[00:05:37]
So if I execute that, if I go ready-tmux it just simply cleared, didn't do anything nothing actually happened. So that means if were to vim and go actually hold on, let's go do I have something just to make sure I don't, okay good. I might forget to delete a few things.

[00:05:55]
So that means if i jump in here and I go ready-tmux theoretically I could have something in here that's gonna be executable, user, bin and bash. Do that, I'll go tmux, new window. So I'm just gonna have it create a new window, and I'm gonna name ii foo, foo, all right?

[00:06:12]
Yeah, right, sounds fantastic, so now, if I go ready-tmux, did that not work? Are you not entertained? Did I mess that up? I didn't make it executable, bam, executable. So now I should be able to do that, we created a new thing, now notice, it runs. This is exactly what we wanted to see, right?

[00:06:35]
It should always run this way, when I do it again, it should just hopefully, boom, we got it again, okay, Fantastic. So now we've been kind of built that like that just tiny little bit we needed, which is not a lot like that's a very simple script to write.

[00:06:49]
And now it just sits somewhere where it's executable, and now we just simply got to take advantage of it now. So let's go back here, we did all that very easy. Look at that, it's pretty much identical code. It's like I planned this before, all right, that was pretty cool, right?

[00:07:01]
We could even take it and you could technically like bind it to a tmux, key so you could do, I don't know, Ctrl A capital S, and it'd be, whoosh, ready up your environment. Or you could attach to your window manager, or you could do some sort of mod and capital S or whatever you wanna do, or you could put it in Zish and do something with that.

[00:07:19]
But think none of those are very useful, cuz I don't ever want to manually execute that script. I just want that script to exist and execute when I open up a new tmux session. So let's do that, so there we go. So now we only have three things left, we have multiple sessions running based on directory.

[00:07:35]
We should be able to navigate to any of these directories, and we should be able to Fuzzy find any of those directories. So we're gonna do a quick detour, I'm gonna show you one of my greatest things of all time or not my. One of my favorite things of all time, which is FCF.

[00:07:48]
FCF is absolutely fantastic. Here's how you would install it, we could, theoretically, in dev end, you could add this under, say, a libs thing. You could add in get clone, this FCF. You could then do this, and then on your zsh profile, you could also have this right here, which says, hey, if this thing is a file, then source that file.

[00:08:05]
And that little zsh thing has some extra cool stuff associated with it. So I can do this, when I press Ctrl R, instead of going and doing a recursive search up your history where you start typing and it shows you one result, instead you get this beautiful fuzzy finder.

[00:08:18]
So if I go Vim, you can see all the different ways I've vimed, right? If I go git clone, you can see all the different dumb things I put. This right here, crazy programming language in which you have to try to specify how to teach the AI is what you want.

[00:08:32]
It's just crazy, right? And so you can do anything you want right there. So now it's like a really nice fuzzy find that also allows you to select things out. But here's where it gets really good, if I go like this, echo 1\n2\n3\n, and I take that and I pipe it into FCF.

[00:08:47]
Notice I can choose which one I want. So anything go into FCF, and then you can fuzzy, find over the top of it. You can go like this, I want three, notice what it did, the standard in gave it its input, the standard out gave out what I select.

[00:09:00]
So I just selected 3, and that's all it gave me. So you can see the power that we're about to have here, the power is just incredible, right?

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