sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #479: Replace with NO Typing

 tip karma   Rating 0/14, Viewed by 2726 

created:   May 23, 2003 10:45      complexity:   intermediate
author:   johnmaxa      as of Vim:   5.7

Often I replace one word with another.  This is much faster than the substitute command and requires no typing.

For example, to change badName(...) to goodName(...) (with the mappings below):

1.  Put the cursor anywhere on goodName and type:  gy

2.  Move the cursor to badName and type:  gp

That's it!

If there are more than one badNames, type *N before typing go, then n gp to change the next one.

I mapped go to put a space after the word, but someone clever might be able to combine them.


Here is how I mapped the commands:

"replace word with register b WITH SPACE AFTER word
noremap go lb"bPldwi <Esc>hbye

"replace word with reg b WITHOUT SPACE after word
"(lb so no move to previous word, but gives error at end of line)
noremap gp lb"bPldwhbyw

"copy this word to register b for replacing with go and gp
noremap gy lb"bye

 rate this tip  Life Changing Helpful Unfulfilling 

<<Copy the search results into clipboard | editing files on an ftp server listening  on a non-standard port >>

Additional Notes

jallen at cashsystemsinc.com, May 23, 2003 11:05
I have always wanted to be a clever guy, so I took a shot at combining them right after I wrote that.  Try this

noremap gp heli x<Esc>b"bPldwxbyw

and you can us gp for both words with and without spaces.
Anonymous, May 24, 2003 20:35
"  COMMENT:   'gp' is already assigned by vim
nmap gw lb"wPlcw<Esc>bb
"     Word from register w replaces the word under cursor.  Use with 'gy'
"     After replace, reposition to previous word for possibly another replace.
nmap gy lb"wye
"     Yank word into register w.
"     ----------------------------------------------- Thanks to vimmers.
Anonymous, May 25, 2003 4:13
I usually search for "badName", then do a "cw" (Change word), type "goodName", and then use the "." (repeat last action) to substitute further occurences..

If the cursor is not at the beginning of "badName", I can always do "bcw" (back-a-word + change word), or "viwc" (change visual-inner-word)

Seems good enough! :-)
yeti at physics.muni.cz, June 1, 2003 1:49
I don't quite understand what this is good for unless you are lucky and have goodName somewhere, where you can jump to really fast. And even then, jumping is context switching and thus slow (on the human's side).

When there's only one thing to change, I do `ctN' and type `good' -- or do `cw', type `good' and Tab to complete it, depending on the situation.
When there's a few of them, I use `.' to repeat it.
When there's many of them, I do `%s/\<^R^W\>/goodName/gc' or whatever regexp and flags are appropriate.
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.
Sponsored by Web Concept Group Inc. SourceForge Logo