How to clear highlighting created by LSP function definition search in Neovim?

304 views Asked by At

I have very recently added my first LSP configuration to my Neovim setup. This is how it looks:

local on_attach = function(_, _) 
    vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, {})
    vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, {})
    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, {})
    vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, {})
    vim.keymap.set('n', 'gr', require('telescope.builtin').lsp_references, {})
    vim.keymap.set('n', 'K', vim.lsp.buf.hover, {})
end

Through this, I have used the gD shortcut (g, Shift+d) which highlights all the occurrences of a function. However, I have been unable to escape or clear this highlight. Can anyone help me?

I tried :q and :close but it closes the Neovim window I am working in. Escape does nothing and the only way out of this currently is to completely exit Neovim and reopen it.

1

There are 1 answers

0
Kyle F. Hartzenberg On BEST ANSWER

To clear highlighting, you need to issue the command nohlsearch, or the abbreviated equivalent, noh.

I map this command to Ctrl+l as it's the same key combination used to clear the terminal. You can remember the first letters of each key (c + l) as a mnemonic for clear (clear terminal, clear highlighting etc.).

To set this mapping, add the following line to your configuration:

vim.keymap.set({ "n" }, "<C-l>", "<cmd>noh<CR>") -- clear highlighting