Last active
November 3, 2023 15:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Here's the same solution, but also open in your favorite editor in the exact line number.
fif() {
out=$(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)
read -r filename line <<< "${out}"
${EDITOR:-vim} "${filename}" +"normal! ${line}zz"
}
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
updated to work in zsh