Skip to main content
This page covers the core features built on top of Code Search’s query engine. For the query syntax itself, see Query syntax.

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
1

Open Saved Searches

Click Tools > Saved Searches in the top navigation bar.
2

Create a new saved search

Click the New saved search button. Enter a description and your search query.
3

Include patternType

Every saved search query must include a patternType: filter. Use patternType:keyword, patternType:regexp, or patternType:literal.
type:diff after:"1 week ago" TODO patternType:keyword
4

Set visibility

New saved searches default to Secret (visible only to you). To share with your organization, use Transfer ownership to move the saved search to an org, then ask a site admin to make it public.
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

DescriptionQuery
Security-sensitive changes on all branches`type:diff repo:@*refs/heads/ after:“1 week ago” \b(authpasswordtokensecret)\b patternType:regexp`
New eslint-disable commentstype:diff after:"1 week ago" eslint-disable patternType:keyword
Dependency file changesfile:package\.json type:diff after:"1 week ago" patternType:keyword
Functions returning errors in Gofunc \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 adding repo: 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 type context: directly in the query:
context:@myteam/microservices DatabaseClient
To search across multiple contexts at once, use the OR operator:
(context:release-v1 OR context:release-v2) criticalBug
When a context is specified in the query, it overrides the dropdown selection.

Creating a search context

1

Navigate to Search Contexts

Go to User menu > Search contexts in the top navigation bar.
2

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.
3

Define repositories and revisions

Specify repositories by URL and pinned revisions. Use "HEAD" for the default branch:
[
  {
    "repository": "github.com/myorg/frontend",
    "revisions": ["HEAD"]
  },
  {
    "repository": "github.com/myorg/backend",
    "revisions": ["v2.1.0", "HEAD"]
  }
]
4

Test and save

Click Test configuration to validate the repository list, then Create search context.

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.
Site admins can set a default context for all users on the instance, which helps focus searches on the most relevant parts of the codebase during onboarding.
Use type: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.
type:diff func authenticate lang:go
Narrow by author, date, and branch:
type:diff repo:^github\.com/myorg/ after:"3 months ago" before:"1 month ago" author:alice password
Search only across unmerged branches:
repo:^github\.com/myorg/api$@*refs/heads/:^refs/heads/main type:diff after:"1 month ago" TODO
Use select:commit.diff.added to return only diffs where the pattern appears in added lines, or select:commit.diff.removed for removed lines:
repo:^github\.com/myorg/api$ type:diff TODO select:commit.diff.removed
Use type:commit to search commit messages. Combine with author:, after:, before:, and message: filters:
type:commit message:"fix auth" after:"2024-01-01"
Use message: within a type:diff query to find diffs associated with a specific commit message:
type:diff message:"security fix" lang:go
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.
type:symbol NewAuthHandler
Use select:symbol.<kind> to restrict to a specific symbol type:
type:symbol select:symbol.function parseConfig lang:go
type:symbol select:symbol.class AuthProvider lang:typescript
On indexed commits, symbol search uses Zoekt for high performance. On unindexed commits, the symbols service is used.

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
Monitors run on type:diff queries. When a new diff matches your query, the monitor triggers the configured action (email, Slack webhook, or generic webhook).
Code monitoring queries must use type:diff. Content searches and type:commit are not supported as monitor triggers.
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:
vendor/
*.generated.go
node_modules/
Files matching these patterns will not appear in search results for that repository.

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, combine src CLI output with jq:
re2_regex='func \w+\(\w+ \*\w+\) \w+'
pcre2_regex='func \w+\(\w+ \*(\w+)\) \1'

src search --json --stream -- "/$re2_regex/" \
  | jq 'select(.type == "content")
    | {content: .chunkMatches[].content} + del(.chunkMatches)
    | select(.content | test($ARGS.positional[0]))
  ' --args "$pcre2_regex"

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:
{
  "experimentalFeatures": {
    "boostRelevantRepositories": false
  }
}

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.

Build docs developers (and LLMs) love