Created
January 21, 2026 22:44
-
-
Save shayelkin/5329b1f039bbcb73bf6c45d258c370fa to your computer and use it in GitHub Desktop.
A CLI replacement for GitHub code search.
This file contains hidden or 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
| #!/bin/bash | |
| # A CLI replacement for GitHub code search. | |
| # Requires: ripgrep, skim (sk), and bat. | |
| github_remote_url=$(gh repo view --json url --jq '.url') | |
| if [[ -z "$github_remote_url" ]]; then | |
| echo "error: no github url for current repository." | |
| exit 1 | |
| fi | |
| query="$1" | |
| if [[ -z "$query" ]]; then | |
| echo "Usage: $(basename "$0") PATTERN" | |
| exit 1 | |
| fi | |
| 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 | |
| if [[ -z "$result" ]]; then | |
| exit 0 | |
| fi | |
| IFS=':' result=($result) | |
| head_ref=$(git rev-parse --abbrev-ref HEAD) | |
| prefix_path=$(git rev-parse --show-prefix) | |
| link="${github_remote_url}/blob/${branch}/${prefix_path}${result[0]}#L${result[1]}" | |
| # Markdown output | |
| echo "[${result[0]} line ${result[1]}]($link)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment