Simple & Advanced Search in VS Code with Sourcegraph
Tags: javascript, vscode, codesearch, tutorial
Published: 2022-02-10 19:15:55

The hallmark of fast and productive developers is finding and deploying resources quickly to succeed in their projects. Software is eating the world. The rapid digital transformation has led to countless innovations & services that improve people's lives all over the world. However, it's not been easy for developers.

Sourcegraph universal code search enables developers to find and fix things fast across the complex universe of interdependent codebases – various programming languages, code hosts, repositories, version control systems, services, and APIs.


## Simple and Advanced Search in VS Code

Sourcegraph released a VS Code extension that enables any developer to search over two million open source repositories right from inside VS Code IDE—for free.

With Sourcegraph for VS Code, you can:

- Learn from open source code examples
- Search best practices
- Copy and paste code from open-source land. 
- Sync your private code and search it within VS Code.

## How to Install Sourcegraph VS Code Extension

- Open VS Code.

- Search for "Sourcegraph for VS Code" in the extensions marketplace. 

![Search for Sourcegraph Extension](https://res.cloudinary.com/unicodeveloper/image/upload/v1644325588/CleanShot_2022-02-08_at_13.59.35_2x_jayl3x.png)

**Note:** An alternative way is to [install directly from the VS Code marketplace](https://marketplace.visualstudio.com/items?itemName=sourcegraph.sourcegraph).

- Click on the wildcard icon on the Activity Bar to launch the extension, as shown in the image below:

![Click on the Sourcegraph wildcard icon](https://res.cloudinary.com/unicodeveloper/image/upload/v1644326017/CleanShot_2022-02-08_at_14.12.40_2x_ndyxlf.png)

- Search for code!

## Code Search with Sourcegraph in VS Code

Sourcegraph supports three modes of code search:

- Literal
- Regular Expression
- Structural 

### Literal Search in VS Code

Sourcegraph allows you to search for the exact string you are looking for in the codebase in a literal search pattern. For example, you can find function calls, comments, error messages, etc.

With literal Search, you can find function calls, error messages, comments, notes etc. For instance, if you want to search for "palindrome" you can search like so:

**Example:** Search for palindrome code samples

![Palindrome search](https://res.cloudinary.com/unicodeveloper/image/upload/v1644328351/CleanShot_2022-02-08_at_14.50.33_t33ozi.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).

### Regular Expression Search in VS Code

Sourcegraph enables the mode to harness regular expression in code search. You need to allow the regular expression mode on the search box by clicking on the `*` sign on the right.

![Enable regex search mode](https://res.cloudinary.com/unicodeveloper/image/upload/v1644328936/CleanShot_2022-02-08_at_15.01.22_bzwail.gif)

**Example:** Find code samples for the following Promise cases in JavaScript. `Promise.any`, `Promise.all`, `Promise.allSettled` and `Promise.case`

![Promise cases](https://res.cloudinary.com/unicodeveloper/image/upload/v1644476729/CleanShot_2022-02-10_at_08.02.54_gec2ea.gif)

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

### Structural Search in VS Code

Structural Search gives you the ability to copy-paste code blocks into the search box and get accurate results returned.

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

![Enable Structural Search Mode](https://res.cloudinary.com/unicodeveloper/image/upload/v1644477222/CleanShot_2022-02-10_at_08.12.57_l7feop.gif)

**Example:** Find code samples of `try and catch` in JavaScript code.

![Search for try and catch code samples](https://res.cloudinary.com/unicodeveloper/image/upload/v1644477625/CleanShot_2022-02-10_at_08.18.09_fmor9x.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).

## Private Code Search in VS Code

The VS Code extension provides private code search. It allows you add and search your own code. To search for private code, you'll need to do the following:

- If you don't have an account on [Sourcegraph](https://sourcegraph.com?utm_campaign=7015b000005hsb3aae&utm_medium=referral&utm_source=dev.to&utm_content=prosper-vscodesearchpost), [create one](https://sourcegraph.com/sign-up?utm_campaign=7015b000005hsb3aae&utm_medium=referral&utm_source=dev.to&utm_content=prosper-vscodesearchpost).

- On Sourcegraph Cloud, navigate to **Access tokens** in your account settings and generate a new token.

![Go to settings](https://res.cloudinary.com/unicodeveloper/image/upload/v1644480245/CleanShot_2022-02-10_at_09.02.34_2x_rvvfc2.png)
_Settings_

![About to generate an access token](https://res.cloudinary.com/unicodeveloper/image/upload/v1644480175/CleanShot_2022-02-10_at_09.01.30_2x_valrkq.png)
_Click to generate access token_

![Name the access token](https://res.cloudinary.com/unicodeveloper/image/upload/v1644480583/CleanShot_2022-02-10_at_09.07.21_2x_bblmyr.png)
_Give the access token a name_

- Copy the newly generated token, and navigate to the Sourcegraph extension.

![Generated Access token](https://res.cloudinary.com/unicodeveloper/image/upload/v1644480637/CleanShot_2022-02-10_at_09.07.56_2x_exl6zo.png)
_Copy the newly generated access token_

- Paste the token into the _access token input field_ in the extension's sidebar. 

![Paste the generated access token into Sourcegraph VS Code extension](https://res.cloudinary.com/unicodeveloper/image/upload/v1644481092/CleanShot_2022-02-10_at_09.17.21_2x_ot8wlg.png)
_Paste the generated access token into Sourcegraph VS Code extension_

- The Editor will be reloaded automatically to use the newly added token.


## Conclusion

Sourcegraph for VS Code brings the power of [Sourcegraph query language](https://sourcegraph.com?utm_campaign=7015b000005hsb3aae&utm_medium=referral&utm_source=dev.to&utm_content=prosper-vscodesearchpost) into your IDE. Now, you are building & leveraging advanced code search in the same environment. One more step to optimizing your developer productivity!

You can also run [Sourcegraph locally](https://docs.sourcegraph.com/admin/install#self-hosted?utm_campaign=7015b000005hsb3aae&utm_medium=referral&utm_source=dev.to&utm_content=prosper-vscodesearchpost), or opt-in for us to set up a managed Sourcegraph instance 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_campaign=7015b000005hsb3aae&utm_medium=referral&utm_source=dev.to&utm_content=prosper-vscodesearchpost) where our team will be happy to answer any questions you may have about Sourcegraph.