About code search query structure
The search syntax in this article only applies to searching code with GitHub code search. Note that the syntax and qualifiers for searching for non-code content, such as issues, users, and discussions, is not the same as the syntax for code search. For more information on non-code search, see "About searching on GitHub"and"Searching on GitHub."
Search queries consist of search terms, comprising text you want to search for, and qualifiers, which narrow down the search.
A bare term with no qualifiers will match either the content of a file or the file's path.
For example, the following query:
http-push
The above query will match the filedocs/http-push.txt
,even if it doesn't contain the termhttp-push
.It will also match a file calledexample.txt
if it contains the termhttp-push
.
You can enter multiple terms separated by whitespace to search for documents that satisfy both terms.
For example, the following query:
sparse index
The search results would include all documents containing both the termssparse
andindex
,in any order. As examples, it would match a file containingSparseIndexVector
,a file with the phraseindex for sparse trees
,and even a file namedindex.txt
that contains the termsparse
.
Searching for multiple terms separated by whitespace is the equivalent to the searchhello AND world
.Other boolean operations, such ashello OR world
,are also supported. For more information about boolean operations, see "Using boolean operations."
Code search also supports searching for an exact string, including whitespace. For more information, see "Query for an exact match."
You can narrow your code search with specialized qualifiers, such asrepo:
,language:
andpath:
.For more information on the qualifiers you can use in code search, see "Using qualifiers."
You can also use regular expressions in your searches by surrounding the expression in slashes. For more information on using regular expressions, see "Using regular expressions."
Query for an exact match
To search for an exact string, including whitespace, you can surround the string in quotes. For example:
"sparse index"
You can also use quoted strings in qualifiers, for example:
path:git language: "protocol buffers"
Searching for quotes and backslashes
To search for code containing a quotation mark, you can escape the quotation mark using a backslash. For example, to find the exact stringname = "tensorflow"
,you can search:
"name = \" tensorflow\ ""
To search for code containing a backslash,\
,use a double backslash,\\
.
The two escape sequences\\
and\ "
can be used outside of quotes as well. No other escape sequences are recognized, though. A backslash that isn't followed by either"
or\
is included in the search, unchanged.
Additional escape sequences, such as\n
to match a newline character, are supported in regular expressions. See "Using regular expressions."
Using boolean operations
Code search supports boolean expressions. You can use the operatorsAND
,OR
,andNOT
to combine search terms.
By default, adjacent terms separated by whitespace are equivalent to using theAND
operator. For example, the search querysparse index
is the same assparse AND index
,meaning that the search results will include all documents containing both the termssparse
andindex
,in any order.
To search for documents containing either one term or the other, you can use theOR
operator. For example, the following query will match documents containing eithersparse
orindex
:
sparse OR index
To exclude files from your search results, you can use theNOT
operator. For example, to exclude files in the__testing__
directory, you can search:
"fatal error" NOT path:__testing__
You can use parentheses to express more complicated boolean expressions. For example:
(language:ruby OR language:python) AND NOT path: "/tests/"
Using qualifiers
You can use specialized keywords to qualify your search.
- Repository qualifier
- Organization and user qualifiers
- Language qualifier
- Path qualifier
- Symbol qualifier
- Content qualifier
- Is qualifier
Repository qualifier
To search within a repository, use therepo:
qualifier. You must provide the full repository name, including the owner. For example:
repo:github-linguist/linguist
To search within a set of repositories, you can combine multiplerepo:
qualifiers with the boolean operatorOR
.For example:
repo:github-linguist/linguist OR repo:tree-sitter/tree-sitter
Note:Code search does not currently support regular expressions or partial matching for repository names, so you will have to type the entire repository name (including the user prefix) for therepo:
qualifier to work.
Organization and user qualifiers
To search for files within an organization, use theorg:
qualifier. For example:
org:github
To search for files within a personal account, use theuser:
qualifier. For example:
user:octocat
Note:Code search does not currently support regular expressions or partial matching for organization or user names, so you will have to type the entire organization or user name for the qualifier to work.
Language qualifier
To narrow down to a specific languages, use thelanguage:
qualifier. For example:
language:ruby OR language:cpp OR language:csharp
For a complete list of supported language names, seelanguages.yamlingithub-linguist/linguist.If your preferred language is not on the list, you can open a pull request to add it.
Path qualifier
To search within file paths, use thepath:
qualifier. This will match files containing the term anywhere in their file path. For example, to find files containing the termunit_tests
in their path, use:
path:unit_tests
The above query will match bothsrc/unit_tests/my_test.py
andsrc/docs/unit_tests.md
since they both containunit_test
somewhere in their path.
To match only a specific filename (and not part of the path), you could use a regular expression:
path:/(^|\/)README\.md$/
Note that the.
in the filename is escaped, since.
has special meaning for regular expressions. For more information about using regular expressions, see "Using regular expressions."
You can also use some limited glob expressions in thepath:
qualifier.
For example, to search for files with the extensiontxt
,you can use:
path:*.txt
To search for JavaScript files within a `src` directory, you could use:
path:src/*.js
-
By default, glob expressions are not anchored to the start of the path, so the above expression would still match a path like
app/src/main.js
.But if you prefix the expression with/
,it will anchor to the start. For example:path:/src/*.js
-
Note that
*
doesn't match the/
character, so for the above example, all results will be direct descendants of thesrc
directory. To match within subdirectories, so that results include deeply nested files such as/src/app/testing/utils/example.js
,you can use**
.For example:path:/src/**/*.js
You can also use the?
global character. For example, to match the pathfile.aac
orfile.abc
,you can use:
path:*.a?c
To search for a filename which contains a special character like `*` or `?`, just use a quoted string:
path: "file?"
Glob expressions are disabled for quoted strings, so the above query will only match paths containing the literal stringfile?
.
Symbol qualifier
You can search for symbol definitions in code, such as function or class definitions, using thesymbol:
qualifier. Symbol search is based on parsing your code using the open sourceTree-sitterparser ecosystem, so no extra setup or build tool integration is required.
For example, to search for a symbol calledWithContext
:
language:go symbol:WithContext
In some languages, you can search for symbols using a prefix (e.g. a prefix of their class name). For example, for a methoddeleteRows
on a structMaint
,you could searchsymbol:Maint.deleteRows
if you are using Go, orsymbol:Maint::deleteRows
in Rust.
You can also use regular expressions with the symbol qualifier. For example, the following query would find conversions people have implemented in Rust for theString
type:
language:rust symbol:/^String::to_.*/
Note that this qualifier only searches for definitions and not references, and not all symbol types or languages are fully supported yet. Symbol extraction is supported for the following languages:
- Bash
- C
- C#
- C++
- CodeQL
- Elixir
- Go
- JSX
- Java
- JavaScript
- Lua
- PHP
- Protocol Buffers
- Python
- Ruby
- Rust
- Scala
- Starlark
- Swift
- Typescript
We are working on adding support for more languages. If you would like to help contribute to this effort, you can add support for your language in the open sourceTree-sitterparser ecosystem, upon which symbol search is based.
Content qualifier
By default, bare terms search both paths and file content. To restrict a search to strictly match the content of a file and not file paths, use thecontent:
qualifier. For example:
content:README.md
This query would only match files containing the termREADME.md
,rather than matching files namedREADME.md
.
Is qualifier
To filter based on repository properties, you can use theis:
qualifier.is:
supports the following values:
archived
:restricts the search to archived repositories.fork
:restricts the search to forked repositories.vendored
:restricts the search to content detected as vendored.generated
:restricts the search to content detected as generated.
For example:
path:/^MIT.txt$/ is:archived
Note that theis:
qualifier can be inverted with theNOT
operator. To search for non-archived repositories, you can search:
log4j NOT is:archived
To exclude forks from your results, you can search:
log4j NOT is:fork
Using regular expressions
Code search supports regular expressions to search for patterns in your code. You can use regular expressions in bare search terms as well as within many qualifiers, by surrounding the regex in slashes.
For example, to search for the regular expressionsparse.*index
,you would use:
/sparse.*index/
Note that you'll have to escape any forward slashes within the regular expression. For example, to search for files within theApp/src
directory, you would use:
/^App\/src\//
Inside a regular expression,\n
stands for a newline character,\t
stands for a tab, and\x{hhhh}
can be used to escape any Unicode character. This means you can use regular expressions to search for exact strings that contain characters that you can't type into the search bar.
Most common regular expressions features work in code search. However, "look-around" assertions are not supported.
Separating search terms
All parts of a search, such as search terms, exact strings, regular expressions, qualifiers, parentheses, and the boolean keywordsAND
,OR
,andNOT
,must be separated from one another with spaces. The one exception is that items inside parentheses,(
)
,don't need to be separated from the parentheses.
If your search contains multiple components that aren't separated by spaces, or other text that does not follow the rules listed above, code search will try to guess what you mean. It often falls back on treating that component of your query as the exact text to search for. For example, the following query:
printf( "hello world\n" );
Code search will give up on interpreting the parentheses and quotes as special characters and will instead search for files containing that exact code.
If code search guesses wrong, you can always get the search you wanted by using quotes and spaces to make the meaning clear.
Case sensitivity
Code search is case-insensitive. Searching forTrue
will include results foruppercaseTRUE
andlowercasetrue
.You cannot do case-sensitive searches. Regular expression searches (e.g. for[t][H][i][S]
) are also case-insensitive, and thus would returnThis
,THIS
andthis
in addition to any instances oftHiS
.