I have neovim 0.6.1 configured with some LSPs and using the default configurations found here: https://github.com/neovim/nvim-lspconfig#suggested-configuration. Some of the key bindings start with <space>, such as vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts). I assumed <space> meant the space bar, but that does not work. For example, with the rename key binding if I press SPACE-r-n I end up just replacing the char under the cursor with n… as expected with just r-n. How do I use these key bindings that start with <space>?
The vim.lsp.buf.rename() command works correctly if I call it manually… just to rule that out right away. The issue is with not being able to use the key binding. Also, the other key bindings from the default config that do not start with <space>, such as g-d work just fine.
Reading :help mapleader led me to try :let mapleader = "<space>", but that is not working. I then tried adding vim.g.mapleader = "<space> " and then vim.g.mapleader = " " to my init.lua, but the shortcut defined with <space>rn in the suggested configuration is still not working.
OK, narrowing it down even more. If I do :noremap <space>rn :lua vim.lsp.buf.rename()<CR> during a session the rename keybinding works correctly. What could cause the lua keybindings with <space> to not take effect in my init.lua, but the similar ones adjacent to it to work just fine.
I did, but it seems I was misled into thinking gd was working because it did something similar, but when I run the command manually it is a bit different. So, it seems that entire block of commands is not working.
-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
Having a hard time debugging though since I don’t know lua or about nvim coding.
Note: you must pass the defined on_attach as an argument to every setup {} call and the keybindings in on_attach only take effect on buffers with an active language server.
But I’m not sure how to do that. I thought maybe this loop was doing that:
-- Use a loop to conveniently call 'setup' on multiple servers and
-- map buffer local keybindings when the language server attaches
for _, lsp in pairs(servers) do
require('lspconfig')[lsp].setup {
on_attach = on_attach,
flags = {
-- This will be the default in neovim 0.7+
debounce_text_changes = 150,
}
}
end
And I have this array set: local servers = {'ccls', 'gopls', 'pylsp'}
That solved it! Turns out the default config for nvim-cmp also called setup and wiped the on_attach callback. The readme even mentions to look out for this, but it was hard to find within the default setup from another plugin. Thanks for all the help.
I had code copied from different example configs calling the setup method on the same servers more than once. Make sure this method is only called once per server and consolidate all necessary code from other calls.