Fish FZF Configuration

FZF is an interactive Unix filter for command-line that can be used with any list; files, command history, processes, hostnames, bookmarks, git commits, etc.

Tree

Display directories as trees (with optional color/HTML output)

1
brew install tree

fd

Simple, fast and user-friendly alternative to find.

1
brew install fd

ripgrep

ripgrep is a line-oriented search tool that recursively searches the current directory for a regex pattern.

1
brew install ripgrep

fish

User-friendly command-line shell for UNIX-like operating systems.

1
brew install fish

fzf

fzf is a general-purpose command-line fuzzy finder.

1
2
3
4
brew install fzf

# To install useful key bindings and fuzzy completion:
$(brew --prefix)/opt/fzf/install

FZF preview

Let’s create a function that will preview file or directory using mentioned tools

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# .config/fish/functions/__fzf_preview.fish

function __fzf_preview
if file --mime "$argv" | grep -q directory
tree -L 3 "$argv"
else if file --mime "$argv" | grep -q binary
echo "$argv is a binary file"
else
if command --quiet --search bat
bat --color=always --line-range :250 "$argv"
else if command --quiet --search cat
cat "$argv" | head -250
else
head -250 "$argv"
end
end
end

Now change the default option

1
2
3
4
5
6
set -U FZF_DEFAULT_OPTS " \
--multi --cycle --keep-right -1 \
--height=80% --layout=reverse --info=default \
--preview-window right:50%:wrap \
--preview '__fzf_preview {}' \
--ansi"

Let’s make our search faster using fd and ripgrep

1
2
set -U FZF_ALT_C_COMMAND "fd -t d . \$dir"
set -U FZF_CTRL_T_COMMAND "rg --files --no-require-git"