8 Sourcegraph Search Query Features You Should Know About
Tags: codesearch, webdev, programming, productivity
Published: 2022-01-03 09:01:51


[Sourcegraph](https://sourcegraph.com) is a search tool that enables developers to search, explore and better understand all of their public and private code. It supercharges developer productivity and automates large-scale code changes. 

In this article, I’ll explore some Sourcegraph search gems with you. 

Here are 8 Sourcegraph search features you should know about!

## 1. Literal Expression Search

By default, all search queries on Sourcegraph are treated as literal patterns. If you know the exact string you’re looking for in the codebase, then all you need to do is paste it in the query box and search.
 
Sourcegraph interprets this search pattern literally (as the name defines it) to simplify searching for words or punctuation in your codebase.

With literal search, you can find function calls, error messages, comments, notes etc. For instance, if you want to search for how to use the *browser navigator clipboard API*, you can simply use:

```
navigator.clipboard
```

**Search Query:** [https://sourcegraph.com/search?q=context:global+navigator.clipboard&patternType=literal](https://sourcegraph.com/search?q=context:global+navigator.clipboard&patternType=literal)


![Navigator Clipboard search](https://res.cloudinary.com/unicodeveloper/image/upload/v1641198704/navigator-clipboard.gif)


For more info, check out a [detailed article about literal expression search on Sourcegraph](https://learn.sourcegraph.com/how-to-search-code-with-sourcegraph-using-literal-patterns).


## 2. Regular Expression Search

Sourcegraph’s query language is very powerful. One of its strengths is in the regular expression search it provides.

On the search box, you need to enable the regular expression mode by clicking on the `*` sign on the right.


![Enable regular expression](https://res.cloudinary.com/unicodeveloper/image/upload/v1641196727/unnamed_2_zfgnmo.gif)



Once the regex mode is enabled, you can start using regular expressions for advanced search patterns. You can also add `patterntype:regexp` to your queries and this will automatically enable regex mode.  

Let's see a few different use cases of regex search as examples.

**1. Find how the filesync `readFile` and `writeFile` methods are used in several projects.**

```
fs\.(read|write)File patterntype:regexp 
```

**Search Query:** [https://sourcegraph.com/search?q=context:global+fs%5C.%28read%7Cwrite%29File&patternType=regexp](https://sourcegraph.com/search?q=context:global+fs%5C.%28read%7Cwrite%29File&patternType=regexp)

![Read write file search](https://res.cloudinary.com/unicodeveloper/image/upload/v1641200216/read-write-file-search.gif)


**2. Find real-life use cases for `Promise.all`, `Promise.any`, `Promise.race`, and `Promise.allSettled`.**

```
Promise.(any|race|allSettled) lang:JavaScript patterntype:regexp
```

**Search Query:** [https://sourcegraph.com/search?q=context:global+Promise.%28any%7Crace%7CallSettled%29+lang:JavaScript&patternType=regexp](https://sourcegraph.com/search?q=context:global+Promise.%28any%7Crace%7CallSettled%29+lang:JavaScript&patternType=regexp)


![Promise search](https://res.cloudinary.com/unicodeveloper/image/upload/v1641199017/promise-js-search.gif)

Check out a [detailed article about the regular expression search on Sourcegraph](https://learn.sourcegraph.com/how-to-search-with-sourcegraph-using-regular-expression-patterns).

## 3. Structural Search

Structural search gives you wings as a developer. With structural search, you can copy-paste blocks of code into the Sourcegraph search box, and accurate results will be returned.

First, enable the structural search mode by clicking on the `[ ]` sign on the right:

![Enable Structural search](https://res.cloudinary.com/unicodeveloper/image/upload/v1641199976/enable-structural-search.gif)


Then you can go ahead and perform a structural search like so:

```
"exclude": [...] lang:json file:tsconfig.json patterntype:structural
```

**Search Query:** 
[https://sourcegraph.com/search?q=context:global+%22exclude%22:+%5B...%5D+lang:json+file:tsconfig.json&patternType=structural](https://sourcegraph.com/search?q=context:global+%22exclude%22:+%5B...%5D+lang:json+file:tsconfig.json&patternType=structural)

![Structural search](https://res.cloudinary.com/unicodeveloper/image/upload/v1641199948/exclude-structural-search.gif)
 

To learn more, check out a [detailed article about structural search on Sourcegraph](https://learn.sourcegraph.com/how-to-search-with-sourcegraph-using-structural-patterns).


## 4. Diff Search

Diff search allows you to search for code in file diffs. It makes it easy for a developer to search for code that used to exist and compare it with code that presently exists.

For example, the following search…

```
repo:^github\.com/sourcegraph/sourcegraph$ type:diff matrix
```

**Search Query:** [https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+type:diff+matrix&patternType=literal](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+type:diff+matrix&patternType=literal)

![Diff search](https://res.cloudinary.com/unicodeveloper/image/upload/v1641199855/diff-search.gif)

## 5. Commit Search

Commit search allows you to search for specific strings used in commit messages across your codebase.

Let’s search over commit messages like so:

![Commit search](https://res.cloudinary.com/unicodeveloper/image/upload/v1641199769/commit-search.gif)



## 6. Search Contexts

Search contexts are a set of repositories at specific revisions that search queries on Sourcegraph will target. 

In other words, search contexts enable you to narrow, group and search only the code repositories you care about.

By default, Sourcegraph uses a global search context (namely, **context:global**) for all search queries, when no search context is specified by the actor (me, you, or a programmable robot) performing the search.


![Search contexts](https://res.cloudinary.com/unicodeveloper/image/upload/v1641198531/search-contexts-chakra.gif)


To learn more, check out a [detailed article about creating, and using search contexts on Sourcegraph](https://dev.to/sourcegraph/how-to-use-search-contexts-on-sourcegraph-138n).


## 7. Symbol Search

Symbol search comes in handy when you want to directly search for function, class, variable and const definitions in your code search.

With Sourcegraph, you can use the “type:symbol” filter in your search query like so:

```
repo:^github\.com/sourcegraph/sourcegraph$ type:symbol Vector
```

The search query returns the symbol you searched for. All the symbols in that particular repository or file are grouped and shown on the left-hand side.

The grouped symbol list is searchable as shown in the gif below. You can search for whatever symbol you want.


**Note:** Sourcegraph has Symbol support for 75+  languages


![Symbol search](https://res.cloudinary.com/unicodeveloper/image/upload/v1641198656/symbol-search.gif)


## 8. Branch & Tag Search

Sourcegraph makes it possible to search for code in all of your branches and tags. Yes, all your branches, not only the main branch!

### Branch Search

**Use Case:** Search the “add-panel-zoekt-cap” branch of the [sourcegraph/sourcegraph repo](https://github.com/sourcegraph/sourcegraph) for webhook.

**Search Query:** [https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rev:*refs/heads/add-panel-zoekt-cap+webhook&patternType=literal](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rev:*refs/heads/add-panel-zoekt-cap+webhook&patternType=literal)

![Tag search for webhook](https://res.cloudinary.com/unicodeveloper/image/upload/v1641199027/tag-webhook-search.gif)

### Tag Search

**Use Case:** Search the “v3.32.0-rc.2” tag of the [https://github.com/sourcegraph/sourcegraph repo](https://github.com/sourcegraph/sourcegraph) for batch changes.

**Search Query:** [https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rev:*refs/tags/v3.32.0-rc.2+batch+changes&patternType=literal](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rev:*refs/tags/v3.32.0-rc.2+batch+changes&patternType=literal)


![Tag search](https://res.cloudinary.com/unicodeveloper/image/upload/v1641199042/tags-batch-changes-search.gif)



## Conclusion

The benefits of code search are innumerable. The faster you can find code, the higher the development velocity for you and your teammates.

[Sourcegraph Cloud](https://sourcegraph.com) is not the only option to add, explore private code and use search contexts. Sourcegraph can be installed in different ways to give you advanced search features for your private code. 

You can run [Sourcegraph locally](https://docs.sourcegraph.com/admin/install#self-hosted) or opt-in for us to set up a [managed Sourcegraph instance](https://docs.sourcegraph.com/admin/install/managed) for your team. You'll be able to use a self-hosted Sourcegraph instance for free (up to 10 users).

---
Have suggestions or questions? Leave a comment, or join our [Community Slack Space](https://about.sourcegraph.com/community/?utm_medium=social&utm_source=devto&utm_campaign=slacklaunch) where our team will be happy to answer any questions you may have about Sourcegraph.