-
Notifications
You must be signed in to change notification settings - Fork 0
/
sniplib.sh
executable file
·38 lines (27 loc) · 998 Bytes
/
sniplib.sh
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
sl() {
# N = number of lines to skip
# skip tags line
# https://stackoverflow.com/questions/604864/print-a-file-skipping-first-x-lines-in-bash
local N=1
out=("$(rg "(\"{3}|'{3})[\sa-z0-9]*(\"{3}|'{3})" $1 | fzf --delimiter=: \
--preview='[[ $(file --mime {1}) =~ binary ]] && echo {1} is a binary file || (bat --style=numbers --color=always {1}|| cat {1}) 2> /dev/null | head -500' \
--query="${@:2}" \
--exit-0 --expect=ctrl-e)")
# you can add the following to replace `bat` with something else:
# || highlight -O ansi -l {1} || coderay {1} || rougify {1}
key=$(head -1 <<< "$out")
file=$(head -2 <<< "$out" | tail -1 | cut -d":" -f1)
# if ctrl-e: edit, else:
if [ -n "$file" ]; then
[ "$key" = ctrl-e ] && $EDITOR "$file" || tail -n +$((N+1)) "$file" | clipcopy
fi
}
clipcopy() {
if hash pbcopy 2>/dev/null; then
pbcopy "$@"
else
xclip -sel clip "$@"
fi
}
sl "$@"