When I use g*
or *
or g#
it will trigger a search for the word under the cursor. However, the cursor moves to the next/previous occurrence of that word. Is there a way to search for the current word without having the cursor moving away?
It is annoying because often I want to press
*
:.,+5s/foo/bar/g
But this problem forces me to do
*
*
(I want to skip this)To search using Vim/vi, for the current word: In normal mode, you can search forward or backward. One can search forward in vim/vi by pressing / and then typing your search pattern/word. To search backward in vi/vim by pressing ? and then typing your search pattern/word.
To delete a word, position the cursor at the beginning of the word and type dw . The word and the space it occupied are removed. To delete part of a word, position the cursor on the word to the right of the part to be saved. Type dw to delete the rest of the word.
Is there a way to search for the current word without having the cursor moving away?
The whole point of *
, #
, and friends is made pretty clear in the documentation: "search forward" or "search backward". Your problem seems to be that you use those commands not for their intended purpose but for a side effect, presumably highlighting all occurrences of the word under the cursor.
Since there's no built-in command for that you will need to map it yourself:
nnoremap <key> *``
nnoremap <anotherkey> #``
...
Instead of pressing * to fill the search pattern copy the word directly to command line using CTRL-R CTRL-W. I.e.:
:.,+5s/<C-R><C-W>/bar/g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With