-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxf
executable file
·47 lines (38 loc) · 1.08 KB
/
xf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
# Check if a search term is provided
if [ -z "$1" ]; then
echo "Usage: $0 <search_term>"
exit 1
fi
if command -v rg >/dev/null; then
:
else
apt update && apt install ripgrep -y
fi
if command -v batcat >/dev/null; then
:
else
apt update && apt install bat -y
fi
# Start an infinite loop for repeated search-and-edit
while true; do
# Run rg and fzf, capturing the selected line
selected=$(rg --line-number --no-heading --color=always "$1" | \
fzf --ansi \
--preview="batcat --style=numbers --color=always --highlight-line {2} {1}" \
--preview-window=right:50%:wrap \
--delimiter=":" \
--with-nth=1,2,3 \
--bind "enter:accept" \
--bind "esc:abort")
# Exit if no selection is made (e.g., if Esc is pressed)
if [ -z "$selected" ]; then
echo "Exiting."
break
fi
# Extract the file path and line number from the selected item
filepath=$(echo "$selected" | cut -d: -f1)
linenumber=$(echo "$selected" | cut -d: -f2)
# Open the file in vi at the specified line
vi +"$linenumber" "$filepath"
done