copilot.lua icon indicating copy to clipboard operation
copilot.lua copied to clipboard

Feature Request: Disabled by Default?

Open 0x4D5352 opened this issue 1 year ago • 10 comments

I'm using copilot.lua and copilot-cmp.lua and find the workflow great, but prefer to start my sessions with copilot disabled, then enable it when desired/needed. Could there be an option added to change the initialized state from enabled to disabled?

0x4D5352 avatar Jul 24 '24 15:07 0x4D5352

Try:

return {
  {
    "zbirenbaum/copilot.lua",
    opts = {
      filetypes = {
        ["."] = false,
        go = true
      },
    },
  },
}

nounder avatar Jul 28 '24 22:07 nounder

Unfortunately, does not seem to change the behavior @nounderline.

0x4D5352 avatar Aug 05 '24 21:08 0x4D5352

+1 to this. I expected the enabled option in setup to do this, but it doesn't and if I turn off all filetypes with ["*"] = false, I can't manually enable it with Copilot enable. I get [Copilot] 'filetype' python rejected by config filetypes[*].

I can enable it using Copilot! attach, but I don't know if always using that command is a good idea.

oyarsa avatar Oct 04 '24 09:10 oyarsa

I agree that an option like that is needed, copilot is for many people something that should be opt in and not opt out, using Copilot! attach is problematic because you might want to use it for a session or whatever and not want to have to do it for each buffer.

A workaround is just to have vim.cmd 'Copilot diable' in init.lua, which i do, but it unnecessarily loads the plugin.

StitiFatah avatar Oct 12 '24 04:10 StitiFatah

+1

It would be great to have a way for disabling copilot by default. I share my neovim between different devices with and without copilot access. Thus it pops up on every completion request to login to copilot, which initially let me drop the plugin.


Edit found the issue on my side. cmp-copilot started copilot in the background, even if I did not source/setup cmp.

For loading it only on certain devices, I wrote the following code to just include the my config module based on the hostname.

local function get_hostname()
    local f = io.popen ("/bin/hostname")
    local hostname = f:read("*a") or ""
    f:close()
    hostname =string.gsub(hostname, "\n$", "")
    return hostname
end

if get_hostname() == "HOSTNAME_WITH_COPILOT" then
    require "config.general.copilot"
end

dj95 avatar Nov 29 '24 17:11 dj95

see my setup that is losely based on what I used to have with supermaven and since then moved (can't get supermaven to work as source for blink)

plugin via plugins/copilot.lua

M = {
	"zbirenbaum/copilot.lua",
	cmd = "Copilot",
	event = "InsertEnter",
	config = function()
		-- only setup if global state control value is set ( and it's not by default )
		-- just an extra ensure check as setup is meant to only be called by toggling func
		local is_off = vim.g.copilot_enabled
		if not is_off then
			-- setup own false augroup to prevent inbuilt teardown from erroring out due to lack
			-- of internally set up augroup
			-- (setup's not been called yet. We are killing it right away in the womb xd)
			vim.api.nvim_create_augroup("copilot.client", { clear = true })
			-- wrap things down forcefully using inbuilt client function
			require("copilot.client").teardown()
		else
			local copilot = require("copilot")
			copilot.setup({
				suggestions = { enabled = false },
				panel = { enabled = false },
			})
		end
	end,
}
return M

setup toggling command in my user_commands.lua

-- custom toggle for AI
local toggleCopilot = function()
	local copilot = require("copilot")
	local client = require("copilot.client")
	-- toggle global variable for stop condition
	-- first toggle sets the none existing variable to true
	vim.g.copilot_enabled = not vim.g.copilot_enabled
	-- stop or start copilot
	local noti = require("notify")
	local noti_opts = { title = "Copilot", icon = "", timeout = 1000, hide_from_history = true }
	if vim.g.copilot_enabled then
		-- spin up lsp from scratch ang get client setup and attached
		copilot.setup()
		client.setup()
		noti("ON", "info", noti_opts)
	else
		-- detatch first to prevent lsp spamming it's own notifications when teardown is called
		client.buf_detach()
		-- destroy microsoft XD
		client.teardown()
		noti("OFF", "error", noti_opts)
	end
end

	api.nvim_create_user_command("ToggleCopilot", toggleCopilot, { range = false })

then we just set up a binding as we need

local map = vim.keymap.set
-- AI
map("n", "<leader>S", "<cmd>ToggleCopilot<cr>", { desc = " Toggle Copilot" })

tom-gora avatar Mar 09 '25 14:03 tom-gora

Is the goal to simply not have the suggestions? If so you can use the auto_trigger config to achieve this. When set to false it will not attach to the buffer, and you can then execute :Copilot suggestion toggle_auto_trigger to toggle it on and use it, then toggle it back to deactivate it. I believe the toggle is on a per_buffer basis though.

If you are looking to minimize the startup time then I could submit a PR with the option to have it 'enabled manually only', as right now it is enabled upon loading. Though @zbirenbaum might lose it if I create yet another PR 😂😭

AntoineGS avatar Mar 19 '25 21:03 AntoineGS

This setup of mine works fine with LazyVim.

lua/config/options.lua:

vim.g.copilot_enabled = false -- sets the default

lua/plugins/copilot.lua:

return {
    "zbirenbaum/copilot.lua",
    cmd = "Copilot",
    build = ":Copilot auth",
    config = function()
        Snacks.toggle({
            name = "Github Copilot",
            get = function()
                if not vim.g.copilot_enabled then -- HACK: since it's disabled by default the below will throw error
                    return false
                end
                return not require("copilot.client").is_disabled()
            end,
            set = function(state)
                if state then
                    require("copilot").setup() -- setting up for the very first time
                    require("copilot.command").enable()
                    vim.g.copilot_enabled = true
                else
                    require("copilot.command").disable()
                    vim.g.copilot_enabled = false
                end
            end,
        }):map("<leader>ux")
    end,
}

samyakbardiya avatar Apr 02 '25 16:04 samyakbardiya

Just call the plugin command to disable copilot after loading the plugin.

require("copilot.command").disable()

or

vim.cmd('Copilot disable')

mrjohannchang avatar Aug 08 '25 11:08 mrjohannchang