Toggling Github Copilot in Vim with unimpaired

I was trying to remove the Github Copilot configuration from my Vim setup, but then I noticed it was not there at all. I have been neglecting to commit changes to my dotfiles from my MacBook, as I did not have the courage to share stuff I was just trying out. On my new Linux-based Thinkpad I do in fact commit everything, as running open source stuff makes me want to work in public more too.

But since this config was only in the copy of my dotfiles that now lives on ~/dotfiles-mac, it will get lost now that I don't copy it. A good reason to blog about it.

First: unimpaired is a Vim plugin by tpope, and it is one that should just be part of Vim itself. It adds all kinds of mappings with the [ and ] brackets, and many of them I use daily (most notably [q, [e and [space). The o variant of this will change options, with the special yo binding to toggle the option. I use yoh all the time (toggles search highlighting) and also yol is very useful (shows invisible characters).

I wanted to be able to toggle Github Copilot for the current buffer in this same style. Luckily, the g was still available, and luckily, unimpaired provides an easy way to add new mappings. Unlucky as we are, copilot does not actually provide a toggle, but with Ctrl+R and = in insert mode, we can evaluate some internal options and print a string based on that. See the full command below.

" Going with that flow
Plug 'github/copilot.vim'

" Toggle github copilot
nmap <script> <Plug>(unimpaired-enable)g  :Copilot enable<CR>
nmap <script> <Plug>(unimpaired-disable)g :Copilot disable<CR>
nmap <script> <Plug>(unimpaired-toggle)g  :Copilot <C-R>=copilot#Enabled() ? "disable" : "enable"<CR><CR>

As for why I stop with Copilot? It was very useful in my time at Sneaker District, as I was the only developer working on the project and I wanted to be faster than I was on my own. Having a junior developer in my editor was a nice thing to have: it provided me with good suggestions that I usually had to edit a bit.

But I also noticed it slowed me down at times: I got into the habit of stopping to type to see what it would suggest. And sometimes I just stopped to think, and then it would give me suggestions for directions I did not want to go in. For now, I want to experience less distractions, and get into the habit of typing and thinking for myself again.

(There is also some money involved, and the question whether or not you want to send your code to Github for this purpose. It is nice to have those concerns gone, but it wasn't my primary one.)