The problem
Let’s face it, using the terminal to do even the simplest of tasks can be a pain. For example, let’s say you want to find a file in your project. You can use the find
command to do that. But, it’s not very user-friendly. You have to remember the exact name of the file you’re looking for. If you don’t remember the exact name, you’ll have to use the find
command with the -name
option and then pipe the output to grep
to search for the file. This is a lot of work and can be very time-consuming.
The same issue applies to everything else as soon as we need to find something from a list. Here are a few examples:
- Changing directories
- Finding a command from the history
- Finding a process from the list of running processes
- …and so on
The solution
In a nutshell, fzf
is a powerful fuzzy finder that can be used with any list. That means that we can use fzf
to quickly filter through large lists of files, directories, commands, processes, …etc. fd
has some neat features when it comes to searching for files.
Each tool deserves its own blog post. This article is more of a guide to help you get started with fzf
and fd
and get some ideas on how to speed up your workflow. As a quick into, here’s the official description of fzf
and fd
from their respective GitHub repositories:
fd
fd
is a program to find entries in your filesystem. It is a simple, fast and user-friendly alternative tofind
. While it does not aim to support all of find’s powerful functionality, it provides sensible (opinionated) defaults for a majority of use cases.
fzf
fzf is a general-purpose command-line fuzzy finder.
It’s an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.
Installation
If you can’t find installation commands for your OS, please refer to the official documentation for each tool.
# Homebrew is required for easy installation
brew install fd
brew install fzf
# Or, try the following if homebrew doesn't work as expected
# git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
# ~/.fzf/install
sudo apt install fd-find
sudo apt install fzf
# Chocolatey is required for easy installation
choco install fd
choco install fzf
apk add fd
apk add fzf
Configuration
As mentioned before fzf
works great when combined with fd
. Let’s start by configuring fzf
to use fd
as the default file finder. To do that, we need to add the following line to our .bashrc
or .zshrc
file:
export FZF_DEFAULT_COMMAND='fd --type f'
You can also add a few arguments to better suit your needs. This is my favorite configuration:
export FZF_DEFAULT_COMMAND='fd --hidden --follow --exclude .git --exclude node_modules'
Let’s break down the arguments:
--hidden
- Include hidden files and directories--follow
- Follow symlinks--exclude .git
- Exclude.git
directory--exclude node_modules
- Excludenode_modules
directory
You’re all set! now if you run fzf
in the terminal you’ll see a recursive list of files in your current directory which you can filter through using fuzzy search.
Usage
History
If you enabled keyboard bindings for fzf
during the installation process (which is the default), you can use Ctrl + R
to search through your command history. This is a great way to quickly find a command you used before.
or you can use the following command:
history | fzf
Processes
You can use fzf
to quickly find a process from the list of running processes. To do that, run the following command:
ps aux | fzf
Killing a process is as easy as running the following command:
kill -9 **<TAB>
Files
You can use fzf
to quickly find a file in your project. To do that, run the following command:
fzf
Directories
You can use fzf
to quickly change directories. To do that, run the following command:
cd $(fzf)
You can also use the keyboard shortcut Alt+C
to do the same thing.
Editing files
You can use fzf
to quickly edit a file in your project. To do that, run the following command:
vim **<TAB>
# Or, if you prefer nano
nano **<TAB>
Conclusion
We barely scratched the surface of what fzf
and fd
can do. The official documentation is a great place to learn more about these tools and take them even further. I hope you found this article useful, if you have any questions or suggestions, feel free to leave a comment below.