Query Syntax
Code Search supports a rich query language for searching code. This guide covers everything from basic searches to advanced query techniques.
Basics
Section titled “Basics”Plain Text Search
Section titled “Plain Text Search”By default, search queries match as exact substrings. Type what you’re looking for and it will match literally.
func main()Matches the exact string func main() in your code.
fmt.PrintlnMatches the exact string fmt.Println.
Query Operators
Section titled “Query Operators”When you use query operators (like repo:, lang:, file:, etc.), the query is parsed using Zoekt’s query language. In this mode, search terms are treated as regular expressions using Go’s regexp syntax.
lang:go func\s+main\(Uses regex to match func followed by whitespace and main( in Go files.
Expressions
Section titled “Expressions”A query with operators consists of one or more expressions separated by spaces. Each expression is treated as a pattern to match.
| Query | Meaning |
|---|---|
foo | Match the exact substring foo |
lang:go foo bar | Match both /foo/ AND /bar/ in Go files (regex mode) |
"foo bar" | Match the literal string foo bar (space included) |
Boolean Logic
Section titled “Boolean Logic”Implicit AND
Section titled “Implicit AND”By default, multiple expressions are combined with AND logic. A file must contain matches for all expressions to appear in results.
repo:myorg foo bar bazReturns files containing ALL of: /foo/, /bar/, and /baz/ in the myorg repository.
OR Operator
Section titled “OR Operator”Use or to combine expressions disjunctively:
repo:myorg foo or barReturns files containing /foo/ OR /bar/ (or both).
Grouping with Parentheses
Section titled “Grouping with Parentheses”Group expressions to create complex logic:
repo:myorg foo (bar or baz)Returns files with /foo/ AND (either /bar/ OR /baz/).
Groupings can be nested:
repo:myorg (foo or bar) (baz or qux)Negation
Section titled “Negation”Prefix an expression with - to exclude matches:
foo -barReturns files with /foo/ that do NOT contain /bar/.
Negation works with groups:
foo -(bar or baz)Excludes files with either /bar/ or /baz/.
foo -(bar baz)Excludes files with BOTH /bar/ and /baz/.
Field Expressions
Section titled “Field Expressions”Field prefixes restrict where a pattern matches. Without a field prefix, patterns match both file names and content.
Search Fields
Section titled “Search Fields”| Field | Description | Example |
|---|---|---|
file: | Match file names only | file:README |
content: | Match file contents only | content:TODO |
repo: | Filter by repository name | repo:linux |
branch: | Filter by branch name | branch:main |
lang: | Filter by programming language | lang:go |
sym: | Match symbol definitions (ctags) | sym:main |
Examples
Section titled “Examples”file:READMEMatches files with “README” in their name.
content:TODOMatches “TODO” only in file contents (not file names).
repo:myorg/api lang:go func.*HandlerSearch for func.*Handler in Go files within the myorg/api repository.
-file:test assertMatch /assert/ but exclude files with “test” in their name.
Case Sensitivity
Section titled “Case Sensitivity”The case: modifier controls case matching:
| Value | Behavior |
|---|---|
case:auto | Case-sensitive if pattern has uppercase (default) |
case:yes | Always case-sensitive |
case:no | Always case-insensitive |
FOO case:yesMatches only FOO, not foo or Foo.
TEST case:noMatches TEST, test, Test, etc.
Result Type
Section titled “Result Type”Control what kind of results are returned:
| Value | Description |
|---|---|
type:filematch | File content matches (default) |
type:filename | Only matching filenames |
type:repo | Only repository names |
type:repo configReturns repository names matching “config” instead of file matches.
Inline Regex
Section titled “Inline Regex”The regex: operator treats the following pattern as a regular expression, even without other query operators:
regex:func\s+main\(Matches func followed by whitespace and main( using regex.
Text Values
Section titled “Text Values”Field values can be plain text, quoted strings, or regex:
Plain Text
Section titled “Plain Text”repo:myprojectlang:typescriptQuoted Strings
Section titled “Quoted Strings”Use double quotes for values with spaces or special characters:
file:"my file.txt"content:"foo bar"repo:"my org/my project"Regular Expressions
Section titled “Regular Expressions”Use forward slashes for regex patterns:
content:/func\s+\w+\(/file:/.*\.test\.ts$/repo:/github\.com\/myorg\/.*/Escape Characters
Section titled “Escape Characters”Escape special characters with backslash:
content:"foo\"bar"file:my\ file.txtLanguage Support
Section titled “Language Support”The lang: field filters by programming language as detected by go-enry (based on linguist):
| Language | Also matches |
|---|---|
lang:go | Go |
lang:python | Python |
lang:javascript | JavaScript |
lang:typescript | TypeScript |
lang:java | Java |
lang:rust | Rust |
lang:c | C |
lang:cpp or lang:c++ | C++ |
| Language | Also matches |
|---|---|
lang:ruby | Ruby |
lang:php | PHP |
lang:swift | Swift |
lang:kotlin | Kotlin |
lang:scala | Scala |
lang:shell or lang:bash | Shell scripts |
lang:yaml | YAML |
lang:json | JSON |
Common Patterns
Section titled “Common Patterns”Find Function Definitions
Section titled “Find Function Definitions”lang:go func\s+\w+\(lang:python def\s+\w+\(lang:typescript function\s+\w+\(Find Import Statements
Section titled “Find Import Statements”lang:go ^import\slang:python ^(from|import)\slang:typescript ^import\sFind TODOs and FIXMEs
Section titled “Find TODOs and FIXMEs”regex:(TODO|FIXME|HACK|XXX):Find Configuration Files
Section titled “Find Configuration Files”file:(config|settings)\.(json|yaml|yml|toml)$Exclude Test Files
Section titled “Exclude Test Files”myfunction -file:test -file:spec -file:mockSearch Specific Repository
Section titled “Search Specific Repository”repo:myorg/myrepo lang:go error handlingAdvanced Examples
Section titled “Advanced Examples”API Endpoints
Section titled “API Endpoints”Search for HTTP handlers in Go:
lang:go func.*Handler repo:apiSecurity Audit
Section titled “Security Audit”Find potential SQL injection:
lang:go (exec|query).*\+.*\$Dead Code
Section titled “Dead Code”Find unused exports:
sym:deprecated -file:testDependency Check
Section titled “Dependency Check”Find package imports:
lang:go "github.com/pkg/errors"More Examples
Section titled “More Examples”Find error handling in Go, excluding tests:
lang:go (err != nil) -file:_test\.goSearch multiple repos for a pattern:
(repo:frontend or repo:backend) authFind README files:
type:filename READMECase-sensitive class name search:
lang:java class MyService case:yesFind files modified in feature branches:
branch:/feature-.*/ configQuick Reference
Section titled “Quick Reference”Operators
Section titled “Operators”| Operator | Example | Description |
|---|---|---|
| (space) | foo bar | AND (both required) |
or | foo or bar | OR (either match) |
- | -foo | NOT (exclude) |
() | (a or b) c | Grouping |
"" | "foo bar" | Literal string |
// | /foo.*/ | Regex pattern |
Fields Summary
Section titled “Fields Summary”| Field | Values | Description |
|---|---|---|
file: | text/regex | File name filter |
content: | text/regex | Content only filter |
repo: | text/regex | Repository filter |
branch: | text/regex | Branch filter |
lang: | language name | Language filter |
sym: | text/regex | Symbol definitions |
case: | yes/no/auto | Case sensitivity |
type: | filematch/filename/repo | Result type |
regex: | pattern | Inline regex mode |
- Start Simple: Begin with plain text and add filters as needed
- Use Quotes: When searching for phrases with spaces
- Add Filters: Use
repo:orlang:to narrow results quickly - Exclude Noise: Use
-file:testto skip test files - Use Regex Mode: Add
regex:prefix for complex pattern matching - Check Case: Use
case:auto(default) for smart matching