Search code examples
neovimgithub-copilot

activate copilot for only one directory


If it matters, I'm using neovim.

I have a directory /home/laurent/dev containing my development files. I want Copilot to be activated only on that directory (and its subdirectories).

The reason is that, in other directories, I have personal data. I don't want to use copilot on personal data.

quasi duplicate: Can you make GitHub Copilot opt-in on a per-project basis? Here I'm asking for neovim instead of visual studio.


Solution

  • I don't have coopilot, so you need to test it for yourself, but in general this is how I'm auto running commands in buffers with specific path.

    In neovim you can use lua version of autocmd. Bellow you have 2 of them.

    One is to run :Copilot enable in your ~/dev/ directory.

    
        -- Force enable
        vim.api.nvim_create_autocmd({ 'BufEnter' }, {
            pattern = '/home/laurent/dev/*',
            -- you could optionaly replace with
            -- pattern = vim.fn.expand('~') .. '/dev/*',
            command = "Copilot enable"
        })
    
    

    Second is optional. :Copilot disable in place with your secrets.

    
        -- Force disable
        vim.api.nvim_create_autocmd({ 'BufEnter' }, {
            pattern = '/home/laurent/private/*',
            command = "Copilot disable"
        })
    
    

    edit:

    @Laurent mentioned in the comment, a solution that will disable every buffer and then enable ~/dev/. Here is the code for it.

    
        -- Force disable everywhere
        vim.api.nvim_create_autocmd({ 'BufEnter' }, {
            pattern = '*',
            command = "Copilot disable"
        })
        
        -- Force enable ~/dev/*
        vim.api.nvim_create_autocmd({ 'BufEnter' }, {
            pattern = vim.fn.expand('~') .. '/dev/*',
            command = "Copilot enable"
        })