Query Syntax
Code Search uses the Zoekt query language for powerful code searching. This guide covers everything from basic searches to advanced query techniques.
Basics
Section titled “Basics”Everything is Regex
Section titled “Everything is Regex”All queries in Zoekt are parsed as regular expressions using Go’s regexp syntax. This means queries containing regex special characters must escape them to match literally.
For example:
foo.barmatchesfooxbar(.matches any character)foo\.baror"foo.bar"matches literallyfoo.bar
Expressions
Section titled “Expressions”A query consists of one or more expressions separated by spaces. Each expression is treated as a pattern to match.
| Query | Meaning |
|---|---|
foo | Match the regex /foo/ |
foo bar | Match both /foo/ AND /bar/ in the same file |
"foo bar" | Match the literal string foo bar (space included) |
foo\ bar | Same as above (escaped space) |
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.
foo bar bazReturns files containing ALL of: /foo/, /bar/, and /baz/.
OR Operator
Section titled “OR Operator”Use or to combine expressions disjunctively:
foo or barReturns files containing /foo/ OR /bar/ (or both).
Grouping with Parentheses
Section titled “Grouping with Parentheses”Group expressions to create complex logic:
foo (bar or baz)Returns files with /foo/ AND (either /bar/ OR /baz/).
Groupings can be nested:
(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 | Alias | Description | Example |
|---|---|---|---|
file: | f: | Match file names only | file:README |
content: | c: | Match file contents only | content:TODO |
repo: | r: | Filter by repository name | repo:linux |
branch: | b: | Filter by branch name | branch:main |
lang: | l: | 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.
-f:test assertMatch /assert/ but exclude files with “test” in their name.
Repository Filters
Section titled “Repository Filters”| Field | Values | Description | Example |
|---|---|---|---|
archived: | yes, no | Filter archived repos | archived:no |
fork: | yes, no | Filter forked repos | fork:no |
public: | yes, no | Filter public/private repos | public:yes |
archived:no fork:no readmeSearch for “readme” in non-archived, non-forked repositories.
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.
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”(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:
(exec|query).*\+.*\$ lang:goDead 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 in non-archived repos:
type:filename README archived:noCase-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 | Alias | Values | Description |
|---|---|---|---|
file: | f: | text/regex | File name filter |
content: | c: | text/regex | Content only filter |
repo: | r: | text/regex | Repository filter |
branch: | b: | text/regex | Branch filter |
lang: | l: | language name | Language filter |
sym: | - | text/regex | Symbol definitions |
case: | - | yes/no/auto | Case sensitivity |
type: | t: | filematch/filename/repo | Result type |
archived: | a: | yes/no | Archived repos |
fork: | - | yes/no | Forked repos |
public: | - | yes/no | Public repos |
- Start Simple: Begin with basic patterns and add filters as needed
- Use Quotes: When searching for phrases or special characters
- Leverage Regex: For complex pattern matching
- Filter Early: Add
repo:orlang:to narrow results quickly - Exclude Noise: Use
-file:testto skip test files - Check Case: Use
case:auto(default) for smart matching