Okay, the reason :noremap <C-/> :nohlsearch<CR> often doesn't work is because many terminal emulators don't transmit the Ctrl-/ key combination in a way that Vim reliably recognizes using the standard <C-key> notation. The / character itself can sometimes interfere or be mapped differently at the terminal level when combined with Ctrl.
The most common and reliable alternative that works across most terminals is Ctrl-_ (Control-Underscore).
The Solution:
Use Ctrl-_ in your mapping:
nnoremap <C-_> :nohlsearch<CR>
Explanation:
nnoremap: This specifically maps the key combination in Normal mode and ensures the mapping is norecursive (meaning it won't re-trigger other mappings). It's generally safer than noremap which maps across Normal, Visual, Select, and Operator-pending modes. Since :nohlsearch is typically run from Normal mode, nnoremap is preferred.
<C-_>: This is the notation Vim uses for Ctrl-_. Many terminals send the same (or a compatible) sequence for Ctrl-/ and Ctrl-_. Using Ctrl-_ is the conventional way to map this function.
:nohlsearch<CR>: This is the command to turn off the current search highlighting, and <CR> simulates pressing Enter (Return) to execute the command.
How to Verify:
If you want to be sure what your terminal is sending, you can do the following in Vim:
- Enter Normal mode.
- Press
Ctrl-V. (This tells Vim to insert the next character literally).
- Now, press
Ctrl-/.
Vim will likely display something like <C-_> or potentially something else entirely if your terminal handles it unusually. If it shows <C-_>, then mapping nnoremap <C-_> ... is definitely the way to go.
Where to put the mapping:
Add the line nnoremap <C-_> :nohlsearch<CR> to your Vim configuration file:
- Vim:
~/.vimrc
- Neovim:
~/.config/nvim/init.vim or ~/.config/nvim/init.lua (adjust syntax for Lua)
After adding it, save the file and either restart Vim or run :source % (if you have the config file open in Vim) to apply the changes.