Code with Gemini Code Assist

This document describes how you can use Gemini Code Assist, an AI-powered collaborator in Google Cloud, to help you do the following in VS Code or IntelliJ and other supported JetBrains IDEs:

  • Provide guidance to help you solve problems with your code.
  • Generate code for your project.
  • Receive inline suggestions while you're coding.

Learn how and when Gemini for Google Cloud uses your data.

To help you comply with any license requirements for your code, Gemini Code Assist provides source citations when its suggestions directly quote at length from a specific source. To learn more about how and when Gemini cites sources, see How Gemini helps you generate code and cites sources.

Code customization lets you get code suggestions based on your organization's private codebase directly from Gemini Code Assist. Learn how to configure code customization.

This document is intended for developers of all skill levels. It assumes you have working knowledge of VS Code or IntelliJ and other supported JetBrains IDEs, and are familiar with Google Cloud. If you prefer, you can also explore Gemini Code Assist in Cloud Shell Editor and Cloud Workstations.

Before you begin

VS Code

  1. Before testing Gemini Code Assist capabilities in your code file, make sure your file's coding language is supported. For more information on supported coding languages, see Supported coding languages.

  2. If you prefer to use your IDE behind a proxy, see Network Connections in Visual Studio Code.

IntelliJ

  1. Before testing Gemini Code Assist capabilities in your code file, make sure your file's coding language is supported. For more information on supported coding languages, see Supported coding languages.

  2. If you prefer to use your IDE behind a proxy, see HTTP Proxy.

Install the Gemini Code Assist plugin

To install the Gemini Code Assist plugin in your IDE, follow these steps:

VS Code

  1. To open the Extensions view in VS Code, click Extension icon Extensions or press Ctrl/Cmd+Shift+X.

  2. Search for Gemini Code Assist.

  3. Click Install.

  4. If prompted, restart VS Code.

    After the extension has successfully installed, Gemini Code Assist appears in the activity bar and is ready for use. You can further configure your Gemini Code Assist installation by specifying your preferences using the top-level application taskbar: navigate to Code > Settings > Settings > Extensions and search for Gemini Code Assist.

IntelliJ

  1. Click settings IDE and Project Settings > Plugins.
  2. In the Marketplace tab, search for Gemini Code Assist.
  3. Click Install to install the plugin.
  4. When the installation is finished, click Restart IDE.
  5. When the IDE restarts, Gemini Code Assist appears in your activity bar.

    The Gemini Code Assist icon appears in the activity bar

Now you're ready to use Gemini Code Assist in your IDE.

Sign into Google and select a Google Cloud project

In this section, you sign in to your Google Account, and if it's your first time using Gemini Code Assist in your IDE, you select a Google Cloud project.

VS Code

If you select a Google Cloud project without the Gemini for Google Cloud API enabled, you receive a notification that gives you the option to enable the API from the IDE. Select Enable the API in the notification window to enable the API for your project. For more information, see Set up Gemini Code Assist for a project.

If you prefer to follow the Code with Gemini Code Assist walkthrough directly in your IDE, click Launch VS Code and follow the steps in the walkthrough to connect to Google Cloud and activate Gemini Code Assist.

Launch VS Code

Otherwise, follow these steps:

  1. Launch your IDE.

  2. In the activity bar, click Gemini Code Assist.

  3. In the Gemini Code Assist chat pane, click Login to Google Cloud.

  4. When prompted to allow Gemini Code Assist to open the external website, click Open.

  5. Follow the prompts to sign into your Google Account.

  6. When asked if you downloaded Gemini Code Assist from Google, click Sign In.

    You're now connected to Google Cloud.

    Next, to select a Google Cloud project that has the Gemini for Google Cloud API enabled, follow these steps:

  7. In the Gemini Code Assist status bar, click Gemini Code Assist.

    The Gemini status bar is available.

  8. In the Gemini Code Assist menu, select Select Gemini Code project.

  9. Select a Google Cloud project that has the Gemini for Google Cloud API enabled.

    Gemini Code Assist is ready to use.

    The Gemini icon in status bar is set to normal.

IntelliJ

