Cheatsheet & Examples: fzf
The fzf command is a powerful, keyboard-driven fuzzy finder for the command line, enabling quick selection of items from a list with pattern matching and interactive filtering. It is commonly used to search files, history, or command outputs.
Search files in the current directory
Example Usage:
ls | fzf
What it does: Displays a list of files in the current directory, allowing interactive fuzzy search to select a file.
Command-line Arguments Explained:
ls: Lists files in the current directory.fzf: Pipes the output oflstofzffor interactive selection.
Preview file contents during search
Example Usage:
ls | fzf --preview 'cat {}'
What it does: Displays a preview of the selected file's content in a split window while searching.
Command-line Arguments Explained:
--preview 'cat {}': Shows the content of the selected file usingcatin a preview pane ({}is replaced by the selected file path).
Search through command history
Example Usage:
history | fzf
What it does: Presents a list of previously executed commands for quick selection and re-execution.
Command-line Arguments Explained:
history: Lists the command history.fzf: Enables fuzzy search on the history list.
Select multiple files interactively
Example Usage:
find . -type f | fzf -m
What it does:
Allows selecting multiple files from the output of find using the --multi flag.
Command-line Arguments Explained:
-m: Enables multi-select mode, letting users choose multiple items.
Search and modify git commit history
Example Usage:
git log --oneline | fzf --print0 | xargs -0 git checkout
What it does:
Selects a git commit from the log and checks it out, using --print0 for safe handling of filenames with spaces.
Command-line Arguments Explained:
--print0: Outputs items separated by null bytes, suitable for use withxargs -0.git log --oneline: Displays commit history in a compact format.
Fuzzy search with custom prompt
Example Usage:
fzf --prompt 'Search: '
What it does:
Displays a custom prompt (e.g., "Search: ") when launching fzf for user input.
Command-line Arguments Explained:
--prompt 'Search: ': Sets the prompt text shown at the top of the interface.
Filter and select lines from a file
Example Usage:
grep 'pattern' file.txt | fzf
What it does: Filters lines matching a pattern from a file and allows interactive selection of one or more lines.
Command-line Arguments Explained:
grep 'pattern' file.txt: Filters lines containing "pattern" from the file.fzf: Provides a fuzzy search interface for the filtered lines.
Search for a file and open it with a viewer
Example Usage:
find . -type f | fzf --preview 'less {}'
What it does:
Searches for files in the current directory and opens the selected file with less for preview.
Command-line Arguments Explained:
--preview 'less {}': Useslessto preview the selected file in the interface.
Search for a command from history and execute
Example Usage:
history | fzf | awk '{print $2}' | xargs bash
What it does:
Selects a command from history and executes it by extracting the command part using awk.
Command-line Arguments Explained:
history: Lists command history.fzf: Enables fuzzy selection of the command.awk '{print $2}': Extracts the command from the history line (assuming the first column is the number).xargs bash: Executes the selected command.
Cycle through options with interactive navigation
Example Usage:
fzf --cycle
What it does:
Enables cycling through search results using the keyboard without typing (useful for plugins like fzf-tab).
Command-line Arguments Explained:
--cycle: Allows navigating through options one by one without typing a search pattern.
Search with exact matching mode
Example Usage:
fzf --exact
What it does: Enforces exact matching of the search pattern instead of fuzzy logic.
Command-line Arguments Explained:
--exact: Disables fuzzy matching and requires the search text to exactly match the item.
Set a fixed height for the interface
Example Usage:
fzf --height 40%
What it does:
Adjusts the size of the fzf interface window to 40% of the terminal height.
Command-line Arguments Explained:
--height 40%: Sets the height of the interface as a percentage of the terminal size.
Reverse the order of displayed items
Example Usage:
fzf --reverse
What it does: Displays search results in reverse order, with the most recently added item at the top.
Command-line Arguments Explained:
--reverse: Inverts the vertical order of items in the interface.
Search with a custom header
Example Usage:
fzf --header 'Select an option'
What it does: Adds a custom header line above the search results to provide context.
Command-line Arguments Explained:
--header 'Select an option': Displays the specified text at the top of the interface.
Use fzf as a CLI for selecting a file
Example Usage:
fzf +m --print0 (with +m for multi-select and --print0 for safe processing)
What it does: Allows selecting multiple files or items with null-terminated output for robustness.
Command-line Arguments Explained:
+m: Enables multi-select mode (alternative to-m).--print0: Outputs selections separated by null bytes, avoiding issues with spaces in filenames.
Bind a custom key to an action
Example Usage:
fzf --bind 'Ctrl-x:execute(echo selected: {} + "\n")'
What it does:
Assigns a custom keybinding (e.g., Ctrl-x) to perform an action based on the selected item.
Command-line Arguments Explained:
--bind 'Ctrl-x:execute(echo selected: {} + "\n")': BindsCtrl-xto a command (here, printing the selected item).
Use fzf in a vertical layout
Example Usage:
fzf --layout=reverse
What it does: Displays the interface in a vertical layout that allows scrolling and selection in a column format.
Command-line Arguments Explained:
--layout=reverse: Arranges the interface in reverse vertical mode (useful for viewing long lists).
Search and highlight matches
Example Usage:
fzf --emphasize
What it does: Highlights matching characters in the search results for better visibility.
Command-line Arguments Explained:
--emphasize: Applies color highlighting to matched parts of the items.
Disable horizontal scrolling
Example Usage:
fzf --no-hscroll
What it does: Prevents horizontal scrolling in the interface, ensuring all text fits within the window width.
Command-line Arguments Explained:
--no-hscroll: Disables horizontal scrolling to avoid truncation of long lines.
Use fzf to select branch names from git
Example Usage:
git branch | fzf
What it does: Interactively selects a git branch name for easier checkout or management.
Command-line Arguments Explained:
git branch: Lists git branches.fzf: Enables fuzzy search to pick a branch.

