Check out a free preview of the full Developer Productivity course:
The "fzf" 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 using the fuzzy finder fzf to find and write out a selected line. A walk through of creating a base fuzzy finder using the core utility find along with mindepth and maxdepth is also provided in this segment.

Get Unlimited Access Now

Transcript from the "fzf" Lesson

[00:00:00]
>> Let's jump over to fzf. And so I did wanna talk about this. We are building this tool for those that don't remember what the tool is. It's been too long of a break. Let me just say it really quickly. We're building a tool for better navigation, because, right now, we're still having the same problem with team ups where it's just like it's really hard to really get around It doesn't feel like it really solves a problem.

[00:00:18] It's like it looks at the problem from a new direction. But we're gonna actually script and make something else. So before we do that, I did wanna show you one thing. I would be doing you a disservice really by not showing you this is written by a guy named June Gun.

[00:00:30] First off, that's like the coolest handle on GitHub. And also he writes amazing. Plugins so June gun awesome, but let me show you team ox you can install with apt brew, get clone it slash install. It offers a lot of cool things. So let's check this out. So let's go like this.

[00:00:48] Remember that previous echo. Where we had these numbers instead of piping it into this little pipe program we have right here, let's pipe it into fzf. Now what fcf does is it allows you to have a fuzzy like it's just as a quick fuzzy finder so if I type in the number five Notice that it highlights this five right here, and that five right there.

[00:01:07] So it's saying, I found two results. So if I type in seven, it's gonna go, this is the one that closest matches it. When I press enter, it's gonna take the line that you have selected and simply write it out. To standard out. So it's just a really nice little, kind of really Unix philosophy style program.

[00:01:27] It just does one thing and it does that one thing really, really, really well, right? It takes a bunch of input and reduces it down. You can actually select multiple inputs if you want to. But for us, we're just really thinking about the singular input style. So I can take a bunch of lines and turn it into one line with just a few characters.

[00:01:45] Awesome. FCF is awesome. And if you install FCF, you can use, remember, I don't know if you've ever done this before, but you've pressed control R and you get like previous it's called like recursive searching and you get to search up your history. Well, with FCF, if you do control, are you actually get your history and then you can start reducing it by runtime, you can start looking at all the different things you're doing.

[00:02:09] And it just gives you a faster way to look through your history. It's much, much nicer, like No, I have that one LS of command. Okay, so that's how you use LSF. I remember I used it once before, I couldn't remember it. Awesome, right? It just has those things built in, which is really, really, I mean, for me, it's just super duper nice.

[00:02:27] It's much better than that other search because you don't really get to see a window of what you're looking for. So I love fcf I use it all the time. It's fantastic and it has some other things, right? You can grab files in here so I only have one file in here so if I back up.

[00:02:40] I have a few files in here so if I press T, it can go through here and grab a bunch of stuff and say okay, here's everything that you can possibly look through. So I can start opening up one of those folders if I press enter. It just simply gives me that folder.

[00:02:53] And then I can press enter, and it would navigate there, right? Pretty nice little, Nice little item right there. All right, let's see. Let's go back to Tampa. All right, we're back in Tampa. Everything's great. So now that you kind of know what FCF is, it's just a fuzzy finder takes standard and, and searches over it and returns and prints out to standard out what you've selected.

[00:03:13] So that's Super useful, so I kind of feel like we've effectively in most sense, solved this thing right here. We know where we want to look, we know how to use a fuzzy finder, we know how to target. All we got to do now is just right the bits and pieces together to create this script.

[00:03:33] All right, so let me, let's just do it right now. Let's jump in here I use vim, vim fantastic fees VS code, you can do a little code dot right here. Wait for that sweet 40 seconds which I'm buying you time right now you're up and running, you're feeling good about yourself.

[00:03:47] Let's go Let's create a new file called tmux session. Iser, you can call it whatever you want. Now remember, since we're creating a little file that we want to be runnable. We're gonna need to add in a little shebang. If you're not familiar with what shibang effectively do, they just tell, they effectively identify what type of program they're going to be.

