-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive-grep
executable file
·38 lines (33 loc) · 1.08 KB
/
live-grep
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
#!/usr/bin/env bash
# Colors
CATPPUCCIN_GREEN='#a6da95'
CATPPUCCIN_MAUVE='#c6a0f6'
# Key bindings
COPY_FILE_PATH='ctrl-y:execute(echo -n {1}:{2} | pbcopy)'
KEYS="$COPY_FILE_PATH"
# Optional flag for execution and exit behavior
if [[ $1 == '--exit-on-execution' ]]; then
KEYS="$KEYS+abort"
shift # remove the flag from the arguments so it's not passed to the `rg` command
fi
IFS=$'\n' readarray -t selected_matches < <(
rg --color=always --line-number --no-heading --smart-case "${*:-}" |
fzf --ansi \
--border \
--color "hl+:$CATPPUCCIN_GREEN:reverse,hl:$CATPPUCCIN_MAUVE:reverse" \
--delimiter ':' \
--height '100%' \
--multi \
--print-query --exit-0 \
--preview 'bat {1} --highlight-line {2}' \
--preview-window 'right,+{2}+3/3,~3' \
--scrollbar '▍' \
--bind "$KEYS"
)
# Print the absolute file path and line number of each match
for line in "${selected_matches[@]:1}"; do
file=$(echo "$line" | cut -d: -f1)
line_number=$(echo "$line" | cut -d: -f2)
absolute_path=$(realpath "$file")
echo "$absolute_path:$line_number"
done