In this ultimate guide, we’ll walk you through the steps to set up, configure, and use ChatGPT in VS Code while showcasing its diverse features and capabilities to enhance your coding experience.
Installation and Configuration of ChatGPT in VS Code
Install ChatGPT extension
To get started with ChatGPT in VS Code, you’ll need to install the ChatGPT extension. Launch VS Code and proceed to the Extensions Marketplace by following these steps:
Search for “ChatGPT” and click the “Install” button.
Upon successful installation, you will find the ChatGPT icon displayed on the left sidebar of VS Code.
Configure ChatGPT with API key
After installing the ChatGPT extension, you’ll need to set up an API key to access the GPT-3 language model. Follow these steps to configure ChatGPT with your API key:
To access the settings in VS Code, click on the gear icon located in the bottom left corner of the interface. Utilize the search bar to
Obtaining the session token
To use the ChatGPT extension, you’ll need to authenticate with a valid session token. Follow these steps to obtain a session token:
Go to https://chat.openai.com/chat and log in or sign up.
Open the developer tools in your browser (e.g Chrome, Firefox).
Navigate to the Application tab and open the Cookies section.
Copy the value for __Secure-next-auth.session-token and save it.
Once you have the session token, add it to the ChatGPT settings in VS Code.
Find out how to run ChatGPT locally on a PC in our comprehensive guide!
Using the ChatGPT extension
After successfully installing and configuring the ChatGPT extension, you can start using it to enhance your coding experience. Here are some key features and use cases:
In VS Code, open a text editor and locate the ChatGPT icon in the sidebar. By clicking on the icon, a panel will appear, presenting an input field where you can enter your prompt or question.
Press enter to send your query to ChatGPT. The artificial intelligence (AI’s) response will be displayed in the sidebar (please note that it may take some time for the response to be calculated).
To utilize ChatGPT in VS Code, you have multiple options. You can either select a code snippet in the editor and provide a prompt in the side panel, or you can right-click and choose “Ask ChatGPT.” When you send your query to the AI, the selected code will be automatically appended.
Simply click on the code block displayed in the panel to insert a code snippet from the AI’s response into the editor. The code will then be inserted at the current cursor position in the active editor.
Check out the 25 best ChatGPT extensions for enhancing your conversations in our detailed blog!
Refactoring and explaining code with ChatGPT
ChatGPT in VS Code can also help you refactor or explain selected code. To use this feature:
Select the code in the editor.
Perform a right-click on the selected code, and from the context menu, select one of the following options:
Ask ChatGPT: Provides a prompt for you to enter any query.
ChatGPT Explain selection: Explains what the selected code does.