summaryrefslogtreecommitdiff
path: root/fedora/.local/bin/rgafiles
diff options
context:
space:
mode:
authorTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2026-04-28 15:42:50 +0900
committerTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2026-04-28 15:42:50 +0900
commitae78dbbff81196f1d7bc8fabf84d05e6b9f3ca03 (patch)
treefdc69ee3e2772aa4db7e8efe4bd30d101c7f82ac /fedora/.local/bin/rgafiles
parent06ad645351572c0e7188c52028998384d718df2e (diff)
updatesHEADmaster
Diffstat (limited to 'fedora/.local/bin/rgafiles')
-rwxr-xr-xfedora/.local/bin/rgafiles108
1 files changed, 97 insertions, 11 deletions
diff --git a/fedora/.local/bin/rgafiles b/fedora/.local/bin/rgafiles
index e8b5e72..2fc8488 100755
--- a/fedora/.local/bin/rgafiles
+++ b/fedora/.local/bin/rgafiles
@@ -2,7 +2,7 @@
# Usage function to display script options
usage() {
- echo "Find files using ripgrep and open them in Neovim."
+ echo "Find files using ripgrep and open them in Vim."
echo ""
echo "Usage: ${0##*/} [-s] [-i] [-l] [-p] [<tag>] <query>"
echo ""
@@ -17,7 +17,7 @@ usage() {
echo "Examples:"
echo " ${0##*/} -p TODO 'KEYWORD' # Search for 'KEYWORD' in files tagged with 'TODO' in the project directories"
echo " ${0##*/} -l -p 'KEYWORD' # List files associated with the default 'PROJECT' tag and 'KEYWORD'"
- echo " ${0##*/} 'KEYWORD' # Open files containing 'KEYWORD' in nvim"
+ echo " ${0##*/} 'KEYWORD' # Open files containing 'KEYWORD' in vim"
exit 0
}
@@ -36,10 +36,24 @@ search_term() {
# Construct the preview command
preview_cmd=$(printf "rga %s --pretty --context 10 '%s' {}" "$case_flag" "$*")
- rga_output=$(rga --follow --no-ignore --hidden --text --max-count=1 ${case_flag:+$case_flag} --files-with-matches --no-messages --glob '!**/.git/*' "$*")
+ query="$*"
+ rga_output=$(rga --follow --no-ignore --hidden --text --max-count=1 ${case_flag:+$case_flag} --files-with-matches --no-messages \
+ --glob '!**/.cache/*' \
+ --glob '!**/.git/*' \
+ --glob '!**/.github/*' \
+ --glob '!**/.next/*' \
+ --glob '!**/.venv/*' \
+ --glob '!**/build/*' \
+ --glob '!**/coverage/*' \
+ --glob '!**/dist/*' \
+ --glob '!**/node_modules/*' \
+ --glob '!**/target/*' \
+ --glob '!**/vendor/*' \
+ --glob '!**/venv/*' \
+ "$query")
# Use fzf to select files
- files=$(echo "$rga_output" | fzf-tmux +m --preview="$preview_cmd" --reverse --multi --select-1 --exit-0) || return 1
+ files=$(echo "$rga_output" | fzf +m --preview="$preview_cmd" --reverse --multi --select-1 --exit-0) || return 1
# Check if files are selected
if [ -z "$files" ]; then
@@ -48,9 +62,42 @@ search_term() {
fi
# copy target to the clipboard
- echo "$@" | xclip -selection clipboard 2>/dev/null
-
- openfiles "$files"
+ echo "$query" | xclip -selection clipboard 2>/dev/null
+
+ # Open files at matching lines
+ count=$(echo "$files" | wc -l)
+ if [ "$count" -eq 1 ]; then
+ # Find the first matching line number in the selected file
+ line=$(rga --max-count=1 --line-number --no-filename ${case_flag:+$case_flag} --no-messages "$query" "$files" 2>/dev/null | head -1 | cut -d: -f1)
+ if [ -n "$line" ]; then
+ ${EDITOR:-vim} "+${line}" "$(realpath "$files")"
+ else
+ openfiles "$files"
+ fi
+ else
+ # Multiple files: open at matching lines via quickfix
+ tmpfile=$(mktemp)
+ echo "$files" | while IFS= read -r file; do
+ match=$(rga --max-count=1 --line-number --no-filename ${case_flag:+$case_flag} --no-messages "$query" "$file" 2>/dev/null | head -1)
+ if [ -n "$match" ]; then
+ printf '%s:%s\n' "$(realpath "$file")" "$match" >> "$tmpfile"
+ else
+ printf '%s:1:\n' "$(realpath "$file")" >> "$tmpfile"
+ fi
+ done
+ if [ "$count" -lt 5 ]; then
+ ${EDITOR:-vim} -q "$tmpfile" \
+ -c 'for i in range(2,len(getqflist())) | vsplit | execute "cc ".i | endfor | wincmd t'
+ else
+ first_line=$(head -1 "$tmpfile" | cut -d: -f2)
+ file_args=""
+ while IFS=: read -r fpath _; do
+ file_args="$file_args \"$fpath\""
+ done < "$tmpfile"
+ eval "${EDITOR:-vim}" "+${first_line}" "$file_args"
+ fi
+ rm -f "$tmpfile"
+ fi
# print the file names
echo "$rga_output"
@@ -68,7 +115,20 @@ list_or_open_project_files() {
rga_output=""
for path in $project_paths; do
if [ -d "$path" ]; then
- rga_result=$(rga --follow --no-ignore --hidden --text --max-count=1 --files-with-matches --no-messages --glob '!**/.git/*' "$project_tag" "$path")
+ rga_result=$(rga --follow --no-ignore --hidden --text --max-count=1 --files-with-matches --no-messages \
+ --glob '!**/.cache/*' \
+ --glob '!**/.git/*' \
+ --glob '!**/.github/*' \
+ --glob '!**/.next/*' \
+ --glob '!**/.venv/*' \
+ --glob '!**/build/*' \
+ --glob '!**/coverage/*' \
+ --glob '!**/dist/*' \
+ --glob '!**/node_modules/*' \
+ --glob '!**/target/*' \
+ --glob '!**/vendor/*' \
+ --glob '!**/venv/*' \
+ "$project_tag" "$path")
rga_output="$rga_output $rga_result"
fi
done
@@ -86,9 +146,35 @@ list_or_open_project_files() {
if [ "$list_mode" -eq 1 ]; then
echo "$rga_output"
else
- # Otherwise, open the files with nvim
- set -- "$(printf "%s\n" "$rga_output")"
- openfiles "$@"
+ # Open files at matching lines
+ file_count=$(echo "$rga_output" | wc -w)
+ if [ "$file_count" -eq 1 ]; then
+ line=$(rga --max-count=1 --line-number --no-filename --no-messages "$project_tag" "$rga_output" 2>/dev/null | head -1 | cut -d: -f1)
+ if [ -n "$line" ]; then
+ ${EDITOR:-vim} "+${line}" "$(realpath "$rga_output")"
+ else
+ openfiles "$rga_output"
+ fi
+ else
+ # Multiple files: open at matching lines via quickfix
+ tmpfile=$(mktemp)
+ for file in $rga_output; do
+ match=$(rga --max-count=1 --line-number --no-filename --no-messages "$project_tag" "$file" 2>/dev/null | head -1)
+ if [ -n "$match" ]; then
+ printf '%s:%s\n' "$(realpath "$file")" "$match" >> "$tmpfile"
+ else
+ printf '%s:1:\n' "$(realpath "$file")" >> "$tmpfile"
+ fi
+ done
+ if [ "$file_count" -lt 5 ]; then
+ ${EDITOR:-vim} -q "$tmpfile" \
+ -c 'for i in range(2,len(getqflist())) | vsplit | execute "cc ".i | endfor | wincmd t'
+ else
+ ${EDITOR:-vim} -q "$tmpfile" \
+ -c 'silent! for i in range(2,len(getqflist())+1) | execute "silent! cc ".i | endfor | silent! cfirst'
+ fi
+ rm -f "$tmpfile"
+ fi
fi
}