[00:04:05] Now I've been getting better at saying what type of program it is. There we go. I'm gonna say it's a bash program. We're writing a bash program. You just got to put this as your first line, I could technically put something like node here, and it would actually run this program via node, right?

[00:04:19] You can do that you'll see that on top of a lot of your executables downloaded via, like if you looked at the Webpack node modules dot bin, there you go. That's what it's called node modules dot bin. You look at that, you'll actually see a shebang at the top.

[00:04:31] You'll go okay this thing is identifying itself as node so that way it knows how to run itself. Same thing is going on right here. It's identifying itself by what program it needs to or needs to be used to run. So there we go. We put that in, things are looking great.

[00:04:43] So now we need to be able to find all those folders. Well, we're already kind of at a stuck point. So here let me do prefix C, create a new window. And let's talk about fine so I'm gonna go all the way down to here. So I'm in the I'm just in my home directory.

[00:04:56] If I type find and I do dot, well it returns to me like everything. My goodness. I even saw Emacs. Don't tell anybody that there was an Emacs anything on the screen there for a second. I don't want it I don't want anyone getting in trouble here, but that just returns everything, right, way too many things.

[00:05:11] Well find has a lot, a lot, of features. It's one of the most feature-rich little core utility you can have. You can do something like this There's type D. What this will do is it will find everything starting at this folder and only return directories, okay? Faster. But still way too many, right?

[00:05:28] We don't want that many, right? We don't need to be in every single folder on my operating system, we only really need just a select few. I want the folders that exist in my home, the folders that exist in personal, the folders that exist within work, right. I don't need the whole universe, right.

[00:05:43] All right, so let's try this again. So I bet if we go man find We can go to the this manual and probably find something. My guess is its depth right? There we go right away. It's like something that says max depth. Awesome. So let's look up max depth of jump here to the arguments descends at most levels perfect that looks like exactly what we're looking for.

[00:06:02] And also there's min depth looks like exactly what we're looking for side by side. It's almost as if I've planned this out. Awesome. Let's go like this. Let's go back to here we're gonna find with directory. All right, so now what we're going to want to do is we're going to want to do min depth and Max depth.

[00:06:16] So min depth, I wanted this one right, I only want the folders that are one step away from where I'm searching. And I also want the max depth, to be one, only from where we're searching. One depth maximum from our researching. So when I press Enter, look at that.

[00:06:30] It only gives me the folders in my root directory. Awesome. Another cool thing about find is that you can not only provide it a folder to look in, but you can provide it multiple folders to look in. So I can go slash personal slash work, right? And so now, there we go.

[00:06:47] I have All my folders at a singular level right there, none of them are files. They're just folders one deep from where I provided. This is awesome. So this is effectively exactly what we want. So let me let me just take this guy, and yank this and go back here and paste this in here.

[00:07:04] Awesome. There we go. We have the command. So if I were to run this now run this little script, which here let's run this here I'm going to do that sweet little move by the way. I don't know if you just saw what I just did there, but it's it's like the greatest move ever.

[00:07:16] Let me just show you that really quickly. Leader x, look at this little quick vim remap. I do leader x and it will send out to the system chmod which everyone's favorite command to mode, which just changes a files type. To executable and then percent signs current file.

[00:07:34] So if I'm in the file, I can just press leader x and boom. It's now executable one of the greatest remaps ever will likely make it into a YouTube video at some point here soon. So if I LS notice, it's already executable how beautiful is that? It's just so beautiful.

[00:07:46] I love it. So anyway, so now we can run Teamup session either. It does exactly what we wanted it to, right? It's finding all the files, printed them all out, awesome. This is great. So let's go back here. And now we want to pipe this thing to f z F.

[00:08:00] Remember, f cf allow us to fuzzy find. Alright, so let's just do it again. Let's go out here and let's do t mux session Iser and execute that. Wait. Well, we're not even near there. There we go. Let's reopen it. There we go. And go team ox session.

[00:08:14] Iser There we go. I can go, okay, I want to select desktop. Okay, so fcf like it said, takes whatever it finds and pipes it back out to the standard out. Look at that. There's my little desktop. That's exactly what we selected. Awesome. Okay, our scripts coming along nicely