sponsor Vim development Vim logo Vim Book Ad

basic Tip #497: Replacing a string with default buffer contents

 tip karma   Rating 5/6, Viewed by 1055 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   June 27, 2003 12:27      complexity:   basic
author:   Adam Wolff      as of Vim:   6.0

It's common to replace a given string in a file with something that appears elsewhere in the file. Often I don't think to put the replacement string in a named register, so it's hard to delete the text I'm replacing without replacing what's in the default (@") copy register -- :let is too cumbersome for this. (BTW, is there a way to issue a command like 'd' or 'c' *without* replacing what's in @"?)

Anyway, I've been using this map
vmap R :<BACKSPACE><BACKSPACE><BACKSPACE><BACKSPACE><BACKSPACE>:let @9=@"<CR>gvx"9P

which replaces what's highlighted in visual mode the contents of ""

It's ugly though (all the backspaces are necessary to delete the default  :'<,'> that shows up when you start a command in visual mode.)

I find this useful enough that I use it, but what I really want is something similar where I could type say
  R3w
(where R is remapped to my special command) that replaces the next three words the contents of @"

 rate this tip  Life Changing Helpful Unfulfilling 

<< This is the hyperlinked html format of the Vim Reference Manual. | Completation using the syntax file >>

Additional Notes

niklasl, June 28, 2003 3:44
Afaik, Vim already works like your mapping. If you press 'p' when in visual mode, the selected text is replaced by the contents of the default register  ('"<chosen_register>p' works as well). The deleted text is yanked as if deleted normally.
Anonymous, June 28, 2003 9:42
"_d  deletes to the black hole register and does not change any normal register
See :help "_
Maurice, June 28, 2003 18:26
And you can replace these <backspace>'es with one <c-u>
Anonymous, June 30, 2003 11:54
black hole buffer. just what I was looking for. gracias
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.net Logo