Credit: Lucas Gouveia/How-To Geek
The terminal is clunky and inefficient without additional customizations. If you’re a beginner, you may feel that this is as good as it gets, but it’s not. One tool in particular stands out and can improve your experience drastically. I outline several reasons why fzf is my favorite terminal tool.
What is fzf, and why is it amazing?
The fzf command is a tool for performing rapid searches over large lists of items. That may not sound very appealing, but how do you typically search through a vast number of choices on the terminal? For example, when choosing a file path from thousands of others—how do you achieve that? Do you struggle with tab completion, or do you perform some form of awkward terminal acrobatics?…
Credit: Lucas Gouveia/How-To Geek
The terminal is clunky and inefficient without additional customizations. If you’re a beginner, you may feel that this is as good as it gets, but it’s not. One tool in particular stands out and can improve your experience drastically. I outline several reasons why fzf is my favorite terminal tool.
What is fzf, and why is it amazing?
The fzf command is a tool for performing rapid searches over large lists of items. That may not sound very appealing, but how do you typically search through a vast number of choices on the terminal? For example, when choosing a file path from thousands of others—how do you achieve that? Do you struggle with tab completion, or do you perform some form of awkward terminal acrobatics? Neither is a suitable choice, and fzf addresses this problem directly.
Installation
To follow along, you will need to install the fzf package.
On Debian, you should execute the following installation command.
sudo apt install fzf
On Fedora, you should execute the following installation command.
sudo dnf install fzf
On Arch Linux, you should execute the following installation command.
sudo pacman -S fzf
Now, to enable fzf’s advanced features, you need to add a line to your shell configuration file.
If you use Bash, put the following command into your ~/.bashrc file and reload your shell with source ~/.bashrc.
eval "$(fzf --bash)"
If you use Zsh, put this into your ~/.zshrc file and reload your shell with source ~/.zshrc.
source <(fzf --zsh)
Command history search
Searching your shell history in Bash—whether using the history command or arrow keys—is unbearably slow. With fzf, you can instantly fuzzy search thousands of past commands in real time, which is easily my favorite terminal feature. Once you’ve activated fzf, just press Ctrl+R, start typing, and hit Enter to execute.
There is a similar tool called Atuin that provides a very similar experience.
Fast directory navigation
Navigating the file system can be time-consuming on the terminal, and after using it for some time, you may want a faster method. With fzf’s advanced terminal features enabled, you can press Alt+C to quickly jump to any subdirectory. When you do that, fzf will present a recursive list of subdirectories, which allows you to quickly narrow them down and select one. This is helpful when you’re trying to drill down into a deeply nested subdirectory.
Quick file search and selection
Often, when typing out a command, you need to choose a file. Using tab completion is one approach, but it’s not the best. A better approach is to use a fuzzy finder, and fzf provides a built-in file picker.
Start by typing out your command, and when it comes to choosing a file, press Ctrl+T. The fzf file picker will activate, recursively searching the current working directory. When you press Enter on a choice, it will complete your command with the chosen file.
When you get the hang of it, activating fzf mid-command becomes second nature, and it drastically reduces the time to write the command.
Easy process termination
Killing a Linux process can be a guessing game when you don’t know its exact name. Commands like pgrep, killall, and kill all require the process name or ID, which can be difficult to remember at times. With fzf’s shell integration, you can quickly search and select the process you want to terminate.
To kill processes with fzf, enter the following command and press the Tab key (do not press Enter):
kill -9 **<TAB>
Using the fzf command allows you to rapidly select a process and kill it; it’s much faster than the other commands, and it’s forgiving when you’ve forgotten the exact name of the process.
Bonus commands
The “**” syntax triggers fzf’s search window. The fzf commands you added to your shell configuration earlier created functions that handle this syntax and enable other fzf features.
The “**” trigger works with almost any command; some commands are handled specially (like the SSH command), and the rest default to a file picker. I encourage you to try it out, keeping in mind that “**” activates a file picker by default.
SSH
SSH receives special handling. Tab-completing with ** lets you fuzzy search hosts from your SSH config, known hosts, and /etc/hosts.
ssh **<TAB>
Git
The fzf author provides an official Git script called fzf-git.sh. If you follow the installation instructions on the repository README, you can gain access to rapid fuzzy completions over Git operations.
Environment Variables
Execute the following command to quickly know what the value of any environment variable is.
env | fzf
You can even quickly unset an environment variable with fzf:
unset **<TAB>
Systemd
Systemd is the init system for most distros; it handles the boot process, including system services. Managing systemd unit files is painful, and starting and stopping services benefits greatly from a fzf-based script. There is such a script, called fuzzy-sys. To use it, follow the instructions in the script’s repository README, then execute the following command.
fuzzy-sys --status
That will show you the status of any service.
It supports several flags, including start, stop, enable, and edit, etc. This script makes choosing a service a breeze; now you don’t need to type out long and awkward service names.
Fzf dramatically improves terminal productivity and offers an extensive list of community scripts. Complex commands that once took minutes to write and execute now take seconds—writing and running multiple complicated commands in quick succession is now incredibly easy.