Code search tidbits: 4 underrated features

Rijnard van Tonder

You can do some pretty wild things with Sourcegraph that you won't find in any other code search tool today. Below are 4 short-and-sweet tidbits of underrated search features that go a bit extra.

Find repositories by description

Use repo:has.description(scientific computing) to find repositories related to topics like scientific computing. Repositories are ordered by number of stars.

search by repository description

Simply add a pattern like matrix multiplication to search inside repositories that match the description. Try other terms to find projects like game engines, react tutorials, or video editors.

Search over code +added or -removed

Sourcegraph can search over diffs. But did you know: searches can pick out only the lines that were +added or -removed? Use select:commit.diff.added or select:commit.diff.removed to search for added or removed library calls or TODOs in repositories ↗.

search by repository description

Conditionally search repositories

Add a search term like repo:contains(file:package.json content:eslint.*\^8\.13\.0) to search inside repositories only if  they contain a package.json file with a specific eslint version. For example, we can search for the rules field in .eslintrc files, but only if  the repository contains an eslint version of ^8.13.0 in package.json. See it in action with this query↗.

search by repository description

So you basically have "if" statements without needing to do anything too special. These are great for needle-in-a-haystack queries (so they sometimes run a bit longer) but are extremely powerful. Check out repo:has.path(...)↗ and repo:has.content(...)↗ for similar conditional search terms.

Curate groups of repos to search over

Create your own groups of repositories to search using search contexts. I use this to group the top 100 starred GitHub repositories by language. It's really handy to search for examples in a language that I'm new to, like finding how library calls are used.

search by repository description

Even if you know the language, you'll see code examples in popular and high quality repositories for that language. To create your own, just hit the context: drop-down and manage your contexts to create your own from there.

search by repository description

You can reuse others' public contexts, like the ones shown in the screenshot. These are contexts I defined to roughly track the top 100 starred repositories for many different languages. So to search over the top 100 C projects, just use my context:@r/c-100-gh to find examples. Similar for Zig, Rust, Elixir, and many others.

Help

Want to do something with code search that isn't quite working out? Head over to our Discord channel↗ and let us know.

Start using Sourcegraph on your own code

Sourcegraph makes it easy to read, write, and fix code—even in big, complex codebases.