fzf is a general-purpose command-line fuzzy finder.
It's an interactive Unix filter for command-line that can be used with any
list; files, command history, processes, hostnames, bookmarks, git commits,
etc.
Pros
Portable, no dependencies
Blazingly fast
The most comprehensive feature set
Flexible layout
Batteries included
Vim/Neovim plugin, key bindings, and fuzzy auto-completion
fzf#install() makes sure that you have the latest binary, but it's optional,
so you can omit it if you use a plugin manager that doesn't support hooks.
fzf is being actively developed, and you might want to upgrade it once in a
while. Please follow the instruction below depending on the installation
method used.
fzf will launch interactive finder, read the list from STDIN, and write the
selected item to STDOUT.
find * -type f | fzf > selected
Without STDIN pipe, fzf will use find command to fetch the list of
files excluding hidden ones. (You can override the default command with
FZF_DEFAULT_COMMAND)
vim $(fzf)
Using the finder
CTRL-K / CTRL-J (or CTRL-P / CTRL-N) to move cursor up and down
Enter key to select the item, CTRL-C / CTRL-G / ESC to exit
On multi-select mode (-m), TAB and Shift-TAB to mark multiple items
Emacs style key bindings
Mouse: scroll, click, double-click; shift-click and shift-scroll on
multi-select mode
Layout
fzf by default starts in fullscreen mode, but you can make it start below the
cursor with --height option.
vim $(fzf --height 40%)
Also, check out --reverse and --layout options if you prefer
"top-down" layout instead of the default "bottom-up" layout.
vim $(fzf --height 40% --reverse)
You can add these options to $FZF_DEFAULT_OPTS so that they're applied by
default. For example,
Unless otherwise specified, fzf starts in "extended-search mode" where you can
type in multiple search terms delimited by spaces. e.g. ^music .mp3$ sbtrkt !fire
Token
Match type
Description
sbtrkt
fuzzy-match
Items that match sbtrkt
'wild
exact-match (quoted)
Items that include wild
^music
prefix-exact-match
Items that start with music
.mp3$
suffix-exact-match
Items that end with .mp3
!fire
inverse-exact-match
Items that do not include fire
!^music
inverse-prefix-exact-match
Items that do not start with music
!.mp3$
inverse-suffix-exact-match
Items that do not end with .mp3
If you don't prefer fuzzy matching and do not wish to "quote" every word,
start fzf with -e or --exact option. Note that when --exact is set,
'-prefix "unquotes" the term.
A single bar character term acts as an OR operator. For example, the following
query matches entries that start with core and end with either go, rb,
or py.
^core go$ | rb$ | py$
Environment variables
FZF_DEFAULT_COMMAND
Default command to use when input is tty
e.g. export FZF_DEFAULT_COMMAND='fd --type f'
⚠️ This variable is not used by shell extensions due to the
slight difference in requirements.
(e.g. CTRL-T runs $FZF_CTRL_T_COMMAND instead, vim **<tab> runs
_fzf_compgen_path(), and cd **<tab> runs _fzf_compgen_dir())
The available options are described later in this document.
FZF_DEFAULT_OPTS
Default options
e.g. export FZF_DEFAULT_OPTS="--layout=reverse --inline-info"
Options
See the man page (man fzf) for the full list of options.
Demo
If you learn by watching videos, check out this screencast by @samoshkin to explore fzf features.
fzf-tmux is a bash script that opens fzf in a tmux pane.
# usage: fzf-tmux [LAYOUT OPTIONS] [--] [FZF OPTIONS]# See available options
fzf-tmux --help
# select git branches in horizontal split below (15 lines)
git branch | fzf-tmux -d 15
# select multiple words in vertical split on the left (20% of screen width)
cat /usr/share/dict/words | fzf-tmux -l 20% --multi --reverse
It will still work even when you're not on tmux, silently ignoring -[pudlr]
options, so you can invariably use fzf-tmux in your scripts.
Alternatively, you can use --height HEIGHT[%] option not to start fzf in
fullscreen mode.
fzf --height 40%
Key bindings for command-line
The install script will setup the following key bindings for bash, zsh, and
fish.
CTRL-T - Paste the selected files and directories onto the command-line
Set FZF_CTRL_T_COMMAND to override the default command
Set FZF_CTRL_T_OPTS to pass additional options
CTRL-R - Paste the selected command from history onto the command-line
If you want to see the commands in chronological order, press CTRL-R
again which toggles sorting by relevance
Set FZF_CTRL_R_OPTS to pass additional options
ALT-C - cd into the selected directory
Set FZF_ALT_C_COMMAND to override the default command
Set FZF_ALT_C_OPTS to pass additional options
If you're on a tmux session, you can start fzf in a tmux split-pane or in
a tmux popup window by setting FZF_TMUX_OPTS (e.g. -d 40%).
See fzf-tmux --help for available options.
Fuzzy completion for files and directories can be triggered if the word before
the cursor ends with the trigger sequence, which is by default **.
COMMAND [DIRECTORY/][FUZZY_PATTERN]**<TAB>
# Files under the current directory# - You can select multiple items with TAB key
vim **<TAB># Files under parent directory
vim ../**<TAB># Files under parent directory that match `fzf`
vim ../fzf**<TAB># Files under your home directory
vim ~/**<TAB># Directories under current directory (single-selection)cd**<TAB># Directories under ~/github that match `fzf`cd~/github/fzf**<TAB>
Process IDs
Fuzzy completion for PIDs is provided for kill command. In this case,
there is no trigger sequence; just press the tab key after the kill command.
# Can select multiple processes with <TAB> or <Shift-TAB> keyskill -9 <TAB>
Host names
For ssh and telnet commands, fuzzy completion for hostnames is provided. The
names are extracted from /etc/hosts and ~/.ssh/config.
ssh **<TAB>
telnet **<TAB>
Environment variables / Aliases
unset**<TAB>export**<TAB>unalias**<TAB>
Settings
# Use ~~ as the trigger sequence instead of the default **export FZF_COMPLETION_TRIGGER='~~'# Options to fzf commandexport FZF_COMPLETION_OPTS='--border --info=inline'# Use fd (https://github.com/sharkdp/fd) instead of the default find# command for listing path candidates.# - The first argument to the function ($1) is the base path to start traversal# - See the source code (completion.{bash,zsh}) for the details._fzf_compgen_path() {
fd --hidden --follow --exclude ".git"."$1"
}
# Use fd to generate the list for directory completion_fzf_compgen_dir() {
fd --type d --hidden --follow --exclude ".git"."$1"
}
# (EXPERIMENTAL) Advanced customization of fzf options via _fzf_comprun function# - The first argument to the function is the name of the command.# - You should make sure to pass the rest of the arguments to fzf._fzf_comprun() {
local command=$1shiftcase"$command"in
cd) fzf "$@" --preview 'tree -C {} | head -200' ;;
export|unset) fzf "$@" --preview "eval 'echo \$'{}" ;;
ssh) fzf "$@" --preview 'dig {}' ;;
*) fzf "$@" ;;
esac
}
Supported commands
On bash, fuzzy completion is enabled only for a predefined set of commands
(complete | grep _fzf to see the list). But you can enable it for other
commands as well by using _fzf_setup_completion helper function.
# usage: _fzf_setup_completion path|dir|var|alias|host COMMANDS...
_fzf_setup_completion path ag git kubectl
_fzf_setup_completion dir tree
Custom fuzzy completion
(Custom completion API is experimental and subject to change)
For a command named "COMMAND", define _fzf_complete_COMMAND function using
_fzf_complete helper.
After --, simply pass the original completion arguments unchanged ("$@").
Then, write a set of commands that generates the completion candidates and
feed its output to the function using process substitution (< <(...)).
zsh will automatically pick up the function using the naming convention but in
bash you have to manually associate the function with the command using the
complete command.
fzf is fast and is getting even faster. Performance should not be
a problem in most use cases. However, you might want to be aware of the
options that affect performance.
--ansi tells fzf to extract and parse ANSI color codes in the input, and it
makes the initial scanning slower. So it's not recommended that you add it
to your $FZF_DEFAULT_OPTS.
--nth makes fzf slower because it has to tokenize each line.
--with-nth makes fzf slower as fzf has to tokenize and reassemble each
line.
If you absolutely need better performance, you can consider using
--algo=v1 (the default being v2) to make fzf use a faster greedy
algorithm. However, this algorithm is not guaranteed to find the optimal
ordering of the matches and is not recommended.
Executing external programs
You can set up key bindings for starting external processes without leaving
fzf (execute, execute-silent).
# Press F1 to open the file with less without leaving fzf# Press CTRL-Y to copy the line to clipboard and aborts fzf (requires pbcopy)
fzf --bind 'f1:execute(less -f {}),ctrl-y:execute-silent(echo {} | pbcopy)+abort'
See KEY BINDINGS section of the man page for details.
Reloading the candidate list
By binding reload action to a key or an event, you can make fzf dynamically
reload the candidate list. See #1750 for
more details.
1. Update the list of processes by pressing CTRL-R
The following example uses fzf as the selector interface for ripgrep. We bound
reload action to change event, so every time you type on fzf, the ripgrep
process will restart with the updated query string denoted by the placeholder
expression {q}. Also, note that we used --disabled option so that fzf
doesn't perform any secondary filtering.
If ripgrep doesn't find any matches, it will exit with a non-zero exit status,
and fzf will warn you about it. To suppress the warning message, we added
|| true to the command, so that it always exits with 0.
When the --preview option is set, fzf automatically starts an external process
with the current line as the argument and shows the result in the split window.
Your $SHELL is used to execute the command with $SHELL -c COMMAND.
The window can be scrolled using the mouse or custom key bindings.
# {} is replaced with the single-quoted string of the focused line
fzf --preview 'cat {}'
Preview window supports ANSI colors, so you can use any program that
syntax-highlights the content of a file, such as
Bat or
Highlight:
请发表评论