Skip to content

Instantly share code, notes, and snippets.

@gnanderson
Last active November 3, 2023 15:30
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gnanderson/d74079d16714bb8b2822a7a07cc883d4 to your computer and use it in GitHub Desktop.
Save gnanderson/d74079d16714bb8b2822a7a07cc883d4 to your computer and use it in GitHub Desktop.
Find in file using ripgrep, then fuzzy find matched filenames with fzf, preview match using bat
fif() {
rg \
--column \
--line-number \
--no-column \
--no-heading \
--fixed-strings \
--ignore-case \
--hidden \
--follow \
--glob '!.git/*' "$1" \
| awk -F ":" '/1/ {start = $2<5 ? 0 : $2 - 5; end = $2 + 5; print $1 " " $2 " " start ":" end}' \
| fzf --preview 'bat --wrap character --color always {1} --highlight-line {2} --line-range {3}' --preview-window wrap
}
@donhector
Copy link

donhector commented Nov 4, 2021

Slight variant that leverages a key binding (ctrl+o) for opening the file in vscode at the proper line and column while selected in fzf

fif() {
    rg  \
    --column \
    --no-heading \
    --fixed-strings \
    --ignore-case \
    --hidden \
    --follow \
    --glob '!.git/*' \
    --glob '!.vscode-server/*' \
     "$1" \
    | awk -F  ":" '/1/ {start = $2<5 ? 0 : $2 - 5; end = $2 + 5; print $1 " " $2 " " $3 " " start ":" end}' \
    | fzf \
        --bind 'ctrl-o:execute(code --goto {1}:{2}:{3})+cancel' \
        --preview 'bat --wrap character --color always {1} --highlight-line {2} --line-range {4}' \
        --preview-window wrap
}

The output from awk expression is in the form of:

<filename> <linenum> <col> <linenum-5>:<linenum+5>

fzf lets you access the different space separated fields from its input using the {num} syntax

The syntax for opening a file at specified line and column in vscode is code --goto <filename>:<line>:<column>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment