Copilot-logo

Copilot Logo.

Introduction

In my previous blog post Using Copilot in Programming I wrote about my first impressions of using Copilot with VSCode editor. In this blog post, I will tell some tips for using Copilot so that you only trigger it when you need it.

I Started to Practice Clojure Programming Again

After a couple of years doing just enterprise stuff in my current corporation, I wanted to refresh my Clojure programming skills doing the excellent 4ever-clojure exercises. I have already written a blog post regarding doing these exercises earlier: 4Clojure Exercises Part 1. During that blog post and this blog post Copilot and other generative AI programming tools have revolutionized how programmers write code. I have also used Copilot quite a lot. But doing these exercises I really felt Copilot rather intrusive. I didn’t want to see any Copilot code suggestions when I was trying to solve some specific exercise myself. So, one can just turn off Copilot code suggestions in VSCode: Disable Completions. But. Sometimes I wanted Copilot to generate some test data, e.g. some map with repetive elements in the map. Then I had to turn Copilot code suggestions back: Enable Completions. I was wondering if there is a smoother way to trigger Copilot only when I wanted something. So, I googled the keybindings you can use with Copilot.

Copilot Keybindings

First thing I suggest to you is to keep your VSCode settings.json and keybindings.json files under git. This way it is safe to experiment with VSCode settings and keybindings. If you mess something, you can always revert to previous settings and keybindings. In Linux/Debian you can find the VSCode settings in ~/.config/Code/User directory.

These are the settings and keybindings I did to make my Copilot workflow smoother.

Settings (settings.json)

"editor.inlineSuggest.enabled": false This setting turns the Copilot inline suggestions off. Therefore, you don’t get automatic Copilot suggestions, but you have to trigger them on a need basis.

Keybindings (keybindings.json)

These are my new Copilot keybindings. All Copilot suggestion related keybindings use the same modifiers (ctrl+shift) and at the same row in my Dygma Raise Keyboard (in Finnish Layout the keys n, m, , and . are in the lowest row in the right side of the keyboard). So, it is easier to learn the muscle memory when the keybindings use the same modifiers and they are near to each other.

Trigger Copilot inline suggestion:

{
    "key": "ctrl+shift+n",
    "command": "editor.action.inlineSuggest.trigger",
    "when": "config.github.copilot.inlineSuggest.enable && editorTextFocus && !editorHasSelection && !inlineSuggestionsVisible"
},

Accept the whole Copilot suggestion:

{
    "key": "ctrl+shift+m",
    "command": "editor.action.inlineSuggest.commit",
    "when": "inlineEditIsVisible && tabShouldAcceptInlineEdit && !editorHoverFocused && !editorTabMovesFocus && !suggestWidgetVisible || inlineSuggestionHasIndentationLessThanTabSize && inlineSuggestionVisible && !editorHoverFocused && !editorTabMovesFocus && !suggestWidgetVisible || inlineEditIsVisible && inlineSuggestionHasIndentationLessThanTabSize && inlineSuggestionVisible && !editorHoverFocused && !editorTabMovesFocus && !suggestWidgetVisible || inlineEditIsVisible && inlineSuggestionVisible && tabShouldAcceptInlineEdit && !editorHoverFocused && !editorTabMovesFocus && !suggestWidgetVisible"
},

The default keybinding is tab. But I changed this next to my trigger Copilot inline suggestion keybinging.

Accept Copilot suggestion word by word:

{
    "key": "ctrl+shift+,",
    "command": "editor.action.inlineSuggest.acceptNextWord",
    "when": "inlineSuggestionVisible && !editorReadonly"
},

This is actually really nice. You don’t have to accept the whole suggestion, but you can accept suggestion word by word.

Open the Copilot suggestions panel:

{
    "key": "ctrl+shift+.",
    "command": "github.copilot.generate",
    "when": "editorTextFocus && github.copilot.activated && !commentEditorFocused && !inInteractiveInput && !interactiveEditorFocused"
},

This is also nice functionality. You can open another window for the Copilot suggestions. You see several suggestions in a separate panel and the suggestions do not pollute your editor view.

Start Copilot inline chat:

{
    "key": "ctrl+i 9",
    "command": "inlineChat.start",
    "when": "editorFocus && inlineChatHasProvider && inlineChatPossible && !editorReadonly && !editorSimpleInput"
},

I use this functionality a bit seldom. Therefore, I do not mind that I need to hit a bit longer keybinding chord. I configured this keybinding earlier, so it is what it is for historical reasons. Maybe later on I will change it to ctrl+shift+- (- key is next to ., that way all Copilot keybindings would be near to each other).

Conclusions

Copilot is a powerful helper for a programmer. But the default behaviour is rather intrusive. Luckily, you can configure Copilot to help you only when you need help.

The writer is working at a major international IT corporation building cloud infrastructures and implementing applications on top of those infrastructures.

Kari Marttila

Kari Marttila’s Home Page in LinkedIn: https://www.linkedin.com/in/karimarttila/