# fzf
Links: [[Vim]], [[ripgrep]], [[fzf-tab]]
## Install
### Install from source
Prerequisites: [[Oh My Zsh]]
```bash
# fzf
git clone https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
# Config options: Y, Y, N
# No need to update bashrc / zshrc since we're using .dotfiles config
```
### Install with [[apt]]
```bash
sudo apt install fzf
```
### Install on [[macOS]]
```bash
brew install fzf
# To install useful keybindings and fuzzy completion
$(brew --prefix)/opt/fzf/install
# Enable syntax-highlighted previews for fzf and ripgrep
brew install bat
```
### Add to bashrc / zshrc
```bash
# fzf
# Setup
if [[ ! "$PATH" == */usr/local/opt/fzf/bin* ]]; then
export PATH="${PATH:+${PATH}:}/usr/local/opt/fzf/bin"
fi
# Auto-completion
[[ $- == *i* ]] && source "/usr/local/opt/fzf/shell/completion.bash" 2> /dev/null
# Key bindings
# source "/usr/local/opt/fzf/shell/key-bindings.bash"
```
### [[CentOS]]
(from git)
```bash
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
# Config options: Y, Y, N
# No need to update bashrc / zshrc if using .dotfiles config
```
### Use in [[Vim]]
```vim
set rtp+=/usr/local/opt/fzf
```
### Use with [[ripgrep]]
https://github.com/junegunn/fzf#3-interactive-ripgrep-integration
- [ ] TODO implement
From Philip's dotfiles:
https://github.com/phlip9/dotfiles/blob/master/bashrc#L421
In `common`:
```bash
if [ -x "$(command -v rg)" ]; then
export FZF_DEFAULT_COMMAND='rg --files --fixed-strings --ignore-case '\
'--no-ignore --hidden --follow '\
'--glob "!.git/*" --glob "!target/*" '
fi
```
## Configuration
### Keybindings
In the man page:
```text
KEY/EVENT BINDINGS
--bind option allows you to bind a key or an event to
one or more actions. You can use it to customize key
bindings or implement dynamic behaviors.
--bind takes a comma-separated list of binding
expressions. Each binding expression is KEY:ACTION or
EVENT:ACTION.
e.g. fzf --bind=ctrl-j:accept,ctrl-k:kill-line
```
## Tabs
See Dev Profile > fzf Space
## Usage
### Search Syntax
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$
```
### Fuzzy Completion
#### Files and directories
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>`
```bash
# 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.
```bash
# Can select multiple processes with <TAB> or <Shift-TAB> keys
kill -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.
```bash
ssh **<TAB>
telnet **<TAB>
```
#### Environment variables / Aliases
```bash
unset **<TAB>
export **<TAB>
unalias **<TAB>
```