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:
Regular expression search
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
Structural search uses Comby patterns to match syntactic code structures. Use:[name] as a hole that matches any sequence of characters (including across lines):
[] 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
::
File
Language
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
Usecontent:"pattern" to set the search pattern explicitly when the pattern text would conflict with filter syntax:
-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 whentype: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 withAND, 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:
(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 therepo: filter with @, or use the separate rev: filter:
Common query examples
Find hardcoded credentials or private keys across all repositories:Dockerfile and use Python:
Overview
Introduction to Code Search concepts and search types.
Features
Saved searches, search contexts, code monitoring, and more.