To sign in to your Google Account, follow these steps:

  1. In the activity bar, click spark Gemini Code Assist.

  2. Click Log in to Google.

  3. On the page that opens in the web browser, select your Google Account.

  4. On the screen that asks you to make sure that you downloaded this app from Google, click Sign in.

    Gemini Code Assist is now authorized to access your account.

    Next, if this is your first time using Gemini in your IDE, you must select a Google Cloud project by following these steps:

  5. Return to your IDE. In the Gemini Code Assist tool window, if you agree to allow Google to enable the APIs required to use Gemini Code Assist on your behalf for your selected project, click Select a GCP project to continue.

  6. In the Select Google Cloud Project dialog, search for and select your Google Cloud project, and then click OK.

  7. Click FINISH.

Your Google Cloud project is selected with the Gemini Code Assist API enabled. You're ready to use Gemini in your IDE!

Chat with Gemini Code Assist

In this section, you prompt Gemini Code Assist to provide an explanation of your existing code.

VS Code

To get an explanation of your code, follow these steps:

  1. Open your code file.

  2. In the activity bar of your IDE, click spark Gemini Code Assist.

  3. In the Gemini Code Assist pane, enter the prompt Explain this code to me and click send Send.

    Gemini uses the code in your code file as a reference to your prompt and responds with an explanation of your code.

    To refer to a specific block of code instead of all the code in the file, you can select the block in your code file and then prompt Gemini.

IntelliJ

To get an explanation of your code, follow these steps:

  1. In your IDE, open your code file.

  2. In the Gemini Code Assist tool window, enter the prompt Explain this code to me and click Submit.

Gemini Code Assist uses the code in your code file as a reference to your prompt and responds with an explanation of your code.

If you only want an explanation of a certain part of your code, you can select certain code and then prompt Gemini Code Assist again. Gemini Code Assist will only use the selected code as a reference to its generated response.

When Gemini Code Assist provides you with code in its response, you can select the following options listed at the end of its response:

  • Insert at Cursor: Inserts the generated code into your current file at your current cursor position.

  • Insert in New File: Opens a new file and inserts the generated code into the new file.

    Gemini lists code actions at the end of a generated code response.

These options are available when Gemini Code Assist identifies the language used in your code block, and if this language is supported in your current IDE.

View query history

If you want to re-use your previous prompts, you can find them in your Query History in the Gemini Code Assist tool window by clicking schedule Show Query History.

Gemini Query History in the tool window.

Reset chat history

Gemini Code Assist uses the chat history for additional context when responding to your prompts. If your chat history is no longer relevant to what you're trying to achieve, you can reset the chat history:

VS Code

In the Gemini Code Assist pane, click delete Reset Chat.

IntelliJ

In the Gemini Code Assist tool window, click delete Clear Conversation History. This also clears your query history.

Button to clear conversation history in Gemini tool window.

Generate code with prompts

The following sections show you how to use Gemini Code Assist to generate code with the example prompt Function to create a Cloud Storage bucket inside your code file. You can also select a part of your code and then prompt Gemini Code Assist for help through the chat feature, and receive and accept or reject code suggestions while you code.

VS Code

Prompt Gemini Code Assist with code transformation

Code transformation allows you to use commands or natural language prompts in the Quick Pick menu to request modifications to your code, and provides you with a diff view to show pending changes to your code. To prompt Gemini Code Assist with code transformation, follow these steps:

  1. In your code file, on a new line, press Control+I (for Windows and Linux) or Command+I (for macOS) to open the Gemini Code Assist Quick Pick bar.

  2. In the menu, using the /generate command, enter /generate function to create a Cloud Storage bucket and then press Enter (for Windows and Linux) or Return (for macOS).

    Gemini Code Assist generates code with the /generate command.

    Gemini Code Assist generates the code based on your prompt in a diff view.

    Gemini Code Assist opens a diff view to show generated code.

  3. Optional: To accept these changes, click Accept.

Prompt Gemini Code Assist in a code file with a comment

If you prefer, you can also prompt Gemini Code Assist in your code file with a comment by following these steps:

  1. On a new line, enter the comment Function to create a Cloud Storage bucket, and then press Enter (for Windows and Linux) or Return (for macOS).

  2. To generate code, press Control+Enter (for Windows and Linux) or Control+Return (for macOS).

    Next to your prompt text in your code file, Gemini Code Assist generates the code in the form of ghost text.

  3. Optional: To accept the generated code, press Tab.

Prompt Gemini Code Assist with selected code using chat

Gemini Code Assist can perform tasks or answer your questions based on the code that you select. To get generated code that's based on a prompt with selected code, follow these steps:

  1. In the activity bar, click spark Gemini Code Assist to open the Gemini Code Assist pane.

  2. In your code file, select a block of code.

  3. In the Gemini Code Assist pane text field, enter a prompt for the selected code.

    For example, select a function in your code and enter the prompt Write a unit test for this function:

    Gemini writes a unit test for a selected function.

    Gemini uses your selected code as reference and responds to your prompt.

