fif

🔍 Quickly find in files using fuzzy search and context

GPL-3.0 License

Stars
6

Requirements

fzf version 0.18.0 or above.

Optional

Concatenation

For file concatenation either rg or ag is optionally required, and fif will fall back to grep if none of those are present. I recommend using rg or ag since those two respect things like git ignore and hidden files. Most options passed to these tools are configurable. More on that below.

Preview

bat version 0.10.0 or above is optionally required to syntax highlight the preview window, if bat isn't present fif will fall back to highlight, then python-pygments, and lastly cat with no colors other than the highlighted line (inverted). I warmly recommend bat, as its fast and does a good job highlighting

Installation

ZSH Plugin manager

zplug

zplug 'roosta/fif'

zgen

zgen load 'roosta/fif'

Antigen

antigen bundle 'roosta/fif'

Manual installation

Clone this repository somewhere and source fif.plugin.zsh or fif.plugin.sh in your shell config.

Demonstration

Usage

fif [FILE|PATH]

Running fif with no arguments will start fuzzy matching lines in all files from pwd.

fif optionally accept a path or file as an argument and will limit search to that scope.

fif ~/some-file.txt

Configuration

Default keybinds

keybind action
Enter Confirm and open file location
Ctrl-s Toggle sort
Ctrl-p Toggle preview

Options

To use these options export environment variables and source fif.plugin.zsh

export FIF_ALIAS="my-alias"
source ~/fif-location/fif.plugin.zsh
my-alias ~/file.txt

By default fif tries to use $EDITOR (see editor.sh) to open a given file, but since different editors have different syntax, this variable exist to help setting up a custom editor. fif works out of the box with Vim and Emacs but say I wanted to use visual studio code:

First create a script file that takes two arguments. The first argument is the line number of the selected match, the second is the file that is to be opened.

So a custom editor script for visual studio code would look like this:

code --goto "${2}:${1}"

Save the file somewhere and point the FIF_EDITOR_SCRIPT variable to said file.

export FIF_EDITOR_SCRIPT="~/my-editor-script.sh"

Remember to set execute permissions for the script.

Environment that contains the default options when using fzf via fif. Default is:

export FIF_FZF_OPTS="
--ansi
--bind='ctrl-s:toggle-sort'
--bind='ctrl-p:toggle-preview'
--preview-window=up
"

in combination with what's defined in FZF_DEFAULT_OPTS. (No need to repeat the options already defined in FZF_DEFAULT_OPTS)

# Single line
export FIF_GREP_OPTS="--color=always --exclude-dir={.git,.svn,CVS}"

# Multiline
export FIF_GREP_OPTS="\
  --color=always \
  --exclude-dir={.git,.svn,CVS} \
  "

export FIF_GREP_COLORS="mt=97:ln=33:fn=34:se=37"

This will color filenames(fn) with blue, line number(ln) as yellow, line contents(mt) as bright white, and separators(se) as white

Check out the Environment section in the grep manual for an overview.

# Single line
export FIF_RG_OPTS="--hidden --color always --colors=match:none --colors=path:fg:blue --colors=line:fg:yellow --follow"

# Multiline
export FIF_RG_OPTS="\
  --hidden \
  --color always \
  --colors=match:none \
  --colors=path:fg:blue \
  --colors=line:fg:yellow \
  --follow \
  "

# Multiline
export FIF_AG_OPTS="\
  --hidden \
  --color \
  --color-path 34 \
  --color-match 97 \
  --color-line-number 33 \
  --follow \
  "

# Single line

export FIF_AG_OPTS="--hidden --color --color-path 34 --color-match 97 --color-line-number 33 --follow"

Colors used are blue for path, bright white for match, and yellow line number

Attribution

Forgit

Used as an example on how to do zsh plugins and how to handle options. Also took some pointer from its README.

Enhancd

Used for reference, and pointers