Search query syntax

This page describes search pattern syntax and keywords available for code search. A typical search pattern describes content or filenames to find across all repositories. At the most basic level, a search pattern can simply be a word like hello. See our search patterns documentation for detailed usage. Queries can also include keywords. For example, a typical search query will include a repo: keyword that filters search results for a specific repository. See our keywords documentation for more examples.

Search pattern syntax

This section documents the available search pattern syntax and interpretation in Sourcegraph. A search pattern is required to match file content. A search pattern is optional and may be omitted when searching for commits, filenames, or repository names.

Literal search (default)

Literal search interprets search patterns literally to simplify searching for words or punctuation.

Search pattern syntax Description
foo bar Match the string foo bar. Matching is ordered: match foo followed by bar. Matching is case-insensitive (toggle the button to change).
"foo bar" Match the string "foo bar". The quotes are matched literally.

As of version 3.9.0, by default, searches are interpreted literally instead of as regexp. To change the default search, site admins and users can change their instance and personal default by setting search.defaultPatternType to "literal" or "regexp".

Click the toggle to interpret search patterns as regexps. RE2 syntax is supported. In general, special characters may be escaped with \. Here is a list of valid syntax and behavior:

Search pattern syntax Description
foo bar Search for the regexp foo(.*?)bar. Spaces between non-whitespace strings is converted to .*? to create a fuzzy search. Matching is case insensitive (toggle the button to change).
foo\ bar or
/foo bar/
Search for the regexp foo bar. The \ escapes the space and treats the space as part of the pattern. Using the delimiter syntax / ... / avoids the need for escaping spaces.
foo\nbar Perform a multiline regexp search. \n is interpreted as a newline.
"foo bar" Match the string literal foo bar. Quoting strings when regexp is active means patterns are interpreted literally, except that special characters like " and \ may be escaped, and whitespace escape sequences like \n are interpreted normally.

Click the toggle to activate structural search. Structural search is a way to match more complex syntactic structures in code, and thus only applies to matching file contents. See the dedicated usage documentation for more details. Here is a brief overview of valid syntax:

Search pattern syntax Description
New(:[args]) Match the string New followed by balanced parentheses containing zero or more characters, including newlines. Matching is case-sensitive. Make the search language-aware by adding a lang: keyword.
"New(:[args])" or
Quoting the search pattern has the same meaning as New(:[args]), but avoids syntax errors that may conflict with keyword syntax. Special characters like " and \ may be escaped.

Note: It is not possible to perform case-insensitive matching with structural search.

Keywords (all searches)

The following keywords can be used on all searches (using RE2 syntax any place a regex is accepted):

Keyword Description Examples
repo:[email protected]
alias: r
Only include results from repositories whose path matches the regexp. A repository’s path is a string such as or that depends on your organization’s repository host. If the regexp ends in @rev syntax, that revision is searched instead of the default branch (usually master). repo:gorilla/mux testroute
repo:alice/[email protected]
alias: -r
Exclude results from repositories whose path matches the regexp. repo:alice/ -repo:old-repo
alias: g
Only include results from the named group of repositories (defined by the server admin). Same as using a repo: keyword that matches all of the group’s repositories. Use repo: unless you know that the group exists.
alias: f
Only include results in files whose full path matches the regexp. file:\.js$ httptest
file:internal/ httptest
alias: -f
Exclude results from files whose full path matches the regexp. file:\.js$ -file:test http
content:“pattern” Explicitly override the search pattern. Useful for explicitly delineating the pattern to search for if it clashes with other parts of the query. repo:sourcegraph "repo:sourcegraph"
alias: l
Only include results from files in the specified programming language. lang:typescript encoding
alias: -l
Exclude results from files in the specified programming language. -lang: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 exluded by default. fork:yes repo:sourcegraph
archived:yes, archived:only Include archived repositories or filter results to only archived repositories. Results in archived repositories are excluded by default. repo:sourcegraph/ archived:only
repohasfile:regexp-pattern Only include results from repositories that contain a matching file. This keyword is a pure filter, so it requires at least one other search term in the query. Note: this filter currently only works on text matches and file path matches. repohasfile:\.py file:Dockerfile pip
-repohasfile:regexp-pattern Exclude results from repositories that contain a matching file. This keyword is a pure filter, so it requires at least one other search term in the query. Note: this filter currently only works on text matches and file path matches. -repohasfile:Dockerfile docker
repohascommitafter:“string specifying time frame” (Experimental) Filter out stale repositories that don’t contain commits past the specified time frame. repohascommitafter:"last thursday"
repohascommitafter:"june 25 2017"
Retrieve at least 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, or to see results beyond the first page, use the count: keyword with a larger N. This can also be used to get deterministic results and result ordering (whose order isn’t dependent on the variable time it takes to perform the search). count:1000 function
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:^ timeout:15s func count:10000
patterntype:literal, patterntype:regexp, patterntype:structural Configure your query to be interpreted literally, as a regular expression, or a structural search pattern. Note: this keyword is available as an accessibility option in addition to the visual toggles. test. patternType:literal
(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
stable:yes Ensures a deterministic result order. Applies only to file contents. Limited to at max count:5000 results. Note this field should be removed if you’re using the pagination API, which already ensures deterministic results. func stable:yes count:10

Multiple or combined repo: and file: keywords are intersected. For example, repo:foo repo:bar limits your search to repositories whose path contains both foo and bar (such as To include results from repositories whose path contains either foo or bar, use repo:foo|bar.


Use 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). The number of results reports the number of files containing both strings.

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.

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.

Operator scope

Except for simple cases, search patterns bind tightest to scoped fields, like file:main.c. So, a combined query like file:main.c char c or (int i and int j) generally means (file:main.c char c) or (int i and int j)

Since we don’t yet support search subexpressions with different scopes, the above will raise an alert. If the intent is to apply the file scope to the entire pattern, group it like so: file:main.c (char c or (int i and int j))

Operator support

Operators are supported in regexp and structural search modes, but not literal search mode. How operators interpret search pattern syntax depends on kind of search (whether regexp or structural). Operators currently only apply to searches for file content. Thus, expressions like repo:npm/cli or repo:npm/npx are not currently supported.

Keywords (diff and commit searches only)

The following keywords are only used for commit diff and commit message searches, which show changes over time:

Keyword Description Examples
repo:[email protected] Specifies which Git refs (:-separated) to search for commits. Use *refs/heads/ to include all Git branches (and *refs/tags/ to include all Git tags). You can also prefix a Git ref name or pattern with ^ to exclude. For example, *refs/heads/:^refs/heads/master will match all commits that are not merged into master. repo:[email protected]*refs/heads/:^refs/heads/master type:diff task (unmerged commit diffs containing task)
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 <[email protected]>, so to include only authors from a specific domain, use>$.

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"

Repository revisions

The repo: filter accepts a repository pattern followed by @revs, like[email protected]. The @revs part refers to repository revisions (e.g., branches or commit hashes), and may take on the following example forms:

  • @feature-branch - a branch name
  • @1735d48 - a commit hash
  • @3.15 - a tag
  • @feature-branch:1735d48:3.15 - multiple colon-separated revisions of the above forms

Repository names

A query with only repo: filters returns a list of repositories with matching names.

Example: repo:docker repo:registry

A query with type:path restricts terms to matching filenames only (not file contents).

Example: type:path repo:/docker/ registry