Sourcegraph is a search tool that enables developers to search, explore and better understand all of their public and private code. It supercharges developer productivity and automates large-scale code changes.
In this article, I’ll explore some Sourcegraph search gems with you.
Here are 8 Sourcegraph search features you should know about!
1. Literal Expression Search
By default, all search queries on Sourcegraph are treated as literal patterns. If you know the exact string you’re looking for in the codebase, then all you need to do is paste it in the query box and search.
Sourcegraph interprets this search pattern literally (as the name defines it) to simplify searching for words or punctuation in your codebase.
With literal search, you can find function calls, error messages, comments, notes etc. For instance, if you want to search for how to use the browser navigator clipboard API, you can simply use:
navigator.clipboard
Search Query: https://sourcegraph.com/search?q=context:global+navigator.clipboard&patternType=literal
For more info, check out a detailed article about literal expression search on Sourcegraph.
2. Regular Expression Search
Sourcegraph’s query language is very powerful. One of its strengths is in the regular expression search it provides.
On the search box, you need to enable the regular expression mode by clicking on the *
sign on the right.
Once the regex mode is enabled, you can start using regular expressions for advanced search patterns. You can also add patterntype:regexp
to your queries and this will automatically enable regex mode.
Let's see a few different use cases of regex search as examples.
1. Find how the filesync readFile
and writeFile
methods are used in several projects.
fs\.(read|write)File patterntype:regexp
Search Query: https://sourcegraph.com/search?q=context:global+fs%5C.%28read%7Cwrite%29File&patternType=regexp
2. Find real-life use cases for Promise.all
, Promise.any
, Promise.race
, and Promise.allSettled
.
Promise.(any|race|allSettled) lang:JavaScript patterntype:regexp
Search Query: https://sourcegraph.com/search?q=context:global+Promise.%28any%7Crace%7CallSettled%29+lang:JavaScript&patternType=regexp
Check out a detailed article about the regular expression search on Sourcegraph.
3. Structural Search
Structural search gives you wings as a developer. With structural search, you can copy-paste blocks of code into the Sourcegraph search box, and accurate results will be returned.
First, enable the structural search mode by clicking on the [ ]
sign on the right:
Then you can go ahead and perform a structural search like so:
"exclude": [...] lang:json file:tsconfig.json patterntype:structural
Search Query:
https://sourcegraph.com/search?q=context:global+%22exclude%22:+%5B...%5D+lang:json+file:tsconfig.json&patternType=structural
To learn more, check out a detailed article about structural search on Sourcegraph.
4. Diff Search
Diff search allows you to search for code in file diffs. It makes it easy for a developer to search for code that used to exist and compare it with code that presently exists.
For example, the following search…
repo:^github\.com/sourcegraph/sourcegraph$ type:diff matrix
5. Commit Search
Commit search allows you to search for specific strings used in commit messages across your codebase.
Let’s search over commit messages like so:
6. Search Contexts
Search contexts are a set of repositories at specific revisions that search queries on Sourcegraph will target.
In other words, search contexts enable you to narrow, group and search only the code repositories you care about.
By default, Sourcegraph uses a global search context (namely, context:global) for all search queries, when no search context is specified by the actor (me, you, or a programmable robot) performing the search.
To learn more, check out a detailed article about creating, and using search contexts on Sourcegraph.
7. Symbol Search
Symbol search comes in handy when you want to directly search for function, class, variable and const definitions in your code search.
With Sourcegraph, you can use the “type:symbol” filter in your search query like so:
repo:^github\.com/sourcegraph/sourcegraph$ type:symbol Vector
The search query returns the symbol you searched for. All the symbols in that particular repository or file are grouped and shown on the left-hand side.
The grouped symbol list is searchable as shown in the gif below. You can search for whatever symbol you want.
Note: Sourcegraph has Symbol support for 75+ languages
8. Branch & Tag Search
Sourcegraph makes it possible to search for code in all of your branches and tags. Yes, all your branches, not only the main branch!
Branch Search
Use Case: Search the “add-panel-zoekt-cap” branch of the sourcegraph/sourcegraph repo for webhook.
Tag Search
Use Case: Search the “v3.32.0-rc.2” tag of the https://github.com/sourcegraph/sourcegraph repo for batch changes.
Conclusion
The benefits of code search are innumerable. The faster you can find code, the higher the development velocity for you and your teammates.
Sourcegraph Cloud is not the only option to add, explore private code and use search contexts. Sourcegraph can be installed in different ways to give you advanced search features for your private code.
You can run Sourcegraph locally or opt-in for us to set up a managed Sourcegraph instance for your team. You'll be able to use a self-hosted Sourcegraph instance for free (up to 10 users).
Have suggestions or questions? Leave a comment, or join our Community Slack Space where our team will be happy to answer any questions you may have about Sourcegraph.