How to automatically close the split window after selecting the option from the reference list?

Hi there,

I have such configuration inside my neovim config.

// from nvim-lspconfig
...
vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts)
...

Whenever I press gr combination neovim creates a horizontal split and shows all the references to the selected object.

After selecting an option from the “reference window (split)” this split remains open and it forces me to manually move back to the split and close it manually.

It is possible somehow to auto-close the split after selecting an option?

Hey! I’m not sure it’s a really clean solution but something like this worked for me:

vim.api.nvim_create_autocmd("FileType", {
    callback = function()
        local bufnr = vim.fn.bufnr('%')
        vim.keymap.set("n", "e", function()
            vim.api.nvim_command([[execute "normal! \<cr>"]])
            vim.api.nvim_command(bufnr .. 'bd')
        end, { buffer = bufnr })
    end,
    pattern = "qf",
})

When you press “e” it opens the file and close the qf.

1 Like