Skip to content

Instantly share code, notes, and snippets.

@shayelkin
Created November 26, 2025 11:17
Show Gist options
  • Select an option

  • Save shayelkin/b4c5cc1cb5c644266fc590a5be1eb710 to your computer and use it in GitHub Desktop.

Select an option

Save shayelkin/b4c5cc1cb5c644266fc590a5be1eb710 to your computer and use it in GitHub Desktop.
A replacement for GitHub code search
#!/usr/bin/env bash
# rg-gh, a replacement for GitHub code search.
# --------------------------------------------
# Uses ripgrep (rg) to search, skim (sk) and bat to select a result,
# then prints a link to it on GitHub's website.
#
# To install the necessary dependencies:
# cargo install ripgrep -F pcre2
# cargo install skim
# cargo install bat
#
query="$1"
[[ -z "$query" ]] && { echo "Usage: $(basename "$0") PATTERN"; exit 1; }
# Run ripgrep, pipe to skim for selection
result=$(rg --line-number --column --no-heading --color=always "$query" |
sk --ansi --delimiter ':' \
--preview 'bat --style=numbers --color=always --highlight-line {2} {1}' \
--preview-window 'up:+{2}-/2')
# Exit if no selection
[[ -z "$result" ]] && exit 0
# Returns the URL for the first GitHub hosted remote for the current repo
function github_remote_url {
for remote in $(git remote); do
local url=$(git remote get-url "$remote")
if [[ "$url" == *"github.com"* ]]; then
echo "$url"
return
fi
done
}
# Given a "path:line", finds a github remote and generates a link to the location there
function mk_github_link {
local IFS=':'
local args=($1)
local url=$(github_remote_url | sed -E 's#(git@github\.com:|https://github\.com/)(.*)\.git#https://github.com/\2#')
branch=$(git rev-parse --abbrev-ref HEAD)
echo "${url}/blob/${branch}/${args[0]}#L${args[1]}"
}
mk_github_link "$result"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment