Skip to content

Instantly share code, notes, and snippets.

@junegunn

junegunn/ag-change

Last active Dec 18, 2019
Embed
What would you like to do?
Restarting source command
#!/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
#!/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
@junegunn

This comment has been minimized.

Copy link
Owner Author

@junegunn junegunn commented Dec 18, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment