Last active
May 28, 2021 02:13
-
-
Save junegunn/4963bab6ace453f7f529d2d0e01b1d85 to your computer and use it in GitHub Desktop.
Restarting source command
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
#!/usr/bin/env bash | |
ag_source() { | |
local query | |
[ -z "$1" ] && query="^(?=.)" || query="$1" | |
ag --nogroup --column --color "$query" 2> /dev/null | |
} | |
filter() { | |
local query="$1" | |
fzf --ansi --expect=enter,double-click --print-query \ | |
--bind 'change:accept' --no-sort --exact --no-extended \ | |
--query="$query" --prompt="$query> " --no-clear --height 50% | |
} | |
trap 'printf "\x1b[J"' EXIT | |
query="$*" | |
while true; do | |
output=$(ag_source "$query" | filter "$query") | |
status=$? | |
[ $status -eq 130 ] || [ $status -eq 2 ] && exit $status | |
mapfile -t lines <<< "$output" | |
query="${lines[0]}" | |
key="${lines[1]}" | |
result="${lines[2]}" | |
[ -z "$key" ] && continue | |
break | |
done | |
mapfile -d ":" -t tokens <<< "$result" | |
file="${tokens[0]}" | |
line="${tokens[1]}" | |
col="${tokens[2]}" | |
if [ -e "$file" ]; then | |
vim "$file" +"normal! ${line}G${col}|" | |
fi |
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
#!/usr/bin/env bash | |
ag_source() { | |
local query | |
[ -z "$1" ] && query="^(?=.)" || query="$1" | |
ag --nogroup --column --color "$query" 2> /dev/null | |
} | |
filter() { | |
local query="$1" | |
fzf --ansi --expect=ctrl-r --print-query \ | |
--header="Press CTRL-R to reset ag query" --no-clear \ | |
--query="$query" --prompt="$query> " --height 40% --reverse --inline-info | |
} | |
trap 'printf "\x1b[J"' EXIT | |
query="$*" | |
while true; do | |
output=$(ag_source "$query" | filter "$query") | |
status=$? | |
[ $status -eq 130 ] || [ $status -eq 2 ] && exit $status | |
mapfile -t lines <<< "$output" | |
query="${lines[0]}" | |
key="${lines[1]}" | |
result="${lines[2]}" | |
[ "$key" = "ctrl-r" ] && continue | |
[ -z "$result" ] && exit 1 | |
break | |
done | |
mapfile -d ":" -t tokens <<< "$result" | |
file="${tokens[0]}" | |
line="${tokens[1]}" | |
col="${tokens[2]}" | |
if [ -e "$file" ]; then | |
vim "$file" +"normal! ${line}G${col}|" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use https://github.com/junegunn/fzf/blob/master/CHANGELOG.md#0190 instead