iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🦦

Shortcut for GitHub Copilot's #selection Inspired by Cursor's Ctrl+L

に公開

Cursor's Ctrl+L was so convenient that I made it possible to execute GitHub Copilot's #selection with a command. The final behavior looks like this ↓

selection-command

How to Make Selected Text in the Editor the Chat Context (Default)

GitHub Copilot

Select the text, then type #selection in the Chat, and the selected text will be added to the context.

vscode-selection

Cursor

Cursor provides a shortcut to add selected text to the context.
Press Ctrl+L to add the selected text to the context.

cursor-selection

Cursor is super convenient!! So I made it possible to execute the same action with a shortcut key in GitHub Copilot.

Execute GitHub Copilot's #selection with a Command

After selecting text in the editor, right-click to see the menu item Copilot > Add Selection to Chat. This means the action "Add selection to Chat" is registered as a command.
selection-panel

If it's registered as a command, it's simple. Open the GitHub Copilot Chat extension, click the gear icon, and open Keyboard Shortcuts.
ghcp-extension

Next, search for Add Selection to Chat and set a shortcut key.
Press the + button, then press any key to register it as a shortcut (the image shows the key pressed for the screenshot).

Since Ctrl+L is a default VS Code shortcut, I set it to Ctrl+L Ctrl+S.
key-press

After changing the shortcut key, the Source changes from the GitHub Copilot Chat extension's shortcut to User, so be careful when checking the settings again. You can find it by searching for User in the shortcut key settings list.
You can also reset it to the original state by right-clicking.
user-key

With this setup, select text in the editor and press Ctrl+L Ctrl+S, and the selected text will be added to the Chat context. Super easy!

selection-command

Summary

This can be applied to all commands registered by extensions on VS Code, so you can set shortcut keys for other extensions in the same way.

If you find yourself thinking, "I wish I could execute this operation in VS Code with a shortcut key," try looking for the command and setting a shortcut key in the same way!

Have a great VS Code life!

Discussion