Check out a free preview of the full Developer Productivity course
The "Creating the Cheat Sheet Script" Lesson is part of the full, Developer Productivity course featured in this preview video. Here's what you'd learn in this lesson:
ThePrimeagen live codes a script using Cheat Sheet, fzf, and less to query through and return information regarding selected languages and core utilities. When the script searches for the query, a new pane will be created to display the returned information.
Transcript from the "Creating the Cheat Sheet Script" Lesson
[00:00:00]
>> We are creating the greatest scripts known to man, called the cheat sheet. By the way, once again, awesomely straight line freehand, I can't even believe it. So let's build that right now, I can feel it, we don't need any anything extra at this point. We know how to do FCF, we know how to use cheat sheet.
[00:00:20]
We know how to write scripts, we know how to interact with Tmux. We should just be able to write this thing right away. All right, so let's just do this. Here we go, so we're in, we're ready. We're excited, it's write it. So once again, let's do that little shebang, a bang, right?
[00:00:34]
I think the shebang is very important. I'm just going to take it out of here and paste it up here right? Now let's start with this cheap thing. So, the first thing we need to do is we want a list of languages, and we want a list of coreutils, now, for now, let's just keep it pretty simple.
[00:00:48]
So it's like these languages and go in here and let's go like this golang ccpp TypeScript Russ because everybody loves rust. And there we go, let me tell you about our Lord and Savior Cargo. So there we go so there is our languages, so now we can have some core a utils, utils and let's go like this.
[00:01:09]
Let's go find Xargs sed, that's awkward, awk, there we go. We got a couple right there, it looks really nice. So now we're gonna take these, and I'm gonna replace the new lines or the empty spaces with new lines. Look at that, where we're just using tr because tr is funding.
[00:01:30]
There we go we got that thing I'm gonna read paste it in their go here we paste in their perfect. We have tr we have the new lines, everything looks good. Now here's something I always do forget which is when we do echo if we go languages. And then we go core Udall's what is going to happen?
[00:01:45]
Awesome, let's find out, let's use that really cool way to make my program, executable. You'll see right here. I just need to add the word silence because it keeps printing what I've already done we don't need to do that, all right? We made it executable, I'll open up a new window, and we'll go cheat sheet, look at this, okay.
[00:02:02]
Man, I've screwed everything up, haven't I? I forgot the most important part which is Echo, go here, press the dot. Awesome, go back here. There we go, you'll see right away that everything looks fantastic up until right here. We need some new lines. Now sometimes new lines, there we go.
[00:02:19]
Let's put it in a new line right here, why not? There we go, new line is awesome, right? Look at that nice little new line. It just makes me feel all warm and fuzzy on the inside, seeing that new line right in there. So let's fix that, let's make it into a proper new line.
[00:02:31]
So now someone did tell me just recently like two days ago, I can use dashi I don't know what dashi does. I should probably look into the man pages for that, but my assumption it means extended. There we go, it put in the new line for me, I don't need to use printf, printf forget parentheses, bad things happen.
[00:02:47]
There we go, so now we have our items, we can easily search for an easily fuzzy find over. Now, obviously I could make this a really big list, but for the point of this, I don't think I need to make this as big as possible. It just needs to be obvious how it works.
[00:03:01]
So there we go, we have these two things. And now we need to select the thing we want. So really we could do fcf right? Pipe that into fcf perfect, so let's just try it out. Here we go see boom, see? Beautiful, just always makes me feel so good when things just work.
[00:03:18]
How you want them to work. So now of course, selected, a little one of those. There we go, everything is great. Look at that, we're practically done with our script at this point. Now I also learned this recently, fumbling around, trying to make this script a couple of weeks ago.
[00:03:32]
On twitch when people repetitively told me that I am a script getting I don't know what I'm doing. Well, let me show you, me knowing what I'm doing. We're gonna echo out languages, and we're gonna pipe it into grep, and we're gonna do qs effectively. We're making it silent, we're not returning the results, and we just want to know did we find it or did we not find it?
[00:03:50]
Is selected within the languages, right? Cuz remember, if we look right here, when we use languages, it's like a different way we do it. It's a space limited item, when we use a core utils it's not a space to limited item. Again, my goodness, so straight. So here we go, so let's jump into here.
[00:04:13]
Go right there, let's just prove it right, let's go like this echo. I am language, take that f l go here and then go I am core youdil there we go, YOUDIL, that's how I say it, is that normal? Probably not, all right, let's cheat sheet, let's try it with sed, I AM CORE YOUDIL, perfect.
[00:04:34]
Let's try type script. I AM LANGUAGE. My goodness, this is going so good. Everything's working, by the way, this is how I developed bash for anyone that wants to know whenever I'm doing bash type things. I go back and forth like this a lot because bash is just, I never get it right.
[00:04:49]
It's like this is like such a hard language. I swear it feels so easy, but I get it wrong every single time we have this. So now what we need to do is we need to create that curl command, right? So let's try this, let's go like this curl cht, sh.
[00:05:04]
And then we got our selected, right? That's the thing we selected. And since we're under the languages, all we got to do now is go like this echo. We need a query don't, we need like a query So let's make this, read p, and that's p stands for prompt.
[00:05:18]
Give me your query, right, there we go. And we're gonna make it go into variable query. So we're gonna echo query down here. I'm gonna pipe it into tr, right? And tr we're gonna wanna replace every space with plus signs. Now for whatever reason I've decided right here, I wanna use single quotes, which just seems asinine to use single quotes and one single spot in your entire language.
[00:05:40]
There we go, so this should do exactly what we think it should do. It should curl a cheat sheet with our selected language and then have our, plus the limited query string down here. We're gonna wanna do it, just a touch different. Instead, we're gonna wanna to do that little till day and go query, right?
[00:05:59]
Query, awesome, so that should do the other part of it. So if we do find exec, that should work. So let's find out, let's go like this, let's quit. Let's go in here, wrong script, and let's go, I just quit them. Let's open up them because it just feels better, right?
[00:06:14]
There we go, let's go over here. And let's go to cheat sheet, and let's use did I add find, I did add find. So there we go, we got fine, give me your query. Let's do exec, all right. All right, there's everything that has to do with exec right here.
[00:06:26]
Awesome, so like this is really working right?. I feel like we're really doing something awesome. Okay, this is looking good, but the problem is, is we're not t mix out of our mind yet. Right now we're still just kind of lightly t maxing and I don't want that I want to just, I just want this to be the greatest thing ever.
[00:06:43]
So how should we go about this? So let's think about that, we have two options. We could create a new pain when which I've never done and someone suggested, and honestly, it kinda feels like a great challenge. So let's try a new pain for a second. If not, I'll just fall back to the way I already prepared.
[00:06:59]
But we're going off script now okay, people are going all in on this one. I know this has to be the one, it has to be split. Create a new pain by splitting the target pain, okay? Does a horizontal split v does a vertical split, awesome. Okay, so I'm a vertical kinda guy, by the way, no and most normal programs horizontal.
[00:07:22]
So if two problems, i3 does it backwards. Vim does it the correct way, I would consider the verticality versus horizontal is the direction of the split, not the resulting boxes. So an i3, this would be considered a horizontal split because it's splitting the horizontal space. But it results in a vertical bar, I always consider that backwards right, that's totally backwards.
[00:07:44]
If you want to split vertically, you literally do vertical, notice that my bottom line got highlighted down here. And this is a vertical split now that's a horizontal split. I don't know who you're kidding. All right, so I assume tmux is correct and vertical pain literally means the lines vertical, not splitting the window, vertically.
[00:08:06]
There we go, so let's try this out. So we're go this Tmux split window, and my guess is it's dash V for vertical. This is feeling good, we won't provide a target because I'm gonna assume that if there's no target it does my current window. Split window, you never know.
[00:08:21]
Yeah, never mind, split windows right here. Shell command, okay. All right, and we even have starting directory, we can do environment we can provide environment variables really cool by the way that you can provide environment variables. That means you could actually have certain things in this. You could like start a dB, environment which already has all your connections and everything.
[00:08:39]
A lot of cool things you can do here, so I think this should probably be okay. Looks like this split window vertically and then let's just do this. Now, if I'm not mistaken, this should just work first try because it is the only way in which I work.
[00:08:54]
So let's go like this, let's go I just quit them again. Let's not do that, let's quit this. We don't need that anymore. Let's do cheat sheet we're gonna run cheat sheet and now remember I did it only for the languages so let's do this one and let's do enum.
[00:09:06]
All right, here we go. All right, so what just happened there? Does anyone know what happened? I know what happened but do you know what happened?
>> The command finished and it closed the window.
>> Nice, good job. That's correct, so here's a little fun thing about TMX when you do new window.
[00:09:24]
Yes, this was pre-planned. If I like this, echo hello, S and sleep, one and then go like this bash C. So that way it doesn't get all screwed up, right. Poop, and sleep one. It's gonna create a new window, hello. And after it gets done sleeping for one second.
[00:09:44]
When that command finishes, and it's done, well the window's done right? It's no longer needed, so therefore it's going to close. So when I did this little fun thing right here, we did a split window and we curl cheat sheets, so it came back with the answer, but it was done at that point, right?
[00:10:04]
So let's go like this, let's go bash-c. And let's just pass this into less. Less a super-duper ultra cool program. Okay, I'm gonna be let's see, hon, we got a lot of escaping to do here. Let's escape with passion and fury, I don't think I need to escape those ones.
[00:10:21]
Let's just see, I don't think I need to escape anything. Let's just find out, all right. So let's do this again. I keep closing my VIM command there for a second. So let's do cheat sheet, let's do TypeScript. Let's do enum and there we go, yes. First off Teamwork is backwards, which part of me just feels very upset, but look, our window stayed alive.
[00:10:40]
How fantastic is this awesome, right? When I close, it's dead, right? When I close, the ls, it dies. For those that don't know what is, check this out. Let's go back to here, let's go back to this program. And let's just take this and go down and just paste this a bunch of times, right?
[00:10:59]
There we go, it's a pretty long program now. So if I were to cat pipe, it'd be like a really long program, right? Like, look at that, it's the same thing a bunch of times. But if I were to go a cat, this thing, pipe it into less, it shows me what it has, and then I can scroll down.
[00:11:14]
All right, so hopefully that explanation of, did you even catch the explanation of less right there? Hopefully you guys did, if you guys didn't DLC. Get out of here, [LAUGH] doing DLC, that's what we're doing the expansion packs all right, so let me do one more time just in case you didn't hear it.
[00:11:33]
When I pipe that into less it will only show me a screen full and I can use j to go down K to go up very vim bindings right D to jump down half page at a time. There we go, hit q to quit. So kinda feels more like, it has kind of like an ad ish feel to it, a vim ad feel to it.
[00:11:52]
So it's pretty easy to use, there's a few things you need to know about less and that's pretty much all you need to know. And so let's go back into vim now. And let's go back to your little cheat sheet. So there we go, so we just piped into less, so it would work.
[00:12:04]
And then, of course, let's fix that very poor vertical thing to make it horizontal. And let's run it again, I close them again, out clearly out of my element here. Enough, and there we go. Now I have this and so now I can scroll around here to see what's going on.
[00:12:20]
So obviously there's nothing here. So the moment I press Q, it quits, it also quits the split we're back here. That's actually pretty cool, that's a pretty good experience right there. Obviously I usually program not this zoomed in. But since I've been streaming so much, I'm actually so used to like having this much zoomed in this.
[00:12:37]
It's hard for me to program back now to like this. I don't even know it's happening. So for me, the splits might not work as well because now I have weird stream brain on, but that's actually a pretty cool way to use it. And not only can you do that with splits.
[00:12:49]
Let's just say you don't need it to be that split, right? I bet if we go back to tmux, a man tmux, right? And go to split window, I guarantee you, we are gonna find, my goodness. There it is, I bet you, we're gonna find some sort of percentage, P percent.
[00:13:06]
Okay, so there you go, that's what it is. So we could also do something like P 35 or 32, I guess we're doing 32. Here, 42, right? You can set some amount of the window space to be able to take up. So that means you could still have your primary viewport and then just quickly split it off on the side in a smaller amount, cuz maybe you don't need a lot of it.
[00:13:26]
And then you can do that side by side workflow that makes sense for just a little bit. And there we go, I, to me, this seems like a pretty, pretty nice little workflow. And it's kind of fun to do on the fly too. And so, of course, we can do the same thing right here, right?
[00:13:39]
I can just delete that Vi, these little curly braces, and let's go right here. Let's paste that, all right, this is awesome, and we don't really need anything up until here. There we go, I think that's about right. That's about right there, there we go. We have the exact same thing, and now we can do it for a language.
[00:13:59]
So let's just try that again, let's cheat sheet. Let's go to find, then it's gonna say give me a query, let's use exact and so there we go. We have that 32% file, which is pretty annoying for me. But you can imagine if it was like a little bit it'd be pretty easy to see right here, right?
[00:14:15]
All right, rock on, that is awesome. And there we go, we quit, it's gone. It's awesome, you can transfer between panes pretty easily. What I have set up inside my vim or my team ox RC. My goodness. Here, let's go right here, okay, that's really obviously easy to see.
[00:14:31]
I have this right here is that I actually have a bind Kj h now, so it feels like them. And then I can go up down left and right for my pain so when I have multiple of them I just do prefix h to jump over prefix l the jump over, so I added that in there.
[00:14:46]
Some t mux like pain switching, it feels pretty good. All right, exit, awesome. Zooming back in or feeling good
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops