How to use the command fzf (with examples)
fzf is a command-line fuzzy finder that allows users to quickly search and select items from a list. It is similar to the “sk” command and can be used to search and filter through various types of data, such as files, running processes, and more.
Use case 1: Start fzf on all files in the specified directory
Code:
find path/to/directory -type f | fzf
Motivation: This use case is helpful when you want to search and select a specific file from a large directory. Instead of manually navigating through the directory structure, fzf allows you to quickly search and filter files using fuzzy matching.
Explanation:
find path/to/directory -type f
- This command lists all files in the specified directory. Thefind
command is used to traverse a directory recursively and the-type f
option specifies that only regular files should be included.|
- The pipe symbol used in shell commands allows the output of the previous command to be used as input for the next command.fzf
- This command starts the fzf fuzzy finder on the list of files generated by the previous command.
Example output:
/path/to/directory/file1.txt
/path/to/directory/file2.txt
/path/to/directory/file3.txt
Use case 2: Start fzf for running processes
Code:
ps aux | fzf
Motivation: When working with multiple processes running on the system, it can be difficult to locate a specific process to perform actions such as killing or inspecting its details. Using fzf with the ps aux
command helps to quickly search and select a process based on its name or other attributes.
Explanation:
ps aux
- This command lists all running processes along with detailed information. Theaux
options are used to display all processes for all users in a user-friendly format.|
- The pipe symbol used in shell commands allows the output of the previous command to be used as input for the next command.fzf
- This command starts the fzf fuzzy finder on the list of running processes generated by the previous command.
Example output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.2 225672 26848 ? Ss Jul21 0:04 /sbin/init
root 2 0.0 0.0 0 0 ? S Jul21 0:00 [kthreadd]
...
Use case 3: Select multiple files with Shift + Tab and write to a file
Code:
find path/to/directory -type f | fzf --multi > path/to/file
Motivation: This use case is useful when you need to select multiple files from a list and save the selections to a file. It allows you to efficiently perform batch operations on selected files or create a custom list for further processing.
Explanation:
find path/to/directory -type f
- This command lists all files in the specified directory, as in the first use case.|
- The pipe symbol used in shell commands allows the output of the previous command to be used as input for the next command.fzf --multi
- The--multi
option enables multiple selection mode in fzf, allowing you to select multiple files usingShift + Tab
.>
- This symbol is used for output redirection, where the output of the previous command is saved to a file.path/to/file
- This is the path to the file where the selected files will be saved.
Example output: Selected files: file1.txt, file2.txt
Use case 4: Start fzf with a specified query
Code:
fzf --query "query"
Motivation: When you know the specific item you want to search for in a large list, using fzf with a specified query simplifies the searching process. It immediately displays the matched item and allows you to proceed with further actions.
Explanation:
--query "query"
- The--query
option followed by a string allows you to pre-fill the fzf search input with the specified query string.query
- This is the string you want to search for in the list.
Example output: If the query is “apple” and the list contains “apple”, “banana”, and “orange”, the result will be:
apple
Use case 5: Start fzf on entries that start with core and end with either go, rb, or py
Code:
fzf --query "^core go$ | rb$ | py$"
Motivation: This use case is beneficial when you want to filter a list based on specific patterns. In this example, it filters out entries that start with “core” and end with either “go”, “rb”, or “py”.
Explanation:
--query "^core go$ | rb$ | py$"
- The--query
option followed by a regular expression allows you to filter the list based on the specified pattern.^core go$
- This pattern filters entries that start with “core” and end with “go”.rb$
- This pattern filters entries that end with “rb”.py$
- This pattern filters entries that end with “py”.
Example output: If the list contains “corego”, “ruby”, “corepy”, and “python”, the matching entries will be:
corego
corepy
Use case 6: Start fzf on entries that do not match pyc and match exactly travis
Code:
fzf --query "!pyc 'travis"
Motivation: This use case is helpful when you want to exclude specific entries and match exactly another entry in a list. It allows you to filter the list based on negation and exact matching.
Explanation:
--query "!pyc 'travis"
- The--query
option followed by a regular expression allows you to filter the list based on the specified pattern.!pyc
- This pattern excludes entries that match “pyc”.'travis
- This pattern matches entries exactly equal to “travis”.
Example output: If the list contains “pycfile”, “trav”, “travis”, and “awesome”, the matching entry will be:
travis