History
Article Edit this page Discussion

Search and replace the word under the cursor

From Vim Tips Wiki

Jump to: navigation, search

Tip 464 Previous Next Created: April 27, 2003 Complexity: intermediate Author: maurice Version: 6.0


With this mapping in your vimrc, you can easily enter a command to substitute all occurrences of the word under the cursor:

:nnoremap <Leader>s :%s/\<<C-r><C-w>\>/

Given this mapping, if the cursor is on the word foo and you press \s (assuming the default <Leader>), you will see:

:%s/\<foo\>/

If you now type bar/g and press Enter, you will change all foo to bar. The \< and \> ensure that only complete words are found (the search finds foo but not food).

Alternatively, you could use this mapping so that the final /g is already entered:

:nnoremap <Leader>s :%s/\<<C-r><C-w>\>//g<Left><Left>

If you only have a few occurrences to change, you might prefer a manual technique which does not require a mapping.

  • Put the cursor on foo.
  • Press * to search for the next occurrence.
  • Type cw (change word) then bar then press Escape.
  • Press n (move to next occurrence) then . (repeat change).
  • Repeat last step.

[edit] References

[edit] See also

[edit] Comments

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pixar
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Dark Shadows
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for collaboration of the month.

Send this article to a friend
"Search and replace the word under the cursor"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation