 Today we're going to talk about a program that I've talked about in previous videos, FZF, the command line fuzzy finder. Every once in a while you find a program that changes your life. FZF is that program for me. I've been using FZF for probably a year or two now and it is just something that helps with coding, it's useful in scripts and just in your shell usage daily. There is a good chance that FZF is already in repositories for your distribution so use your package manager to install it or if you have Git installed you can clone it using this command. Don't worry too much about copying that command from the video. The link in the description will bring you to the notes on PasteSpin of everything I'm going over in this video and you can just copy and paste stuff from there. Once you have cloned the repository you can run the install script. In most cases I usually recommend using your distribution's repositories to install applications but in this particular case running the install script from the Git repository gives you a few more options during setup. It will ask you a few simple yes or no questions and allow you to set up things like auto completion and bash history and Z shell history for your command line. Now in this case installing from the Debian repositories doesn't give me those options although I'm sure there's probably a way to set it up but it's nice to ask you a few things I suggest answering yes to them and we will go over some of those options a little later in the video and you'll see why they're beneficial. Now that FZF is installed how do you use it and what is it for? Once it's installed all you have to do is type FZF and hit enter. By default if you don't give FZF any data what it's going to do is search your current directory and all subdirectories and list all the files. In my particular example there's 409 files listed. I can now use my arrow keys to go through the list of files and select one. Which is cool but not the point of FZF. What FZF does is it takes a list any text you give it and it allows you to search through it but in a fuzzy kind of way. Basically this means you don't have to be exact about what you're typing. Think of it as sort of like grep only interactive and a little different. Now grep is great it's going to be faster than FZF so if you have super large files because I did once run through like a 5 gig text file through FZF and it is a little slow in cases like that but it's being fuzzy about it. So with grep you have to type exactly what you're looking for where with FZF you type kind of what you're looking for and it finds the best matches. So in this example of 409 files I'm going to look for a file that I know is in this directory in a subdirectory somewhere called HUDJS. So all I have to do is type in HUD. Now again with my HUD you can see it's still it's searched through 409 files and it found six matches. It tries to sort those matches by putting the most exact matches up front. So you can see the first file that it found is the HUD file I was looking for but it also found some other files and although it may be hard to see in the camera here it did highlight the letters it's looking for so even though this doesn't say HUD here you can see that it has an H, a U and a D same with this H, U, D. So it's trying to find the best matches and put them in order. At this point I can hit enter and basically what it did is it echoes out the selection I made. Well that seems simple enough. How is it useful? Lots of ways and that's what we're going to look at today. So again just typing FZF lists all the files in my current directory and subdirectory but knowing that I can put that command inside quotations, dollar sign and brackets and maybe I can say cat. Now it will list all those files and this time I'll type in player out of those 409 files it found a file called playerjs and if I hit enter now it has now cat it out that file for me. Similarly instead of cat I can do something like vim. Again I can hit enter, type what I'm looking for enemy, it's found a file and I'll hit enter. And it opened up that script in vim. Are you still not getting it? Don't worry I've got more examples. Now here's an example I've got a very large text file that I can cat out and it's just got a whole lot of information. You can see how long it just takes to cat out. Now I run that same cat command and pipe it into FZF and you can see it's just shy of 200,000 entries. I can use my arrow keys to go up and down and select one. Or again I can start typing April. And as you can see it found all lines with April on it which is quite a bit. We got people named April and we got addresses where there's an April Boulevard. I can select one of these and hit enter. And it returns that line of data which spans across multiple lines. But what if I don't want all of that information? That's a very good question. Remember FZF is just returning text so we continue, we can continue piping into another command such as cut. So now I run that it shows up just like it did before. Again with almost 200,000 lines. I can type in April again, select the line I want, hit enter, and now it returned the fields that I was looking for. That's great. But what if I want to select more than one? You can! We're going to run the same command as before but to the FZF command we're going to add dash M which means multiple selections. Type what you're looking for and now with the selections up on screen you can go to the one you want, hit tab. Go to another one, hit tab. Go to another one, hit tab. And for each one you select it puts a red triangle next to it. At this point I can backspace, type in a new search, find a line I want and hit tab again. But all my previous selections are still selected and I can hit enter. And there are the selections I made with the fields that I wanted. Also if you accidentally select one you don't want just go to it again and hit tab and it will unselect it. Now this is great because I have a general rule when you're writing out scripts or any type of program and you're going to get user input. I try to not let the user type in what's going to be submitted because you never know how they're going to submit it. For example my name is Chris, K-R-I-S. Let's say someone was trying to enter information about me and they misspell my name. Now it's entered wrong but if they have to select from a list they have to select from the list. So if I have a list of users I can have them search through that with fuzzy finder and select the proper one and my name will always be entered correctly and it will be entered the same every time. It's not going to be Chris with a K sometimes. It's not going to be Chris with a CH sometime. It's not going to be Christopher instead of Chris. It's going to be whatever is in that database and the program is restricting them to that. Which is great. Again you can pipe any text into this application. So here's an example with Wget pulling some information from a website. Again all this information is in the description in the video just go to that pay spin and you'll see the URLs for all this along with the examples. But here with Wget using the o-option it's just going to output the file that we're pulling to the screen. So here we have a list of employees with their employee number divided by a pipe symbol. So even though that file is not on my computer I can pull it real time from a website and put it right in a fuzzy finder. Let's do that. Here's that same Wget command piped directly into FZF. We go ahead and hit enter. We get a list of employees with their names and employee numbers. We can start typing for who we're looking for. Type in Smith. I have two Smiths and I can select one and it returns that employee along with their employee number. But let's say we want just the employee number or just the employee name. You would do it how you would normally do it. You just pipe it into cut or op or whatever program you prefer. Here I'm going to get field two dividing by that pipe symbol which should return the user's name. I'm going to choose Chris Smith and as you can see it returned Chris Smith. If I want Chris Smith's employee number I run that same command but get field one. And I've got his employee number. I have a very strong feeling when it comes to businesses and you have employee names and employee numbers it's obviously important to log the employee's number when you're submitting forms and other information into logs and databases because people's names might change. But it's also annoying for someone who's entering the information if they aren't that employee they have to remember everyone's name and or employee number or look at a list of their employee numbers and stuff like this now you can just search for the person's name or their employee number and then have the script grab what it needs and submit it. This is obviously great for script usage but here we're giving them a list of names but you could be giving them a list of other things or maybe you're giving them a list of names but they're not sure what they're selecting the names for. So we want to prompt them somehow. Well FZF has this built in. All you have to do is add to the FZF command the option to dash dash prompt and then give it a prompt. Please select a name. Now when I hit enter where we normally type our search it gives us a prompt and then we can continue typing. Now let's rewind and go back to our original setup. I said you can use your package manager such as app to install FZF but also you can use the git repository as per the instructions on the git repository and the benefits of this again it will go through questions asking if you want to set up certain things and if you're running at least bash or ZShell those are two I've tested out in it will set up history search for you using FZF which is amazing as well as a few other things. Let's go have a quick look at that. Now normally in bash or ZRC or most shells you can hit control R to go through your history of commands and that's no different on this system except for when I installed FZF it asked if I wanted to set up some bash key bindings and I said yes so now if I hit control R instead of the regular search it actually starts using FZF and brings up my history right in FZF and I can start fuzzy finding searching for stuff so I can type in something like Wget and it starts bringing up all my Wget commands remember earlier we searched for people so I started to type Wget now I'm going to type in P E O and so you can see I didn't even type the command exactly how in the history but it did find what I was looking for in my history so now I can choose that first option or go through previous ones and then just hit enter and it brings that command now it hasn't run it yet because I can still make modifications to it but if I hit it again it will now run that command very very cool this is the sort of thing that once you get used to when you go to a system without it it's so aggravating now if you're familiar with Tmux good for you you should be using it if you're not start using it it is an awesome program for splitting screens keeping things running in the background a whole bunch of stuff well when you install FZF using the get command as we did it also installs a program called FZF-Tmux which works just like regular FZF except for it allows you to have your list in a split screen leaving other information on the screen so for this to work the way we want we need to have Tmux running if you don't it just works like FZF normally does so no harm in that but let's go ahead and start Tmux now running that same W get command from before and piping it into FZF but in this case FZF-Tmux when I hit enter instead of FZF taking over the entire screen it's going to split the screen so we can still have our script information at the top of the screen but our search is at the bottom of the screen by default it's going to split the screen right in half but if you give the dash D option in a percentage you're telling it how much of the screen to use for FZF in this case I'm going to go 35% of the screen and as you can see instead of dividing it directly in half it's only using 35% of the screen so again FZF is dividing its search by line so we can echo something with line breaks in it and use it as a selection tool so in this case I'm just going to echo a list of names with line breaks between them right into FZF when I hit enter that list shows up and again I can go through them or I can start searching them as so so again any text from any program that could be outputted to your shell can be piped into FZF and you can search through it like this let's look at another that same example but in a script and then write it through an if statement so here I'm writing that same echo command with the list of names into FZF but I'm taking the output of that and putting into a variable called name then we're checking if name is John because we don't like John we're going to echo out John you suck anybody else's name we're going to say hello name or of course that person's name when we run that command we get that same list of names we can select anyone from the list or again search through them by typing we'll hit enter and when we hit enter in this case it said hello Sarah run it again this time choose Jack and it says hello Jack run it again this time choose John and it lets John know what you think about him again in a lot of case uses this can replace commands like read where the user's inputting information but if they input the wrong information your script may not run properly where if you're using a list that they can search through and select stuff from well now you know what they can and can't select it can also replace things like case statements which I've never been great at using well now I can create a menu allow them to choose it and run certain functions or even better yet here's an example script I made that actually looks at itself and looks at all its functions and gives them all as an option so again there's links to all this in the description of the video in the notes where you can go through this but basically I have a few functions in the script here and what this main function does is that it greps itself finds all its functions and then it greps again and removes the main function from the option uses oct to get just the name of the functions in its file and after running it through fzf it will run whatever function you selected so again you may not be getting what I'm saying but look at this script so what it's doing is I have three or four functions in here and it looks at itself and finds its list of functions and gives it as a list so anytime I want I can add new functions to the script and they will automatically be added to the menu let's give it a try so again right now I have a function called exit I also have a function called hello and I have a function that counts and loops through one through a hundred now when I run this script I am greeted with a list of all the functions except for the main function from that script and I can choose one by using my arrows or of course searching so I can click hello and it ran the hello function which says hello world and good job if I run that command again I can go up to count and it runs the count function so again I kind of created this as a smart adaptive script that all I have to do is add functions to it and it's going to find them and add them to the list I don't have to modify the menu at all so let's go ahead and add a function to that script function I'll just call it new and I'll say echo this is new and of course we can run a bunch of commands in that function whatever we want it to do we'll run that script again and you can see our menu now has an option of new and when we choose it it runs that function and again that example script is in the notes in the description of the video so if you didn't catch everything I did in the video you can rewind and watch it again or just go and get the script and try it yourself another thing to look at is the guy that creates FZF also create a VIM plugin manager and an FZF plugin for VIM so this plugin does lots of things but there's two main things I use it for opening up new files and switching between buffers so here I am in a directory with a few subdirectories and files for a game that I was working on I can open up the main index file with VIM great but now if I want to go into another file in that sub directory for this project I have keys set up in my particular case I can hit comma F and what it does is it split the screen and gave me a list of all the files in this project that I can now search through or arrow through choose one and it opens it up in a new buffer great again I hit comma F in my case that's just the way I have it set up you can set up whatever keys you want it lists all the files and I can choose one and now I can start editing the thing is it didn't close those are the ones they're still open in buffer so how do I get back to them well in my case I hit comma B for buffer it lists all the files I have open in buffers right now and I can go through them I can't get to the update 3js file because that's what I'm currently in but I can choose one of the other ones and get back to it again I hit comma B it lists everything that's in my buffer and I can choose from the files that are open this is great when you're working on a project and you have to switch back and forth between different files in that project and all you have to do is hit a couple of keys and now you can quickly search you could have hundreds of files in those subdirectories and then quickly just start typing whatever file I'm looking for and go to it and then once it's open I can switch back and forth between the buffers this is something Vim can do but it's so much faster being able to have that searchable list at your disposal so anyway I went over a lot of things with fcf it's a great useful program I use it in a lot of my scripts right now it's very easy to use it's a single executable you can install it on your android phone I have it set up in my termux there I also have it's also been compiled for windows I've used it on windows machines it makes them a lot more useful it is just a great overall program that once you get used to it's kind of hard to live without if you've never used before I really suggest using it I hope that I've inspired you to use it and check it out if you have any questions on it let me know and I'll see if I can do more videos on it because I will definitely be using it in future videos but that's it I hope that you enjoyed this I kind of am trying a different style of video I hope you liked it you guys are probably going to complain but let me know in the comments and I hope that you have a great day oh he put music in the background of this video I'm going to have to comment about how horrible and distracting that is