Saved searches
Saved searches let you store and name any query so you can rerun it instantly and monitor its results over time. They are useful for:- Tracking known anti-patterns or security-sensitive code paths
- Monitoring refactor progress (e.g., how many call sites still use a deprecated API)
- Sharing useful queries across your team or organization
Creating a saved search
Create a new saved search
Click the New saved search button. Enter a description and your search query.
Include patternType
Every saved search query must include a
patternType: filter. Use patternType:keyword, patternType:regexp, or patternType:literal.You cannot create a saved search without a
patternType: filter in the query. Searches will be rejected at creation time if it is missing.Example saved searches
| Description | Query | |||
|---|---|---|---|---|
| Security-sensitive changes on all branches | `type:diff repo:@*refs/heads/ after:“1 week ago” \b(auth | password | token | secret)\b patternType:regexp` |
| New eslint-disable comments | type:diff after:"1 week ago" eslint-disable patternType:keyword | |||
| Dependency file changes | file:package\.json type:diff after:"1 week ago" patternType:keyword | |||
| Functions returning errors in Go | func \w+\(.*\) .*error lang:go patternType:regexp |
Search contexts
Search contexts let you define a named set of repositories and revisions to search by default. Instead of addingrepo: filters to every query, you select or reference a context.
Every query on Sourcegraph runs within a context. If no context is specified, context:global is used, which includes all indexed repositories.
Using a context
Select a context from the dropdown in the search bar, or typecontext: directly in the query:
OR operator:
Creating a search context
Create a new context
Click + Create search context. Set an owner (yourself or a global instance context), a name (max 32 characters, alphanumeric plus
., _, /, -), and visibility.Define repositories and revisions
Specify repositories by URL and pinned revisions. Use
"HEAD" for the default branch:Query-based contexts
Instead of a fixed list of repositories, you can define a context with a restricted search query. Allowed filters are:repo, rev, file, lang, case, fork, and visibility.
Enable this by setting experimentalFeatures.searchContextsQuery to true in global settings. A Create context button will then appear on the search results page.
Commit diff search
Usetype:diff to search the contents of commit diffs—what was added or removed across your repositories over time. This is the most reliable way to find when and where a specific change was introduced.
select:commit.diff.added to return only diffs where the pattern appears in added lines, or select:commit.diff.removed for removed lines:
Commit message search
Usetype:commit to search commit messages. Combine with author:, after:, before:, and message: filters:
message: within a type:diff query to find diffs associated with a specific commit message:
Symbol search
type:symbol searches for named symbols—functions, methods, classes, interfaces, variables, and more—rather than raw text. Symbol results appear in the typeahead as you type, letting you jump directly to a definition without running a full search.
select:symbol.<kind> to restrict to a specific symbol type:
Code monitoring
Code monitoring watches a search query and sends you an alert when new results appear. Use it to get notified about:- New occurrences of a banned function or pattern
- Security-relevant changes (e.g., changes touching authentication code)
- New TODOs or FIXMEs added to production code
type:diff queries. When a new diff matches your query, the monitor triggers the configured action (email, Slack webhook, or generic webhook).
To create a monitor, run a type:diff search in the search bar, then click Create a code monitor in the results toolbar. From there, configure the notification channel and schedule.
Language statistics
After running a search, click the Statistics tab on the results page to see a breakdown of programming languages represented in the results. This is useful for understanding what proportion of a codebase is written in each language, or for auditing the scope of a pattern across languages. Language statistics reflect the actual file contents detected across all matched results, not just file extensions.File and repository filtering
Exclude files from search indexing
Add a.sourcegraph/ignore file to the root of any repository to exclude files or directories from search results. The file uses glob patterns, one per line:
Compare changes across revisions
From a search results page, click the... menu next to the Contributors tab and select Compare to view a diff between two revisions. The file picker lets you select specific files or directories to focus on—useful when comparing branches with thousands of changed files.
Multi-branch indexing
By default, Sourcegraph indexes only the default branch of each repository for fast search. For branches that your team regularly searches (e.g., a long-lived release branch), a site admin can configure up to 64 branches per repository to be indexed. This significantly reduces search latency for those branches. Contact your site admin to configure multi-branch indexing through site configuration.RE2 regular expressions
Sourcegraph uses RE2 syntax for all regular expression searches. RE2 guarantees worst-case linear evaluation time, making it safe for production search at scale. RE2 does not support backreferences or lookaround assertions. If you need PCRE features for post-filtering, combinesrc CLI output with jq:
Personalized search ranking
On Enterprise instances, search results are ranked by relevance to your query and boosted for repositories you have recently contributed to. This helps surface the code most relevant to you when searching large codebases. Personalized ranking is enabled by default. To disable the contribution boost:Explore further
Overview
Introduction to Code Search, supported code hosts, and search types.
Query syntax
Complete filter, boolean operator, and pattern syntax reference.
Deep Search
Run exhaustive searches that go beyond standard index limits.
Stream API
Build integrations using the streaming search results API.