📒 

How to Install and Use fzf on Linux

fzf (Fuzzy Finder) is a powerful command-line tool that allows you to search through a list of items interactively. It is particularly useful for finding files, directories, and command history quickly and efficiently in the terminal. In this article, we will cover how to install fzf on Linux and explore its various uses to enhance your productivity.

What is fzf?

fzf is a general-purpose command-line fuzzy finder. It allows you to filter through large sets of data, making it easier to find what you need. You can use it to search for files, command history, Git branches, and more, all with a simple and intuitive interface.

Installing fzf on Linux

Installing fzf on Linux is straightforward. You can choose from several methods depending on your package manager or preferences.

Method 1: Using Git

  1. Clone the Repository:Open your terminal and run the following command:
    git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
  2. Run the Install Script:Change to the cloned directory and run the install script:
    cd ~/.fzf
    ./install

    During the installation process, you will be prompted to enable key bindings and completion. You can select the options that suit your workflow.

Method 2: Using Package Managers

You can also install fzf using package managers, which simplifies the process.

  • For Ubuntu/Debian:
    sudo apt install fzf
  • For Fedora:
    sudo dnf install fzf
  • For Arch Linux:
    sudo pacman -S fzf
  • For macOS (using Homebrew):
    brew install fzf

Basic Usage of fzf

Once fzf is installed, you can start using it immediately. Here are some basic commands to get you started:

1. Searching Files

You can use fzf to search for files in your current directory:

fzf

This will bring up an interactive prompt where you can start typing to filter through the files and directories. Press Enter to select a file.

2. Searching Command History

You can also search through your command history using:

history | fzf

This will display your command history in the fzf interface, allowing you to quickly find and reuse commands.

3. Using fzf with find

To search for files using the find command, you can combine it with fzf:

find . -type f | fzf

4. Using fzf with Git

If you want to search through Git branches, you can use:

git branch | fzf

This command allows you to quickly find and switch to a branch.

Integrating fzf with Other Commands

fzf can be integrated with various commands to enhance its functionality. Here are some examples:

1. Open Selected File with vim

To open a selected file in vim, use:

vim $(fzf)

2. Copy Selected Path to Clipboard

You can copy the selected file path to your clipboard with:

fzf | xclip -selection clipboard

3. Using fzf with ssh

If you frequently use SSH to connect to different servers, you can streamline the process:

ssh $(cat ~/.ssh/config | grep Host | fzf)

Customizing fzf

fzf is highly customizable. You can change its appearance and behavior by setting environment variables or using configuration files.

1. Setting Colors

You can customize the colors used by fzf by exporting the following environment variables:

export FZF_DEFAULT_OPTS="--color=bg+:yellow,bg:blue,fg:white"

2. Setting Key Bindings

fzf allows you to create custom key bindings for different actions. You can add the following lines to your shell configuration file (e.g., ~/.bashrc or ~/.zshrc):

# Ctrl+T to find files
bind -x '"\C-t": fzf-file-widget
# Ctrl+R to search command history
bind -x '"\C-r": fzf-history-widget'

3. Configuration File

You can create a configuration file for persistent options:

echo "export FZF_DEFAULT_OPTS='--height 40% --reverse --inline-info'" >> ~/.fzf.zsh

Conclusion

fzf is an incredibly useful tool for enhancing productivity in the Linux terminal. Its ability to quickly filter through large sets of data makes it an essential addition to any command-line workflow. By installing and integrating fzf into your daily tasks, you can streamline your processes and improve efficiency. Whether you’re searching for files, navigating your command history, or managing Git branches, fzf can significantly enhance your terminal experience.