D. Ben Knoble's answer provides the correct features to look into. Here's a solution that takes that knowledge and gives you some easy key mappings.
First, here's how regular
* works with
hlsearch enabled (see Ben Knoble's answer for that):
Now the new feature that we will create will do the following. In the next recording, we select some text with VISUAL mode, then hit the
enter key and it will highlight all matches, like so:
It is also useful for selecting whole lines and finding matching lines! In the next recording I pressed capital
V (shift+v) to enter VISUAL LINE mode and select the current line, then I hit the enter key, and it highlighted all three identical lines:
Note, because this hooks into vim's built-in search feature, you can use
p to navigate to the next/previous matches just like with
To have this functionality, add this mapping to your
init.vim in neovim):
" highlight the visual selection after pressing enter.
xnoremap <silent> <cr> "*y:silent! let searchTerm = '\V'.substitute(escape(@*, '\/'), "\n", '\\n', "g") <bar> let @/ = searchTerm <bar> echo '/'.@/ <bar> call histadd("search", searchTerm) <bar> set hls<cr>
When you have text selected, this mapping will programmatically add the selected text as the last thing you search for (same as if you had used the
/ key and inputted in manually).
If you have
hlsearch enabled, the new search text will be highlighted.
I like to toggle the search highlight, so I made ctrl+c toggle it in NORMAL mode (otherwise ctrl+c does not do anything in NORMAL mode, so now ctrl+c will be useful).
When I press ctrl+c repeatedly, it toggles
The mapping for that is:
" Give ctrl+c a job when it is otherwise being wasted!
" Now it toggles `hlsearch` while in NORMAL mode:
nnoremap <silent> <c-c> :if (&hlsearch == 1) \| set nohlsearch \| else \| set hlsearch \| endif<cr>
# will move your cursor, which you may not like. A lot of the time I would like to see matches, but without the cursor moving. So I mapped the
<enter> key in NORMAL mode to search for the current word (very much like
*) but without the cursor jumping.
This is what it looks like when I move to a word, and hit the enter key:
Notice the cursor stayed where it was! In my example, the
on instances are close to each other, but if they were far apart, it could be annoying if the whole screen jumped to a new location when I didn't want it to.
And here's the mapping!
" Put <enter> to work too! Otherwise <enter> moves to the next line, which we can
" already do by pressing the <j> key, which is a waste of keys!
" Be useful <enter> key!:
nnoremap <silent> <cr> :let searchTerm = '\v<'.expand("<cword>").'>' <bar> let @/ = searchTerm <bar> echo '/'.@/ <bar> call histadd("search", searchTerm) <bar> set hls<cr>
You can run Neovim inside VS Code with the
Neo Vim extension. This runs neovim in the background, and you can use the above features right inside VS Code, and also take advantage of all of VS Code's other GUI features.
For example, in the next recording, I selected a line with capital
V, and highlighted the matching identical lines with
enter, and finally navigated to them with
Notice the VS Code GUI including the minimap! With VS Code you'll also have the nice intellisense tooltips with markdown rendering, git GUI, and everything else (many extensions).
Ironically, also notice VS Code's own highlighting (in blue) of the word that is under the cursor! That feature is the whole reason this StackOverflow exists! But now you can do even more with it than just visual matches (navigate to them, and more than just words).
Vim and Neovim are really powerful once you learn them, and especially learn how to script them. They can do so much more with scripting. (And even more with VS Code features in the mix!).
Hope that helps!