IntelliJ

Prompt Gemini Code Assist with code transformation

Code transformation allows you to use commands or natural language prompts in the Quick Pick menu to request modifications to your code, and provides you with a diff view to show pending changes to your code. To prompt Gemini Code Assist with code transformation, follow these steps:

  1. In your code file, on a new line, press Alt+\ (for Windows and Linux) or Cmd+\ (for macOS) to open the Gemini Code Assist Quick Pick menu.

  2. In the menu, using the /generate command, enter /generate function to create a Cloud Storage bucket and then press Enter (for Windows and Linux) or Return (for macOS).

    Code transformation generate function in IntelliJ Gemini Code Assist

    Gemini Code Assist generates the code based on your prompt in a diff view.

    Code transformation diff view in IntelliJ Gemini Code Assist

  3. Optional: To accept these changes, click Accept Changes.

    You can use the following code transformation commands in your IDE:

    • /fix: Fix issues or errors in your code. Example: /fix potential NullPointerExceptions in my code.
    • /generate: Generate code. Example: /generate a function to get the current time.
    • /doc: Add documentation to your code. Example: /doc this function.
    • /simplify: Simplify your code. Example: /simplify if statement in this code.

Prompt Gemini Code Assist in a code file with a comment

  1. In your code file, on a new line, enter the comment Function to create a Cloud Storage bucket.

  2. To generate code, press Alt+G (for Windows and Linux) or Option+G (for macOS). Alternatively, you can right-click next to the comment and select Generate Code.

    Gemini Code Assist generates the code below your comment in the form of ghost text.

  3. Optional: To accept the generated code, press Tab.

Prompt Gemini Code Assist with selected code using chat

Gemini Code Assist can perform tasks or answer your questions based on the code that you select. To get generated code that's based on a prompt with selected code, follow these steps:

  1. In the activity bar, click spark Gemini Code Assist to open the Gemini Code Assist tool window.

  2. In your code file, select a block of code.

  3. In the Gemini Code Assist tool window text field, enter a prompt for the selected code.

    For example, select a function in your code and enter the prompt Write a unit test for this function.

    Gemini Code Assist uses your selected code as reference and responds to your prompt.

Optional: Change keyboard shortcut for generating code

If the default keyboard shortcut for generating code isn't working as outlined in the previous section, you can change the keyboard shortcut by following these steps:

VS Code

  1. In your IDE, click File (for Windows and Linux) or Code (for macOS), and then navigate to Settings > Keyboard Shortcuts.

  2. In the list of keyboard shortcuts, scroll until you find Gemini Code Assist: Generate code.

  3. Click Gemini Code Assist: Generate Code, and then click edit Change Keybinding.

  4. In the dialog that appears, enter your own shortcut.

  5. Press Enter (for Windows and Linux) or Return (for macOS).

    You can now use your newly assigned keyboard shortcut to generate code with Gemini Code Assist.

To learn more about changing shortcuts in your IDE, see Keybindings for Visual Studio Code.

IntelliJ

  1. Navigate to settings IDE and Project Settings > Settings > Keymap > Gemini Code Assist.

  2. Right-click Gemini Code Assist and select Add Keyboard Shortcut.

  3. Enter your preferred keyboard shortcut and then click OK.

  4. Right-click Gemini Code Assist and select Remove Alt+G (for Windows and Linux), or Remove Option+G (for macOS).

You can now use your new keyboard shortcut to generate code in a code file.

Get code completions

While you write code, Gemini Code Assist makes inline code suggestions, also known as code completions, that you can either accept or ignore. To get code completions, follow these steps:

VS Code

  1. In your code file, on a new line, start writing a function. For example, if you're in a Python file, write def.

    Gemini Code Assist suggests code in the form of ghost text.

  2. To accept the code suggestion from Gemini Code Assist, press Tab. Otherwise, to ignore the suggestion, press Esc or continue writing your code.

IntelliJ

  1. In your code file, on a new line, start writing a function. For example, if you're in a Python file, write def.

    Gemini Code Assist suggests code in the form of an inline suggestion.

  2. To accept the code suggestion from Gemini Code Assist, press Tab. Otherwise, to ignore the suggestion, press Esc or continue writing your code.

  3. Optional: If you prefer to use a different shortcut key to accept the inline suggestion, hold your pointer over the inline suggestion and click the Tab dropdown that appears. Then, select your preferred shortcut or click Custom to enter your own shortcut.

    Gemini provides a dropdown menu to change your shortcut to accept an inline suggestion.

