Code Search Capabilities
Learn and understand more about Sourcegraph's Code Search features and core functionality.
Powerful, flexible queries
Sourcegraph code search performs full-text searches and supports both regular expression and exact queries. By default, Sourcegraph searches across all your repositories. Our search query syntax allows for advanced queries, such as searching over any branch or commit, narrowing searches by programming language or file pattern, and more.
See the search query syntax documentation for a comprehensive overview of supported syntax.
Data freshness
Searches scoped to specific repositories are always up-to-date. Sourcegraph automatically fetches repository contents with any user action specific to the repository and makes new commits and branches available for searching and browsing immediately.
Unscoped search results over large repository sets may trail latest default branch revisions by some interval of time. This interval is a function of the number of repositories and the computational resources devoted to search indexing.
Commit diff search
Search over commit diffs using type:diff
to see how your codebase has changed over time. This is often used to find changes to particular functions, classes, or areas of the codebase when debugging.
You can also search within commit diffs on multiple branches by specifying the branches in a repo:
field after the @
sign. After the @
, separate Git refs with :
, specify Git ref globs by prefixing them with *
, and exclude a commit reachable from a ref by prefixing it with ^
. Diff searches can be further narrowed down with parameters that filter by author and time.
See the query syntax documentation for a comprehensive list of supported parameters.
Commit message search
Searching over commit messages is supported in Sourcegraph by adding type:commit
to your search query. Separately, you can also use the message:"any string"
parameter to filter type:diff
searches for a given commit message. Commit message searches can narrowed down further with filters such as author and time.
See our query syntax documentation for a comprehensive list of supported parameters.
Symbol search
Searching for symbols makes it easier to find specific functions, variables, and more. Use the type:symbol
filter to search for symbol results. Symbol results also appear in typeahead suggestions, so you can jump directly to symbols by name. When on an indexed commit, it uses Zoekt. Otherwise it uses the symbols service
Saved searches
Saved searches let you save and describe search queries so you can easily monitor the results on an ongoing basis. You can create a saved search for anything, including diffs and commits across all branches of your repositories. Saved searches can be an early warning system for common problems in your code and a way to monitor best practices, the progress of refactors, etc.
Search contexts
Search contexts help you search the code you care about on Sourcegraph. A search context represents a set of repositories at specific revisions on a Sourcegraph instance that will be targeted by search queries by default.
Every search on Sourcegraph uses a search context. Search contexts can be defined with the contexts selector shown in the search input, or entered directly in a search query.
If you currently use version contexts, you can automatically convert your existing version contexts to search contexts. We recommend migrating to search contexts for a more intuitive, powerful search experience and the latest improvements and updates.
See the search contexts documentation to learn how to use and create search contexts.
Multi-branch indexing
The most common branch to search is your default branch. To speed up this common operation, Sourcegraph maintains an index of the source code on your default branch. Some organizations have other branches that are regularly searched. To speed up search for those branches, Sourcegraph can be configured to index up to 64 branches per repository. Your site admin can configure multi-branch indexing through site configuration.
Exclude files and directories
You can exclude files and directories from search by adding the file .sourcegraph/ignore
to the root directory of your repository. Files or directories matching the glob patterns will not show up in the search results. For more information on creating .sourcegraph/ignore
files, see the configuration reference.
RE2 Regular Expressions
The Sourcegraph search language supports RE2 syntax. If you're used to tools like Perl which uses PCRE syntax, you may notice that there are some features that are missing from RE2 like backreferences and lookarounds. We choose to use RE2 for a few reasons:
- It makes it possible to build worst-case linear evaluation engines, which is very desirable for building a production-ready regex search engine.
- It's well-supported in Go, allowing us to take advantage of a rich ecosystem (notably including Zoekt)
- Our API and tooling makes it straightforward to use Sourcegraph with other tools that provide facilities not built in to the search language.
As an example of how you can use Sourcegraph tooling with other tools, we can use jq
(which supports Perl regexes) along with src
to post-filter search results. In this case, we want to use backreferences to find go functions take a single pointer argument and return a non-pointer of the same type as the input.
SHELLre2_regex='func \w+\(\w+ \*\w+\) \w+' pcre2_regex='func \w+\(\w+ \*(\w+)\) \1' src search --json --stream -- "/$re2_regex/" \ | jq ' //Filter to only content events select(.type == "content") # Filter to only content events //Flatten to a single object per match | {content: .chunkMatches[].content} + del(.chunkMatches) //Select only matches that match the PCRE regex | select(.content | test($ARGS.positional[0])) ' --args "$pcre2_regex"
Search experience
Users on Sourcegraph instance v5.9.0
or more get the improved and new Code Search experience set by default. Sourcegraph Enterprise users' site admins can optionally opt-out and revert to the old view.
You get the following improvements:
- In-line diff view: Easily compare commits and see how a file changed over time, all in-line
- Revamped code navigation: Quickly find a list of references of a given symbol, or immediately jump to the definition
- Reworked fuzzy finder ⌘K: Find files and symbols quickly and easily with our whole new fuzzy finder
- File actions: Like open in editor and open on code host
Other search tips
- When viewing a file or directory, press the
y
key to expand the URL to its canonical form (with the full 40-character Git commit SHA). - To share a link to multi-line range in a file, click on the starting line number and shift-click on the ending line number (in the left-hand gutter).