Repository navigation
Description
Describe the feature or problem you’d like to solve
I use gists for a lot of one-offs: showing people examples of something, storing my own https://play.rust-lang.org gists (so you can edit them), and so much more. The number is untenable, but GitHub offers no way to search them while limiting to some scope e.g., user. I would like a way to search them, and I think others might as well (hence not going straight to an extension).
Proposed solution
Add gh gist search with options to search all (default), --public, or --secret (mirroring gh gist list), and to "grep" just the description, file names, or file contents. A GraphQL query will be built to pull only what is needed, but search will page through all gists.
Examples:
gh gist search 'foo' # find all gists with "foo" in the description
gh gist search 'foo?' --public --filename # find all public gists with "fo" or "foo" in the description or filenames
gh gist search 'foo|bar' --secret --filename --code # find all secret gists with "foo" or "bar" in the description, filenames, or file contentHere I use --filename and --code to mirror some other command parameters in gh already. I think we should support regex from the start using Go's package (not great - no look-arounds, IIRC - but decent) so people aren't hampered when searching for code and don't request it later, creating a possible compatibility issue down the road.
Because the web site already has a search, I'm initially thinking this is scoped to a user. By default, it's effectively @me but you could pass --author {name} to scope to a different author's gists. IMO, we shouldn't error if --secret is specified since we tightly couple the code but could. The service won't return any results.
As for rendered output, this gets trickier. Ideally, gist search would show highlights of the search pattern; however, with the current table format of gist list we can't show multiple file names and certainly not content. Showing multiple file/names for possibly multiple gists would be hard to read. We could mimic the output of gh search code but add another level like:
{gist ID} {gist description}
{filename}
{first or all found search pattern with a line or two above and below for context}
Of course, we'd have to handle highlighting the matches and context lines. Not hard, but starts to bloat the CLI even more.
Instead, we could just list the gists that contain the pattern found in whatever options they specify. They could use existing gist commands to view more. At least in my use cases, I generally know what I'm looking for - and always put a decent description in to help find things - to showing the gist ID and description would satisfy my use case. Curious about others'.
Alternative
Alternatively, we could add a gist subcommand to gh search but I know from experience in the code that all the plumbing for search is very different from what this command would do, so it may not be worth it. At the very least, it would completely bifurcate the code path.
Additional context
Currently, gh gist list | grep 'something' is about as close as you can get and that will only search the view fields you return including the ID (repo name, which is a GUID; and useless to search), the description, and other fields that are useless to search.
Activity
Add `gist search` command
andyfeller commentedon Oct 8, 2024
As always, I appreciate you taking the time to actively solve the challenges you face, @heaths! ❤
I find myself somewhat torn given the technical limitations involved. As you stated, this isn't like
gh searchcommandset that leverages GitHub's search API. In fact, GitHub doesn't officially have an API for search gists though the GitHub gist UI allows various search qualifiers for interactive usage. In actuality, this is less "search" and more "filter". 🤔All of that said, I understand the value it presents; just wondering how not to mislead people if accepted.
Would someone expect this to include gists beyond their own?
gh gistis built around GraphQL queries, which only show the authenticated user's gists. If we want to get gists from other users, we would need to leverage REST API endpoints:Is it important that this is a new command versus new functionality within
gh gist list?Again, I worry that this isn't truly searching and would be confusing in context to other commands.
heaths commentedon Oct 8, 2024
I never realized there were filters: the web UI should show a link like it does when you're focused in the search on the main site.
That said, the goal here is only to search your own as the deception says, and as the existing gist list command says. So what confusion might there be given the command help text? I could elaborate in the Long text that it's fetching all gists to search locally if that helps, but commands don't usually go into implementation details.
I'd love to change the page handling though. The limit is used for the page size as well as max number of results. That doesn't utilize the benefits of paging. I made some changes a few years ago to use channels to fetch smaller pages and process asynchronously - even better for this scenario - and would like to do that here as well if you're all good with it. We'd keep limit as the max still for compatibility.
jtmcg commentedon Oct 8, 2024
Hey @heaths, thanks for the detailed submission! All the thoughts and context were really helpful for us to dig into this 🙌
There are two proposals that I see in the issue and the responses, so I'm going to separate them out so we can discuss them individually.
Proposal 1: Add
gh gist searchsubcommandUpon looking into the existing CLI patterns and a bit of the historical decision making around commands/subcommands, I think this is ultimately what
gh gist listis designed to do. It sounds like that subcommand has some feature gaps that aren't meeting your needs that we could address.But what about the existing
--searchflag in commands likegh pr list?This is an entirely different query and leverages
gh search. There are limitations to this (see below).Proposal 2: Add
gistsubcommand togh searchWhile I think this is probably the correct choice to make for the full "grep" like functionality you've requested, the
gh searchcommand is built on top of github's search apis. Currently, there's notype:gistin search, so we can't support it in the CLI. If the gist-owning team builds out search functionality fortype:gist, then it should be fairly straightforward to bring that functionality natively to the CLI. I'll reach out to them and let them know about this as a feature request.Summary
Overall, I think @andyfeller asked the right question: Is it important that this is a new command versus new functionality within
gh gist list?IMO, we should expand
gh gist listto encompass your needs rather than creating a new command. To do so, could you enumerate the exact functionality you'd like to see, @heaths, perhaps with respect to the following:Ability to search for gists that aren't yours(this is an api limitation, so we can't currently do this without api support)What did I miss?
For reference, here's the schema for
gists, which I got from our API explorer in the docs. We can use the schema to understand better what we can present and filter on to scope the design to what's available would be helpful.gist graphql schema
heaths commentedon Oct 8, 2024
I'm fine with making this some options on
gh gist list, but I'd want it to be able to fetchtextfrom the GraphQL schema (which I've used a lot over the years) conditionally because they can be large payloads. That's more bandwidth on you and consumers.But if we wanted to support filters as @andyfeller called out, that's different. I could add the functionality certainly, but grep'ing over results is local only since, as I already knew, there's no search API. Thus, the most recent commit I just pushed improves that loop a bit. Still, I think fetching and processing should be in separate go routines as I've helped do in other commands I've added or improved on over the years. That could be done in a separate PR or this one.
jtmcg commentedon Oct 8, 2024
For sure, but I think you're a little early on your PR, as we haven't agreed upon an approach to this, yet.
As I stated above I don't think we want to add a
gh gist searchsubcommand and should instead modifygh gist list. I've taken deeper look at the PR to try to understand exactly what you're asking for and how we can solve the problem leveraging thegh gist listsubcommand. It should be relatively easy to change what you've done to flags, but here's a rough outline of the design I imagine:Added to the
gh gist listsubcommand:--filename=<filename>flag that allows filtering on a filename--include-contentboolean flag that toggles whether a gist'stextis fetched--filter=<REGEX>flag that checks ALL returned fields on gists to match the supplied regex (I chose--filteras the name because--searchdoes something different as described in my comment above. I'd prefer to leave the--searchflag name available in case we get API support for searching gists).examples:
I think you can achieve your desired behavior this way. If not, what did I miss and how can we improve it?
heaths commentedon Oct 8, 2024
I'm fine with that. I did the PR earlier because I had time and wanted to this weekend. I can refactor, as I think you'll find my many PRs in the past have been. 😉
Most of the supporting code would still work. It's mostly just a matter of refactoring the command-related code.
Still, in a separate PR (since you squash merge, and I'm a firm believer in focused PRs that can be backed out if needed), I think I should better utilize go routines to convert (your GraphQL response structs get deep-copied to older REST response structs) and filter (running regexes isn't exactly cheap) separately from fetches. No doubt that is still faster then network IO, but why not parallelize them? Plenty other commands in the CLI already do that.
jtmcg commentedon Oct 8, 2024
Sweet! Sounds great. I'll keep an eye out for the PR updates 🙌
heaths commentedon Oct 9, 2024
I won't be able to filter the filename - at least, not with GraphQL or REST API support. I checked both and there's no way to pass a server-processed filter. I could, of course, support that but if we're already going to search descriptions, file names, and optionally content, I see no point A regex pattern could just as easily be a simple string that is a filename e.g., "main.rs", which will still match "main.rs", loosely speaking, but also "mainers". Seems like a very low risk, though, and people could always use "main.rs" instead.
Refactor filtering into existing `gist list`
Add `gist search` command
Refactor filtering into existing `gist list`
Add `gist search` command
Refactor filtering into existing `gist list`
heaths commentedon Oct 15, 2024
Thanks both for pushing the PR through. I also commented on an existing bug about gist searching: https://github.com/orgs/community/discussions/38820#discussioncomment-10953321
At the very least, it would be nice to see a link to the qualifier help somewhere obvious. I spend hours a day on github.com (and docs., gists., etc.) nearly every day and have poured through your (mostly API) docs countless hours and, to be honest, I've never ran across those qualifiers.
mattheworiordan commentedon Apr 15, 2026
Hit the same wall. 500 gists over the years and no way to find anything. GitHub search doesn't index gists, and
gh gist list --include-content --filteris ~30s on my account, because it's serial API calls.I scratched it: https://github.com/mattheworiordan/gistgrep -> Local mirror, incremental sync, fzf TUI with ranked previews, and on-device AI summaries via Apple Intelligence.
Search is now sub-100ms once synced
brew install mattheworiordan/tap/gistgrep. macOS only right now (the summary bit uses FoundationModels). Happy to add a Linux port via Ollama if there's interest, please do raise an issue. I hope my little project helps!