Skip to main content
A Sourcegraph query consists of two parts: a search pattern describing what to find, and filters that scope where to look. Both are optional—you can use filters alone to search commit history, file names, or repository names without a content pattern.
For example:

Search patterns

Keyword search (default)

In keyword mode (the default since Sourcegraph 5.4), terms are matched independently anywhere in the file. Use quotes for phrases and /.../ for inline regex:
Matching is case-insensitive by default. Toggle the Aa button in the search bar or add case:yes to your query to enable case sensitivity.
Enable the (.*) toggle or include patternType:regexp to treat the entire query as a regular expression. Spaces between unquoted terms become .*? (fuzzy match). Quoted terms are matched literally. Sourcegraph uses RE2 syntax. Lookaheads, lookbehinds, and backreferences are not supported. For post-filtering with PCRE features, pipe results through src CLI and jq. Structural search uses Comby patterns to match syntactic code structures. Use :[name] as a hole that matches any sequence of characters (including across lines):
Structural search ignores whitespace differences, making it reliable across codebases with different formatting conventions. Enable it with the [] toggle or patternType:structural.
Structural search requires an indexed commit. It is not available for unindexed branches.

Filters (all searches)

Filters narrow which repositories, files, or branches are searched. Multiple filters of the same type are intersected (AND logic) unless you use | within a single filter value for OR logic.

Repository

To search multiple revisions, separate them with ::
Use glob patterns to search across many branches:

File

Language

Aliases: language:, l:. Language detection is based on file extension by default. Enable the search-content-based-lang-detection feature flag for content-based detection.

Content

Use content:"pattern" to set the search pattern explicitly when the pattern text would conflict with filter syntax:
Use -content:"pattern" to exclude files containing a pattern:

Result type

Select

select: transforms results to return only a specific aspect: Example—find all repositories that use fmt.Errorf:

Repository predicates

Behavior modifiers

Filters (diff and commit searches only)

These filters apply only when type:diff or type:commit is set: Time values accept many formats: "yesterday", "5 days ago", "2 weeks ago", "november 1 2019", "2024-03-15", and Unix timestamps. Example—find all security-related changes on any branch in the last week:

Boolean operators

Combine expressions with AND, OR, and NOT (case-insensitive): AND binds more tightly than OR, so a AND b OR c AND d means (a AND b) OR (c AND d). Use parentheses to override precedence:
When no parentheses are present, filters bind to adjacent terms:
is interpreted as (file:main.c char c) or (int i and int j). To apply file:main.c to the entire expression, use:
To search for the literal words OR, AND, or NOT in code, use content:"query with OR".

Repository revisions

Append a revision to the repo: filter with @, or use the separate rev: filter:

Common query examples

Find hardcoded credentials or private keys across all repositories:
Find all TODOs outside of JSON, Markdown, and text files:
Find recent changes to authentication code across all branches:
Find all repositories that have a Dockerfile and use Python:
Find dependency changes in the last week:

Overview

Introduction to Code Search concepts and search types.

Features

Saved searches, search contexts, code monitoring, and more.