Search for word under cursor

search for word under cursor vim
vim search multiple words
vim search whole word
vim word under cursor
vim search case sensitive
vim highlight word under cursor
vim search highlighted text
vim search for symbol

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

  1. *
  2. :.,+5s/foo/bar/g

But this problem forces me to do

  1. *
  2. then shift+* (I want to skip this)
  3. my search and replace.

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> #``
...

Searching | Vim Tips Wiki, Searching for the current word. In normal mode, move the cursor to any word. Press * to search forwards for the next occurrence of that word, or press # to search  Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)

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

Search for word under cursor, 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  Search Word Under Cursor. While in isearch, press Ctrl+w to select more strings to the right of cursor. The following commands do isearch but uses the word the cursor is on, saves you typing. Alt+x isearch-forward-symbol-at-point 【Alt+s.】 → Search the “symbol” under cursor, with boundary check.

Changing the functionality of * / # / g* etc. to not jump (especially without a given [count]) is a frequent request. Therefore, a plethora of plugins changes Vim's default behavior, often together with other search-related commands. My SearchHighlighting plugin is one such plugin. (The plugin page has links to many alternative plugins that could also try out.)

@romainl's accepted answer is a minimal solution with some drawbacks (e.g. flickering of the screen, possible change of window viewport) that most plugins will avoid.

how can i search word that is under the cursor in VI, Hello all i know the standard way to search in Vi with "/" and "?" but is there any way to perform search on word that is under the cursor ? | The UNIX and Linux  * - search for word currently under cursor g* - search for partial word under cursor (repeat with n) ctrl-o, ctrl-i - go through jump locations [I - show lines with matching word under cursor Search and replace

I use the below mapping to search the current word and keep cursor's posistion:

nnoremap <expr> * ':%s/'.expand('<cword>').'//gn<CR>``'

The extra benefit is we got matches count too.

vim tips and tricks: searching, search for word currently under cursor g* - search for partial word under cursor (​repeat with n) ctrl-o, ctrl-i - go through jump locations [I - show lines with  Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. (but not search!) the word under cursor. Ask

You could map:

nnoremap # #N
nnoremap * *N

How to find a Word in Vim or vi text editor, Explains how to find a Word in Vim or vi text editor under a Linux, FreeBSD, macOS, In normal mode, move the cursor to any word say 254. CMD+E - copies the word under cursor CMD+F - bring up find in local file dialogue Enter - er you know what this means . Substitute CMD+F for CMD+SHIFT+F to find in all files in project (or whatever search range you specify)

How to highlight/match (but not search!) the word under cursor, For a quick solution, try this: :nnoremap <F5> :match StatusLineTerm /<C-R><​C-W>/<CR>. This uses Ctrl-R Ctrl-W to insert the word under the  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.

Search for a word under cursor / General / Sublime Text, Is there a keyboard shortcut or plugin to search for other occurrences of a word under cursor, like vim * and # commands? The standard method in emacs can be cumbersome in comparison. C-s C-w, appends the rest of the word under the cursor to the search pattern. So using M-b to return to the beginning of word and C-s C-w emacs searches the next occurrence of the word. Here are some ways of implementing Vim’s ‘*’ and ‘#’ in Emacs.

Faster Grepping in Vim, Search for the word under the cursor. This searches for the text under the cursor and shows the results in a “quickfix” window: " bind K to grep  The third mapping searches for the whole word under the cursor (like *) in any of the files in the current directory. The fourth mapping searches for the whole word under the cursor (like *) in any of the files in the same directory as the current file. On a Windows system, 'grepprg' may be set to use the findstr utility.

Comments
  • I fail to see the connection between step 1 and step 2.
  • Thank you I believe you answered my question. There are no built in function for this but it can be mapped into my .vimrc .