Optional: Disable code completion

Code completion is enabled by default. If you want to disable code completion, follow these steps:

VS Code

  1. In your IDE, click Code (for macOS) or File (for Windows and Linux), and then navigate to Settings > Settings.

  2. On the User tab of the Settings dialog, navigate to Extensions > Gemini Code Assist.

  3. Scroll until you find the Duet AI: Inline Suggestions: Enable Auto list, and then select Off.

    This turns off the inline suggestions. You can still press Control+Enter (for Windows and Linux) or Control+Return (for macOS) to manually trigger inline suggestions.

IntelliJ

In the status bar of your IDE, click spark Gemini Code Assist: Active and select Enable AI Code Completion.

Gemini Code Completion button in IntelliJ status bar.

This disables the code completion setting, and Gemini Code Assist no longer makes inline suggestions until you enable the setting again.

Use smart actions

To help you be more productive while minimizing context switching, Gemini Code Assist provides AI-powered smart actions directly in your code editor. When you select your code in your code editor, you can view and select from a list of actions relevant to your context.

To use smart actions in your code, follow these steps:

VS Code

  1. In your code file, select a block of code.

  2. Next to the selected code block, click lightbulb Show Code Actions.

    Smart actions lightbulb icon appears after selecting a block of code in VS Code.

  3. Select an action such as Generate unit tests.

    Gemini generates a response that's based on the action you selected.

IntelliJ

  1. In your code file, select a line or block of code.

  2. Right-click the selected code and select a smart action, like Generate unit tests.

    Selecting the smart action will automatically prompt Gemini Code Assist to generate a response to the prompt in the Gemini Code Assist tool window.

Use code transformation quick fix

If there's an error in your code, Gemini Code Assist gives you the option to apply a quick fix to the error with code transformation.

To apply a quick fix in your code file, follow these steps:

VS Code

  1. In your code file, hold your pointer over the squiggly error line and select Quick Fix, and then select /fix.

    Code transformation quick fix in the IDE.

  2. When the quick fix is applied, a diff view appears. To accept these changes, click Accept.

IntelliJ

  1. In your code file, click the red error bulb icon, which indicates an error in your code, and select Fix with Gemini.

    Code transformation option to fix with Gemini in the IDE.

  2. When the fix is applied, a diff view appears. To accept these changes, click Accept.

Prompt with specific files in your workspace with local codebase awareness

Local codebase awareness helps you with high quality code suggestions that are syntactically correct and semantically meaningful within the broader context of your codebase, when you specify files for Gemini Code Assist to use as context.

To specify files in your chat prompt, type @ and select the files you want to specify.

Specify files with local codebase awareness.

To get an explanation on the differences of two files in your codebase, follow these steps:

  1. In the activity bar, click spark Gemini Code Assist.

  2. In the Gemini Code Assist pane, enter the prompt Explain the difference between @YOUR_FILE_NAME_1 and @YOUR_FILE_NAME_2 and press Enter (for Windows and Linux) or Return (for macOS), or Tab. You can also click the name of the file in the list to select the file. Clicking the file name adds the file to your prompt context and opens the file in your IDE.

    Gemini Code Assist responds to your prompt while using the two files you specified for context. Gemini Code Assist also includes the files you specified in Context Sources.

Now that you've specified those files, you can continue asking additional questions or prompts in the same chat history, without having to specify the files again.

For example: In the Gemini Code Assist pane, enter the prompt How can I improve YOUR_FILE_NAME_1? (without the @ symbol) and press Enter (for Windows and Linux) or Return (for macOS).

Gemini Code Assist responds to your enquiry about the file you specified in your prompt.

Optional: Exclude files from context with an .aiexclude file

You can exclude files from being considered by Gemini Code Assist for context by creating an .aiexclude file. An .aiexclude file follows the similar syntax as a .gitignore file, with the following differences:

  • An empty or nonexistent .aiexclude file blocks no files in its directory and all sub-directories.
  • An .aiexclude file doesn't support negation, where you prefix patterns with an exclamation point (!).
  • An .aiexclude file matches greedily on the * character. This will match indiscriminately on directories and files. This .aiexclude file doesn't differentiate between ** or * with regards to folders or files.

Only a single .aiexclude file is supported currently. To configure your .aiexclude file, create a file titled .aiexclude in the root of your workspace folder.

Examples

The following examples demonstrate how you can configure an .aiexclude file:

  • Block all files named apikeys.txt at or below the workspace directory:

    apikeys.txt
    
  • Block all files with the .key file extension at or below the workspace directory:

    *.key
    
  • Block only the apikeys.txtfile at the same directory as the .aiexclude file, but not any subdirectories:

    /apikeys.txt
    
  • Block all files in the directory my/sensitive/dir and all subdirectories. The path should be relative to the workspace root directory.

    my/sensitive/dir/*
    

Disable code suggestions that match cited sources

Gemini Code Assist provides citation information when it directly quotes at length from another source, such as existing open source code. For more information, see How and when Gemini cites sources.

To prevent code that matches cited sources from being suggested to you, do the following:

  1. In the activity bar of your IDE, click Manage > Settings.

  2. In the User tab of the settings window, navigate to Extensions > Gemini Code Assist.

  3. Scroll until you find Duet AI > Recitation: Max Cited Length.

  4. Set the value to 0.

    Gemini Code Assist no longer suggests code to you that matches cited sources.

Known issues

This section outlines the known issues of Gemini Code Assist:

VS Code

  • Chat responses may be truncated when they include an updated version of a large open file

    To work around this issue, select a smaller section of code and include an additional directive in the chat prompt, such as only output the selected code.

  • Vim: Cannot accept or dismiss code generation suggestions unless in insert mode

    When using the Vim plugin in normal mode, you can't accept or dismiss code suggestions.

    To work around this issue, press i to enter insert mode, and then press Tab to accept the suggestion.

  • Vim: Inconsistent behavior when pressing Esc to dismiss suggestions

    When you press Esc, both the IDE and Gemini suggestions are dismissed. This behavior is different from the non-Vim behavior where pressing Esc re-triggers Gemini.

  • Sign-in attempts keep timing out

    If your sign-in attempts keep timing out, try adding the cloudcode.beta.forceOobLogin setting to your settings.json file:

     "cloudcode.beta.forceOobLogin": true
    
  • License recitation warnings don't persist across sessions

    If license recitation warnings don't persist across sessions, refer to the persistent logs:

    1. Click View > Output.

    2. Select Gemini Code Assist - Citations.

  • Connectivity issues in the Gemini Code Assist output window

    If you see a connection error or other connectivity problems in the Gemini Code Assist output window, try the following:

    • Configure your firewall to allow access to oauth2.googleapis.com and cloudaicompanion.googleapis.com.

    • Configure your firewall to allow communication over HTTP/2, which gRPC uses.

    You can use the grpc-health-probe tool to test connectivity. A successful check results in the following output:

    $ grpc-health-probe -addr cloudaicompanion.googleapis.com:443 -tls error: this server does not implement the grpc health protocol (grpc.health.v1.Health): GRPC target method can't be resolved

    An unsuccessful check results in the following output:

    timeout: failed to connect service "cloudaicompanion.googleapis.com:443" within 1s

    To obtain more details, run the following before grpc-health-probe:

    export GRPC_GO_LOG_SEVERITY_LEVEL=info
    
  • 'Activate Gemini Code Assist' still appears after selecting Gemini Code Assist project

    If you selected your project per the instructions in this guide, which includes enabling the Gemini for Google Cloud API, then there may be an issue with the LS server.

    To find out more details about your issue, perform the following actions:

    1. In the status bar, click spark Gemini Code Assist and then select Send feedback.

    2. Click Show for any of the log files to see more details and to help troubleshoot the issue.

IntelliJ

There are no known issues for Gemini Code Assist for IntelliJ and other supported JetBrains IDEs.

Leave feedback

To leave feedback of your experience, follow these steps:

VS Code

  1. In the status bar, click spark Gemini Code Assist, and then in the Quick Pick menu, select Send feedback.

  2. In the form, fill out the Title and Comments fields.

  3. If you want to share your Skaffold or AI Companion logs, make sure that you select the Send Skaffold logs or Send AI Companion logs option.

  4. Click Submit Feedback.

IntelliJ

  1. In the status bar, click spark Gemini Code Assist, and then in the menu, select Submit feedback.

  2. In the text field at the top of the form, enter your feedback.

  3. If you want to share your Gemini Code Assist logs, make sure that you select the log files.

  4. Click Submit Feedback.

What's next