Sourcegraph code search performs full-text searches and supports both regular expression and exact queries. By default, Sourcegraph searches across all your repositories. Our search query syntax allows for advanced queries, such as searching over any branch or commit, narrowing searches by programming language or file pattern, and more.
See the query syntax and query reference documentation for a comprehensive overview of supported syntax.
Sourcegraph supports advanced code search for specifically matching patterns inside code structures, like function parameters and loop bodies.
See the structural search documentation for a detailed explanation of this search mode.
Search over commit diffs using type:diff
to see how your codebase has changed over time. This is often used to find changes to particular functions, classes, or areas of the codebase when debugging.
You can also search within commit diffs on multiple branches by specifying the branches in a repo:
field after the @
sign. After the @
, separate Git refs with :
, specify Git ref globs by prefixing them with *
, and exclude commits reachable from a ref by prefixing it with ^
. Diff searches can be further narrowed down with parameters that filter by author and time.
See the query syntax documentation for a comprehensive list of supported parameters.
Searching over commit messages is supported in Sourcegraph by adding type:commit
to your search query. Separately, you can also use the message:"any string"
parameter to filter type:diff
searches for a given commit message. Commit message searches can narrowed down further with filters such as author and time.
See our query syntax documentation for a comprehensive list of supported parameters.
Searching for symbols makes it easier to find specific functions, variables, and more. Use the type:symbol
filter to search for symbol results. Symbol results also appear in typeahead suggestions, so you can jump directly to symbols by name. When on an indexed commit it uses Zoekt, otherwise it uses the symbols service
Saved searches let you save and describe search queries so you can easily monitor the results on an ongoing basis. You can create a saved search for anything, including diffs and commits across all branches of your repositories. Saved searches can be an early warning system for common problems in your code and a way to monitor best practices, the progress of refactors, etc.
See the saved searches documentation for instructions for setting up and configuring saved searches.
As you type a query, the menu below will contain suggestions based on the query. Use the keyboard or mouse to select a suggestion. For example, if your query is repo:foo file:\.js$ hello
, the suggestions will consist of the list of files that match your query.
You can also type in the partial name of a repository or filename to quickly jump to it. For example, typing in just foo
would show you a list of repositories (first) and files with names containing foo.
Search contexts help you search the code you care about on Sourcegraph. A search context represents a set of repositories at specific revisions on a Sourcegraph instance that will be targeted by search queries by default.
Every search on Sourcegraph uses a search context. Search contexts can be defined with the contexts selector shown in the search input, or entered directly in a search query.
Sourcegraph Cloud currently supports two pre-set search contexts:
- Your personal context,
context:@username
, which automatically includes all repositories you add to Sourcegraph Cloud. - The global context,
context:global
, which includes all repositories on Sourcegraph Cloud.
If no search context is specified, context:global
is used.
Private Sourcegraph instances support custom search contexts:
- Contexts owned by a user, such as
context:@username/context-name
, which can be private to the user or public to all users on the Sourcegraph instance. - Contexts at the global level, such as
context:example-context
, which can be private to site admins or public to all users on the Sourcegraph instance. - The global context,
context:global
, which includes all repositories on the Sourcegraph instance.
This feature is currently under active development for self-hosted Sourcegraph instances and is disabled by default.
Your site admin can enable search contexts on your private instance in global settings using the following:
"experimentalFeatures": {
"showSearchContext": true,
"showSearchContextManagement": true
}
NOTE: This feature is still in active development and must be enabled by a Sourcegraph site admin in site configuration.
The most common branch to search is your default branch. To speed up this common operation Sourcegraph maintains an index of the source code on your default branch. Some organizations have other branches that are regularly searched. To speed up search for those branches Sourcegraph can be configured to index up to 64 branches per repository.
Your site admin can configure indexed branches in site configuration under the experimentalFeatures.search.index.branches
setting. For example:
"experimentalFeatures": {
"search.index.branches": {
"github.com/sourcegraph/sourcegraph": ["3.15", "develop"],
"github.com/sourcegraph/src-cli": ["next"]
}
}
Indexing multiple branches will add additional resource requirements to Sourcegraph (particularly memory). The indexer will deduplicate documents between branches. So the size of your index will grow in relation to the number of unique documents. Refer to our resource estimator to estimate whether additional resources are required.
NOTE: The default branch (
HEAD
) is always indexed.
NOTE: All revisions specified in version contexts are also indexed.
Note: While version contexts are located in the site configuration, search contexts are enabled through the global settings.
Reload the page after saving changes to see search contexts enabled.
If you currently use version contexts, you can automatically convert your existing version contexts to search contexts. We recommend migrating to search contexts for a more intuitive, powerful search experience and the latest improvements and updates.
See the search contexts documentation to learn how to use and create search contexts.