sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #464: search & replace the word under the cursor

 tip karma   Rating 40/20, Viewed by 2646 

created:   April 27, 2003 13:42      complexity:   intermediate
author:   maurice      as of Vim:   6.0

I have this usefull mapping in my vimrc:

nmap ; :%s/\<<c-r>=expand("<cword>")<cr>\>/

Now, if you see a word 'foo' which should be replaced, you only have to put the cursor on it and type ';'.
Then the command-prompt has already the annoying :%s/\<foo\>/ and you just have to enter your new word.

For information:

:help <c-r>
:help expand

 rate this tip  Life Changing Helpful Unfulfilling 

<<XSLT Mappings I use to speed up developing XSLT files. | generic xml imap to make an element of any word you type >>

Additional Notes

gmann, April 27, 2003 14:55
I am curious how this is more beneficial than cw ?  Am I missing something?  One more keystroke?  Thanks for any advice.
maurice, April 27, 2003 15:08
As mentioned, this opens the prompt for search & replace, which applies to the whole buffer and not only for the one occurrence of the word.
Anonymous, April 27, 2003 17:14
your script is interesting, but non-standard, that's why it's not very useful
Anonymous, April 27, 2003 23:09
'cw' only changes the word at the current position, '%s' does theat for all occurances in the file.
Well, what is 'standard' ?
'non-standard' may also be useful. I often found tips and scripts here rated useful, but not for me.
So what about senseless 795,32 colour schemes which may be 'standard' but useless ?

;-)) Thomas
feral at firetop dot comthingie, April 28, 2003 1:47
Another way of accomplishing basically the same thing:

If you do not specify the pattern for <kbd>:s</kbd> it will use register / (current search string), as such I've been <kbd>*:%s//whatever</kbd> whenever I have wanted to change all occurrences of a word.

I had not really thought of using a nmap to construct the :substitute .. (By the by I have been mulling over menu entries)

Related, use <kbd>&</kbd> as part of the replace string to modify your search string  (see :h sub-replace-special)

Also related consider this nmap that uses the contents of the unnamed register: <kbd>nmap ; :%s/<c-r>"/</kbd>

Thanks for the tip Maurice!

p.s. just pretend the html kbd tags did what they are supposed to and denote what to type (=
[email protected], April 28, 2003 2:44
Great idea!
BTW you can use nmap ; :%s/\<<c-r><c-w>\>/ as well...
niklas, April 28, 2003 10:07
Nice tip. One thing though: nmapping ";" disables the default command for that key. That is "repeat last jump to character using f, F, t or T" (for which "," repeats in opposite direction). See ":help f" and the 5 related commands below that one in "motion.txt".
Maurice, April 28, 2003 11:06
Oh.. never used this jumps. But after trying them out it seems that I will do this in the future. Very cool :)
Guess I have to map this replace-stuff to another key...

What is this non-standard-thing? Thought that if its listed in the vim help, it is ok :)
gmann, April 30, 2003 20:06
Thanks for the clarification Maurice.  The tip is quite nice, standard or not.  I like the jumps as well (vi = very impressive).  This would also be nice to map along with the AllBuffers method, can you think of how I can map this, so that I can do the same thing across all buffers which I have open?  Something like the following (which doesn't work)
map <M-t> :call AllBuffers(":%s/\<<c-r><c-w>\>/
Maurice, May 1, 2003 8:22
I don't know about a function 'AllBuffers'. But I think what you want is the bufdo-command.
Your mapping would be something like this:

map <M-t> :bufdo :%s/\<<c-r><c-w>\>/
[email protected], May 21, 2003 1:55

Great macro, thanks.

I have a small question:

When entering this line as it is to my .vimrc file, the last '/'  "messes" the syntax of the configuration file.
IE: it changes the colors of some of the following commands. This is probably due to the special role of '/' .

I "solved" it by removing the '/' (and I enter it manually whenever I need to use the macro).

Any better advice ?

Regards,

Jonathan Orlev.
[email protected], May 21, 2003 2:01
Anybody knows how I performe it, if I mark with ":v2w"  2words and put it directly in the command line for searching and replacing?
Maurice, June 1, 2003 9:32
> Anybody knows how I performe it, if I mark with ":v2w"  2words and put it directly in the command line for searching and replacing?

For example with the help of a register:

vnoremap <your-key> "ty:%s/<c-r>=@t<cr>/

But note that this will alter the register 't'.
If you use XFree, where the clipboard is automatically set by selecting some text, you could also use the '*'-Register:

vnoremap S <esc>:%s/<c-r>*/
[email protected], October 19, 2003 16:24
nnoremap ; :%s/\<<c-r><c-w>\>/

works well for me, it selects the current word and only replaces whole word matches, for example:

$xml_parser = xml_parser_create();
....

if the cursor was on $xml_parser and you typed ';'
only $xml_parser would be replaced, not xml_parser_create()
[email protected], May 9, 2006 22:03
This is the function I have been eagerly looking for! Thanks!
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
    stats
Sponsored by Web Concept Group Inc. SourceForge.net Logo