roosta / fif

πŸ” Quickly find in files using fuzzy search and context

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GitHub

fif is a shell utility to quickly find text in files using fuzzy searching, and a syntax highlighted preview with a surrounding context.

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 'roosta/fif'
zgen load 'roosta/fif'
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

FIF_ALIAS Change the command name of fif via this environment variable.
export FIF_ALIAS="my-alias"
source ~/fif-location/fif.plugin.zsh
my-alias ~/file.txt
FIF_EDITOR_SCRIPT

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.

FIF_FZF_OPTS

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)

FIF_GREP_OPTS Environment variable storing an array of grep options. Default is:
# 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} \
  "
FIF_GREP_COLORS Colors used with grep, default is:
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.

FIF_RG_OPTS Environment variable storing an array of rg options. Defaults:
# 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 \
  "
FIF_AG_OPTS Environment variable storing an array of ag options. Defaults:
# 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

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

Used for reference, and pointers

About

πŸ” Quickly find in files using fuzzy search and context

License:GNU General Public License v3.0


Languages

Language:Shell 100.0%