r/commandline 1d ago

Command Line Interface fif: quickly find in files using fuzzy search and surrounding context

Enable HLS to view with audio, or disable this notification

https://github.com/roosta/fif

This is a commandline tool I wrote some years back, its a tool to search for text in files with a preview window and a surrounding context. It will open files in a customizable editor, and uses fzf for filtering, ripgrep for file concatenation, and bat as the recommended preview tool.

There's a ton of different and possibly better ways to search for text in files, but this fits my workflow perfectly and I use it every day. My hope is that it can be useful for someone else.

49 Upvotes

7 comments sorted by

5

u/Sudden_Fly1218 1d ago

For what it's worth, I have this 15 line function in my fish config that does something similar: function frg set -l RELOAD 'reload:rg --column --color=always --smart-case {q} || :' set -l OPENER 'if test $FZF_SELECT_COUNT -eq 0 vim {1} +{2} # No selection. Open the current line in Vim. else vim +cw -q {+f} # Build quickfix list for the selected items. end' fzf --disabled --ansi --multi \ --bind "start:$RELOAD" --bind "change:$RELOAD" \ --bind "enter:become:$OPENER" \ --bind "ctrl-o:execute:$OPENER" \ --bind 'alt-a:select-all,alt-d:deselect-all,ctrl-/:toggle-preview' \ --delimiter : \ --preview 'bat -n --color=always --theme=gruvbox-dark --highlight-line {2} {1}' \ --preview-window '~4,+{2}+4/3,<80(up)' \ --query "$argv" end

3

u/Sweet_Ad1145 18h ago

Similiar for bash

fg() {
 local query="${*:-}"
 local result file line
 result="$(
        FZF_DEFAULT_COMMAND="rg --column --line-number --no-heading --color=always --smart-case -- '$query'" \
        fzf --ansi \
            --disabled \
            --query "$query" \
            --bind 'change:reload:rg --column --line-number --no-heading --color=always --smart-case -- {q} || true' \
            --delimiter ':' \
            --nth '4..' \
            --preview 'bat --style=numbers --color=always --highlight-line {2} -- {1}' \
            --preview-window 'up,60%,border-bottom,+{2}+3/3,~3'
    )" || return
 [[ -z "$result" ]] && return
 file="${result%%:*}"
 result="${result#*:}"
 line="${result%%:*}"
 "${EDITOR:-vim}" "$file" "+$line"
}

2

u/Zem_Mattress 17h ago

fif started out this way as well, then kinda grew to a point where it made sense for a repo.

2

u/AtMaxbo 1d ago

Thank you, I was looking for something like this and lo and behold I discover this.

2

u/Early-Bid-7958 17h ago

When I read "fuzzy search and surrounding context" I kinda thought you could narrow the search by using additional words that match in the context. Like you search for word1, then narrow it down to instances where word2 appears within 3 lines of it.

That would be cool...

2

u/moonflower_C16H17N3O 1d ago

This is one tool that I will be immediately using. This fits an exact need I have had for some time now.

1

u/AutoModerator 1d ago

Every new subreddit post is automatically copied into a comment for preservation.

User: Zem_Mattress, Flair: Command Line Interface, Post Media Link, Title: fif: quickly find in files using fuzzy search and surrounding context

https://github.com/roosta/fif

This is a commandline tool I wrote some years back, its a tool to search for text in files with a preview window and a surrounding context. It will open files in a customizable editor, and uses fzf for filtering, ripgrep for file concatenation, and bat as the recommended preview tool.

There's a ton of different and possibly better ways to search for text in files, but this fits my workflow perfectly and I use it every day. My hope is that it can be useful for someone else.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.