FZF

Home

Table of Contents

1 SYNOPSIS

fzf is a versatile command-line fuzzy finder that can be used to quickly search and select files, directories, or any list of items in the terminal. Here are a few examples to help you understand how to use fzf effectively:

2 Basic Usage: File Search

One of the most common uses of fzf is to search for files in your project or file system.

Example: Search for a file in the current directory

find . -type f | fzf

Explanation: find . -type f lists all files (recursively) starting from the current directory. The output of find is piped into fzf. fzf opens an interactive interface where you can type to filter the list of files. You can select a file by navigating with the arrow keys and pressing Enter.

3 Opening Files with fzf

You can combine fzf with commands like vim to open a selected file directly.

Example: Open a file in Vim

vim $(fzf)

Explanation: fzf runs and allows you to select a file. The selected file is passed as an argument to vim using $(fzf). The chosen file will be opened in Vim.

4 Search and Execute Commands

You can use fzf to search through a list of commands or processes and then execute them.

Example: Search through command history and execute

history | fzf | cut -d ' ' -f 4- | bash

Explanation: history lists all previously run commands. The output is piped to fzf for interactive selection. cut -d ' ' -f 4- removes the leading command numbers from the selected history entry. The selected command is executed by piping it to bash.

5 Interactive Git Checkout

You can use fzf to make Git commands more interactive.

Example: Checkout a Git branch interactively

git checkout $(git branch | fzf) Explanation: git branch lists all branches in the repository. The output is piped to fzf, where you can select a branch. The selected branch name is passed to git checkout.

6 Search Processes and Kill

You can search through running processes and kill the selected one.

Example: Kill a process

ps aux | fzf | awk '{print $2}' | xargs kill -9

Explanation: ps aux lists all running processes. The output is piped to fzf for selection. awk '{print $2}' extracts the process ID (PID) from the selected line. xargs kill -9 sends a kill signal to the selected PID.

7 Previewing Files with fzf

You can use fzf with a preview window to see file contents before selecting.

Example: Preview files before opening


fzf --preview="cat {}" --height 40%

Explanation: fzf –preview="cat {}" shows a preview of the selected file using the cat command. {} is a placeholder for the currently highlighted item in fzf. –height 40% limits the fzf interface to 40% of the terminal height, giving more space for the preview.

8 Searching for Text in Files

You can combine fzf with ripgrep to search for text patterns within files.

Example: Search for a string in files and open the result in Vim


rg --files | fzf --preview 'rg --color=always --line-number {}' | xargs -o vim


Explanation: rg –files lists all files. fzf –preview 'rg –color=always –line-number {}' searches within the selected file for matching text, showing a preview of matching lines. The selected file is opened in Vim using xargs -o vim. These examples showcase some of the ways you can integrate fzf into your workflow to make searching, selecting, and executing tasks more efficient in the terminal. The flexibility of fzf allows you to customize its usage for almost any scenario involving lists or text.


rg -l snarvaezsoft | fzf --preview 'rg snarvaezsoft -C 5 --color=always --line-number {}' | xargs -o

 emacs

9 More examples


- Start fzf on all files in the specified directory:
   find {{path/to/directory}} -type f | fzf

 - Start fzf for running processes:
   ps aux | fzf

 - Select multiple files with Shift + Tab and write to a file:
   find {{path/to/directory}} -type f | fzf --multi > {{path/to/file}}

 - Start fzf with a specified query:
   fzf --query "{{query}}"

 - Start fzf on entries that start with core and end with either go, rb, or py:
   fzf --query "^core go$ | rb$ | py$"

 - Start fzf on entries that not match pyc and match exactly travis:
   fzf --query "!pyc 'travis"

Author: Sebastian Emilio Narvaez

Created: 2024-09-02 Mon 03:05

Validate