Sourcegraph search query language

This page provides a visual breakdown of our Search Query Language and a handful of examples to get you started. It is complementary to our syntax reference and illustrates syntax using railroad diagrams instead of tables.

How to read railroad diagrams. Follow the lines in these railroad diagrams from left to right to see how pieces of syntax combine. When a line splits it means there are multiple options available. When it is possible to repeat a previous syntax, you'll see a line lead into a box that looks like this:

...

Basic query

search pattern
parameter
...

At a basic level, a query consists of search patterns and parameters. Typical queries contain one or more space-separated search patterns that describe what to search, and parameters refine searches by filtering results or changing search behavior.

Example: repo:github.com/sourcegraph/sourcegraph file:schema.graphql The result

Expression

basic query
AND
OR
basic query
...

Build query expressions by combining basic queries and operators like AND or OR. Group expressions with parentheses to build more complex expressions. If there are no balanced parentheses, AND operators bind tighter, so foo or bar and baz means foo or (bar and baz). You may also use lowercase and or or.

Example: repo:github.com/sourcegraph/sourcegraph rtr AND newRouter

Search pattern

string
quoted string

A pattern to search. By default the pattern is searched literally. The kind of search may be toggled to change how a pattern matches:

  • Perform a [regular expression search](queries.md#regular-expression-search). We support [RE2 syntax](https://golang.org/s/re2syntax). Quoting patterns performs a literal search.
    Example: foo.*bar.*baz"foo bar"
  • Perform a structural search. See our [dedicated documentation](queries.md#structural-search) to learn more about structural sexarch.
    Example: fmt.Sprintf(":[format]", :[args])

Parameter

repo
file
content
language
type
case
fork
archived
repogroup
repo has file
repo has commit after
count
timeout
visibility
pattern type

Search parameters allow you to filter search results or modify search behavior.

Repo

     
   
NOT   whitespace
repo:
r:
regular expression
 
@revision
whitespace rev:revision

Search repositories that match the regular expression. A - before repo excludes the repository. By default the repository will be searched at the HEAD commit of the default branch. You can optionally change the revision.

Example: repo:gorilla/mux testroute -repo:gorilla/mux testroute

Revision

branch name
commit hash
git tag
 
:revision
 
...

Search a repository at a given revision. For example, a branch name, commit hash, or git tag.

Example: repo:^github\.com/gorilla/mux$@948bec34 testroute or repo:^github\.com/gorilla/mux$ rev:v1.8.0 testroute

You can search multiple revisions by separating the revisions with :. Specify HEAD for the default branch.

Example: repo:^github\.com/gorilla/[email protected]:v1.4.0 testing.T or repo:^github\.com/gorilla/mux$ rev:v1.7.4:v1.4.0 testing.T

File

     
   
NOT   whitespace
file:
f:
regular expression

Search files whose full path matches the regular expression. A - before file excludes the file from being searched.

Example: file:\.js$ httptest file:\.js$ -file:test http

Language

language:
lang:
l:

Only search files in the specified programming language, like typescript or python.

Example: lang:typescript encoding

Content

     
   
NOT   whitespace
content: quoted string

Set the search pattern to search using a dedicated parameter. Useful, for example, when searching literally for a string like repo:my-repo that may conflict with the syntax of parameters in this Sourcegraph language.

Example: repo:sourcegraph content:"repo:sourcegraph"

Type

type:
symbol
repo
path
file
commit
diff
commit parameter

Set whether the search pattern should perform a search of a certain type. Notable search types are symbol, commit, and diff searches.

Example: type:symbol path type:commit author:nick

Case

case:
yes
no

Set whether the search pattern should be treated case-sensitively. This is synonymous with the toggle button.

Example: OPEN_FILE case:yes

Fork

fork:
yes
no
only

Set to yes if repository forks should be included or only if only forks should be searched. Respository forks are excluded by default.

Example: fork:yes repo:sourcegraph

Archived

archived:
yes
no
only

Set to yes if archived repositories should be included or only if only archives should be searched. Archived repositories are excluded by default.

Example: archived:only repo:sourcegraph

Repo group

repogroup:
g:

Only include results from the named group of repositories (defined by the server admin). Same as using repo that matches all of the group’s repositories. Use repo unless you know that the group exists.

Example: repogroup:go-gh-100 helm  – searches the top 100 Go repositories on GitHub, ranked by stars.

Repo has file

     
   
NOT   whitespace
repohasfile:
regular expression

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.

Example: repohasfile:\.py file:Dockerfile$ pip

Repo has commit after

repohascommitafter:
quoted string

Filter out stale repositories that don’t contain commits past the specified time frame. This parameter is experimental.

Example: -repohasfile:Dockerfile docker

Count

count:
number

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.

Example: count:1000 function

Timeout

timeout:
time value

Set a search timeout. The time value is a string like 10s or 100ms, which is parsed by the Go time package's ParseDuration. 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.

Example: timeout:15s count:10000 func  – sets a longer timeout for a search that contains a lot of results.

Visibility

visibility:
any
public
private

Filter results to only public or private repositories. The default is to include both private and public repositories.

Example: type:repo visibility:public

Pattern type

patterntype:
literal
regexp
structural

Set whether the pattern should run a literal search, regular expression search, or a structural search pattern. This parameter is available as a commannd-line and accessibility option, and synonymous with the visual search pattern toggles. in search pattern.

Regular expression

string
quoted string

A string that is interpreted as a RE2 regular expression.

String

string

An unquoted string is any contiguous sequence of characters not containing whitespace.

Quoted string

"any string"
'any string'

Any string, including whitespace, may be quoted with single ' or double " quotes. Quotes can be escaped with \.

Commit parameter

author
before
after
message
...

Set parameters that apply only to commit and diff searches.

Author

author: regular expression

Include commits or diffs that are authored by the user.

Before

before:
until:
quoted string

Include results which have a commit date before the specified time frame.

Example: before:"last thursday" before:"november 1 2019"

After

after:
since:
quoted string

Include results which have a commit date before the specified time frame.

Example: after:"6 weeks ago" after:"november 1 2019"

Message

message:
msg:
m:
quoted string

Include results which have commit messages containing the string.

Example: type:commit message:"testing"

Whitespace

space
...

Attribution: The railroad diagrams use assets generated by the H2 Database Engine project and licensed under MPL 2.0.