DEV Community

Cover image for Simple & Advanced Search in VS Code with Sourcegraph
Prosper Otemuyiwa for Sourcegraph

Posted on

Simple & Advanced Search in VS Code with Sourcegraph

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

Note: An alternative way is to install directly from the VS Code marketplace.

  • 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

  • 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

For more info, check out a detailed article about literal expression search on Sourcegraph.

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

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

Promise cases

To learn more, check out a detailed article about regular expression search on Sourcegraph.

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

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

Search for try and catch code samples

To learn more, check out a detailed article about structural search on Sourcegraph.

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, create one.

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

Go to settings
Settings

About to generate an access token
Click to generate access token

Name the access token
Give the access token a name

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

Generated Access token
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
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 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, 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 where our team will be happy to answer any questions you may have about Sourcegraph.

Top comments (1)

Collapse
 
erikaheidi profile image
Erika Heidi

Great post Prosper!