Search Query Syntax
This page describes the query syntax for Code Search.
A typical search consists of two parts:
- A search pattern containing the terms you want to search, for example
println
- Search filters that scope the search to certain repositories, languages, etc., for example
lang:java
For a graphical view of Sourcegraph's query syntax, see the search language reference.
Search patterns
This section documents the search pattern syntax in Sourcegraph. To match file content, you need to specify a search pattern. Search patterns are optional when searching commits, filenames, or repository names.
Keyword search (default)
Keyword search matches individual terms anywhere in the document or the filename. Use "..."
to match phrases exactly. Specify regular expressions inside /.../
.
Search pattern syntax | Description |
---|---|
foo bar | Match documents containing both foo and bar anywhere in the document. |
"foo bar" | Match the string foo bar exactly. The space between the terms is interpreted literally. The quotes are not matched. |
"foo \"bar\"" | Match the string foo "bar" exactly. |
/foo.*bar/ | Match the regular expression foo.*bar . We support RE2 syntax. |
foo OR bar | Match documents containing foo or bar anywhere in the document. |
Aa
button to change).keyword
replaced standard
as the new default pattern type.Regular expression search
Clicking the (.*) toggle interprets all search patterns as regular expressions.
/.../
to perform regular expression searches. Use this mode if you prefer writing out regular expressions without enclosing them. In this mode spaces between patterns mean match anything. Patterns inside quotes mean match exactly.Search Syntax | Description |
---|---|
foo bar | Search for the regexp foo(.*?)bar . Spaces between non-whitespace strings becomes .*? to create a fuzzy search |
foo\ bar or /foo bar/ | Search for the regexp foo bar . The \ escapes the space and treats the space as part of the pattern. Use /.../ to avoid escaping spaces |
foo\nbar | Perform a multiline regexp search. \n is interpreted as a newline |
"foo bar" | Match the string literal "foo bar" . Quotes interpret strings exactly, except that special characters like " and \ may be escaped, and whitespace escape sequences like \n are interpreted normally |
Aa
button to change).Filters (all searches)
The following filters can be used on all searches (using RE2 syntax any place a regex is accepted):
Filters | Description | Examples |
---|---|---|
repo:regexp-pattern repo:regexp-pattern@rev repo:regexp-pattern rev:rev alias: r | Include results from repos whose path matches a specified regexp-pattern. The repo path is github.com/myteam/abc or code.example.com/xyz depending on your repo host. If the regexp ends in @rev , search that revision instead of the default main branch. repo:regexp-pattern@rev is equivalent to repo:regexp-pattern rev:rev | repo:gorilla/mux testroute repo:^github\.com/sourcegraph/sourcegraph$@v3.14.0 mux |
-repo:regexp-pattern alias: -r | Exclude results from repositories whose path matches the regexp | repo:alice/ -repo:old-repo |
rev:revision-pattern alias: revision | Search a revision instead of the default branch. rev: can only be used in conjunction with repo: and may not be used more than once. See our revision syntax documentation to learn more | repo:sourcegraph/sourcegraph rev:v3.14.0 mux |
file:regexp-pattern alias: f | Only include results in files whose full path matches the regexp | file:\.js$ httptest file:internal/ httptest |
-file:regexp-pattern alias: -f | Exclude results from files whose full path matches the regexp | file:\.js$ -file:test http |
content:"pattern" | Set the search pattern with a dedicated parameter. Useful when searching literally for a string that may conflict with the search pattern syntax. In between the quotes, the \ character will need to be escaped (\\ to evaluate for \ ) | repo:sourcegraph content:"repo:sourcegraph" |
-content:"pattern" | Exclude results from files whose content matches the pattern. | file:Dockerfile alpine -content:alpine:latest |
select:result-type select:repo select:commit.diff.added select:commit.diff.removed select:file select:content select:symbol.symbol-type select:file.owners (Experimental) | Shows only query results for a given type. For example, select:repo displays only distinct repository paths from search results, and select:commit.diff.added shows only added code matching the search. See language definition for full list of possible values | fmt.Errorf select:repo |
language:language-name alias: lang, l | Only include results from files in the specified programming language | language:typescript encoding |
-language:language-name alias: -lang, -l | Exclude results from files in the specified programming language | -language:typescript encoding |
type:symbol | Perform a symbol search | type:symbol path |
case:yes | Perform a case sensitive query. Without this, everything is matched case insensitively | OPEN_FILE case:yes |
fork:yes, fork:only | Include results from repository forks or filter results to only repository forks. Results in repository forks are excluded by default | fork:yes repo:sourcegraph |
archived:yes, archived:only | The yes option, includes archived repositories. The only option, filters results to only archived repositories. Results in archived repositories are excluded by default | repo:sourcegraph/ archived:only |
repo:has.meta(...) | Experimental Conditionally search inside repositories only if they are associated with a specified metadata: 1. key-value pair, or 2. key with any value, or 3. key with no value See built-in predicates for more | 1. repo:has.meta(owning-team:security) 2. repo:has.meta(owning-team) 3. repo:has.meta(archived:) |
repo:has.path(...) | Conditionally search inside repositories only if they contain a file path matching the regular expression. See built-in predicates for more | repo:has.path(\.py) file:Dockerfile pip |
repo:has.topic(...) | Search only in repos repositories if they have the given GitHub or GitLab topic. See built-in predicates for more | repo:has.topic(code-search) rank |
repo:has.commit.after(...) | Filter out stale repositories that don't contain commits past the specified time frame. See built-in predicates for more | repo:has.commit.after(yesterday) repo:has.commit.after(june 25 2017) |
rev:at.time(...) | Search a repo or a branch at a specific point in time. | rev:at.time(1 year ago) rev:at.time(2021-01-01) |
file:has.content(...) | Conditionally search files only if they contain contents that match the provided regex pattern. See built-in predicates for more | file:has.content(Copyright) Sourcegraph |
file:has.owners(...) | Beta Conditionally search files only if they are owned by the given owner. Empty means any owner. See code ownership documentation for more | file:has.owner(alice@sourcegraph.com) Sourcegraph |
file:has.contributor(...) | Conditionally search files only if a file contributor's name or email matches the provided regex pattern. See built-in predicates for more | file:has.contributor(alice@sourcegraph.com) Sourcegraph |
count:N,< /> count:all | Retrieve N results. By default, Sourcegraph stops searching early and returns if it finds a full page of results. This is desirable for most interactive searches. To wait for all results, use count:all | count:1000 function count:all err |
timeout:go-duration-value | Customizes the timeout for searches. The value of the parameter is a string that can be parsed by the Go time package's ParseDuration (e.g. 10s, 100ms). By default, the timeout is set to 10 seconds, and the search will optimize for returning results as soon as possible. The timeout value cannot be set longer than 1 minute. When provided, the search is given the full timeout to complete | repo:^github.com/sourcegraph timeout:15s func count:10000 |
patterntype:keyword, patterntype:regexp | Configure your query to be interpreted as a keyword search, or a regular expression. Note: this filter is available as an accessibility option in addition to the visual toggles | test. patternType:keyword (open/close)file patternType:regexp |
visibility:any, visibility:public, visibility:private | Filter results to only public or private repositories. The default is to include both private and public repositories | type:repo visibility:public |
Multiple or combined repo: and file: filters are intersected. For example, repo:foo repo:bar
limits your search to repositories whose path contains both foo
and bar
(such as github.com/alice/foobar
). To include results from repositories whose path contains either foo
or bar
, use repo:foo|bar
.
rev:at.time(...)
filter is only supported for Sourcegraph versions 5.4 or more.Boolean operators
Use boolean operators to create more expressive searches.
Operator | Example |
---|---|
and , AND | conf.Get( and log15.Error( , conf.Get( and log15.Error( and after |
Returns results for files containing matches on the left and right side of the and
(set intersection).
Operator | Example |
---|---|
or , OR | conf.Get( or log15.Error( , conf.Get( or log15.Error( or after |
Returns file content matching either on the left or right side, or both (set union). The number of results reports the number of matches of both strings. Note the regex or operator |
may not work as expected with certain operators for example file:(internal/repos)|(internal/gitserver)
, to receive the expected results use subexpressions, (file:internal/repos or file:internal/gitserver)
Operator | Example |
---|---|
not , NOT | lang:go not file:main.go panic , panic NOT ever |
NOT
can be used in place of -
to negate filters, such as file
, content
, lang
, repohasfile
, and repo
. For
search patterns, NOT
excludes documents that contain the term after NOT
. For readability, you can also include the
AND
operator before a NOT
(i.e. panic NOT ever
is equivalent to panic AND NOT ever
).
OR
in your code use content
like this: content:"query with OR"
.Operator precedence and groups
Operators may be combined. and
expressions have higher precedence (bind tighter) than or
expressions so that a and b or c and d
means (a and b) or (c and d)
.
Expressions may be grouped with parentheses to change the default precedence and meaning. For example: a and (b or c) and d
.
Filter scope
When parentheses are absent, we use the convention that operators divide sequences of terms that should be grouped together. That is:
file:main.c char c or (int i and int j)
generally means (file:main.c char c) or (int i and int j)
To apply the scope of the file
filter to the entire subexpression, fully group the subexpression:
file:main.c (char c or (int i and int j))
.
Filters (diff and commit searches only)
The following filters are only used for commit diff and commit message searches, which show changes over time:
Filter | Description | Examples |
---|---|---|
repo:regexp-pattern@rev | Specifies which Git revisions to search for commits. See our repository revisions documentation to learn more about the revision syntax. | repo:vscode@*refs/heads/:^refs/heads/master type:diff task (unmerged commit diffs containing task ) |
type:diff type:commit | Specifies the type of search. By default, searches are executed on all code at a given point in time (a branch or a commit). Specify the type: if you want to search over changes to code or commit messages instead (diffs or commits). | type:diff func type:commit test |
author:name | Only include results from diffs or commits authored by the user. Regexps are supported. Note that they match the whole author string of the form Full Name <user@example.com> , so to include only authors from a specific domain, use author:example.com>$ . You can also use author:@SourcegraphUserName to search on a Sourcegraph user's list of verified emails.You can also search by committer:git-email . Note: there is a committer only when they are a different user than the author. | type:diff author:nick |
-author:name | Exclude results from diffs or commits authored by the user. Regexps are supported. Note that they match the whole author string of the form Full Name <user@example.com> , so to exclude authors from a specific domain, use author:example.com>$ . You can also use author:@SourcegraphUserName to search on a Sourcegraph user's list of verified emails.You can also search by committer:git-email . Note: there is a committer only when they are a different user than the author. | type:diff author:nick |
before:"string specifying time frame" | Only include results from diffs or commits which have a commit date before the specified time frame | before:"last thursday" before:"november 1 2019" |
after:"string specifying time frame" | Only include results from diffs or commits which have a commit date after the specified time frame | after:"6 weeks ago" after:"november 1 2019" |
message:"any string" | Only include results from diffs or commits which have commit messages containing the string | type:commit message:"testing" type:diff message:"testing" |
-message:"any string" | Exclude the results from diffs or commits which have commit messages containing the string | type:commit message:"testing" type:diff message:"testing" |
Repository Search
Repository Revisions
To search revisions other than the default branch, specify the revisions by either appending them to the
repo:
filter or by listing them separately with the rev:
filter. This means: repo:github.com/myteam/abc@<revisions>
is equivalent to repo:github.com/myteam/abc rev:<revisions>
.
The <revisions>
part refers to repository
revisions (branches, commit hashes, and tags) and may take on the following forms:
(All examples apply to @
as well as rev:
)
@branch
- a branch name@1735d48
- a commit hash@3.15
- a tag
You can separate revisions by a colon to search multiple revisions at the same time, @branch:1735d48:3.15
.
Per default, we match revisions to tags, branches, and commits. You can limit the search to branches or tags by adding
the prefix refs/tags
or refs/heads
. For example @refs/tags/3.18
will search the commit tagged
with 3.18
, but not a branch called 3.18
and vice versa for @refs/heads/3.18
.
Glob patterns allow you to search over a range of branches or tags. Prepend *
to mark a revision
as glob pattern and add the glob-pattern after it like this repo:<repo>@*<glob-pattern>
. For example:
@*refs/heads/*
- search across all branches@*refs/tags/*
- search across all tags
We automatically add a trailing /*
if it is missing from the glob pattern.
You can negate a glob pattern by prepending *!
, for example:
@*refs/heads/*:*!refs/heads/release* type:commit
- search commits on all branches except on those that start with "release"@*refs/tags/v3.*:*!refs/tags/v3.*-* context
- search all versions starting with3.
except release candidates, alpha and beta versions.
Repository names
A query with only repo:
filters returns a list of repositories with matching names.
- Example:
repo:docker repo:registry
matches repositories with names that contain bothdocker
andregistry
substrings - Example:
repo:docker OR repo:registry
matches repositories with names that contain eitherdocker
orregistry
substrings
Commit and Diff searches
Commit and diff searches act on sets of commits. A set is defined by a revision (branch, commit hash, or tag), and it contains all commits reachable from that revision. A commit is reachable from another commit if it can be reached by following the pointers to parent commits.
For commit and diff searches it is possible to exclude a set of commits by prepending a caret ^
. The caret acts as a set
difference. For example, repo:github.com/myteam/abc@main:^3.15 type:commit
will show all commits in main
minus the commits reachable from the commit tagged with 3.15
.
Filename Search
A query with type:path
restricts terms to matching filenames only (not file contents).
- Example:
type:path repo:/docker/ registry
Content Search
A query with type:file
restricts terms to matching file contents only (not filenames).