Replace word with what's ready to put in vi/vim? - vim

For example, I have:
rm -f $(NAME).elf $(name).d
where the second 'name' is a typo, and should also be 'NAME'.
So on 'N', I did yw, and then moved to 'n'.
I realised that if I now hit cw to change it, I'll be in insert mode - but I thought ah well, it's only one extra key (ESC) and then I can just p the corrected version in.
But in that case, p did exactly what u would have done, it put 'name' back. I guess because the 'old text' is loaded into the register on change too, which I didn't realise.
Is there a "replace with register" command that can be applied to a word/letter/etc.?

after you yw on NAME and moved to name you can do:
viwp
or golf abit: vep

I need this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.
This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.
Your example
With NAME yanked into the default register, and the cursor on the n, just use gre. gr invokes the command, and e is the motion that covers the entire name to be replaced.

Yet another way:
ciw<Ctrl-r>0
or
ce<Ctrl-r>0
This would allow for repeating with . without the use of any plugin, as you mentioned on the comments that you are currently avoiding them.

You could simply change the case of name with gUiw.

Related

In vim, how to paste without copy selected content when select some content before

I usually want to replace some content in vim. After copy, I select the content to be replaced, then paste content I copied. But then the content to be replaced will be copied to clipboard, and I can't replace next place.
I hope content in clipboard don't change when I paste on some selected place.
Short answer: "0p (instead of p)
This question sort of has an answer here, but I will add more details.
The :registers, command will show you what the registers are currently storing.
(use :help registers for more information)
Explanation:
Using the double quote (") before a yank or put command tells vim you want to use a specific register. The command "xp (where x is a number (0-9), or a letter (a-z)) says you want to put the data that was stored in register x.
In your case, you want to retrieve from register 0, because that was what you most recently yanked. (Every yank you do automatically fills register 0 with the text, as well as the unnamed register that is used by default with each yank and put)
You could also choose a named register to store your data in. I will choose letter 'h', for example.
To yank: "hy. To put: "hp.
Now if you use the :registers (or :reg) command, you will see that you have text stored in your "h register.
(Here is a question that explains registers very well)
I need this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.
This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.

Vim: Have a paste operator (similar to c, d or y)

I'd like to have an operator-pending mapping for paste that works in a way similar to how d or c work.
Something so that I can do pi" which would mean "paste inside quotes".
Is there already a plugin for this or would I have to make one? If there isn't I'll make it, but I was curious in case I could save myself some work. :)
Of course I would make it using Kana's vim-operator-user if I do make it myself.
Thanks for your help. :)
I need this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.
This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.

Vim command to paste over highlighted word

I've only recently learned about use v command to mark a section I want to copy or cut. This is great!
But I'd like to use the v command to make the section I want to replace.
For example:
$another_obj->method_in_obj
$self->method_actually_in_obj
I want to mark "another_obj" and overwrite "self" with "another_obj". Basically I want to tell vim that, I want to take these 4 characters (self) and turn them into these 11 characters (another_obj) and have vim adjust my line accordingly.
So the final should be:
$another_obj->method_in_obj
$another_obj->method_actually_in_obj
Thanks!
#Kent has outlined the basic approach: You yank the source word, then paste over the selected replacement. The downside is that you can do that only once, because that action overrides the original yanked text! To avoid that, you need to specify the black-hole register.
I do this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.
This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.
cursor on [a]nother, pressing yw
then j cursor is on [s]elf, press:vawp

Vim replace inside quotes

Many times I find that I will will want to yank the content between quotes and paste them inside another set up of quotes. For example, take this code for instance.
var foo = 'bar',
baz = 'buz';
I would normally do a yi' inside of 'bar' to yank the word bar.
How do I replace buz with my yank? I know one option is to do a di'"0P, I just wonder if there is an easier solution I'm overlooking.
With your cursor anywhere on the word buz, vi'p to visually select inside the quotes and then put the contents of the most recent yank.
I need this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.
This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.
also note after you replace the content in visual mode,the content being replaced will now in you register. command :reg will show it.
In your example, command p will paste the buz in the editor.

Vim copy and paste

My previous question seems to be a bit ambiguous, I will rephrase it:
I have a file like this:
copythis abc
replacethis1 xyz
qwerty replacethis2
hasfshd replacethis3 fslfs
And so on...
NOTE: replacethis1, replacethis2, replacethis3, ... could be any words
How do I replace "replacethis1","replacethis2","replacethis3",.. word by "copythis" word by using minimum vim commands.
One way I can do is by these steps:
delete "replacethis1","replacethis2","replacethis3",.. by using 'dw'
copy "copythis" using 'yw'
move cursor to where "replacethis1" was and do 'p'; move cursor to where "replacethis2" was and do 'p' and so on...
Is there a better way to do this in VIM (using less number of vim commands)?
Since you changed your question, I'd do it this way:
Move to the first "replacethis1" and type cw (change word), then type "copythis" manually.
Move to the next "replacethis", hit . (repeat last operation)
Move to the next "replacethis", hit .,
and so on, and so on.
If "copythis" is a small word, I think this is the best solution.
The digit needs to be included, and there could be more than one instance per line:
:%s/replacethis\d/copythis/g
Given that "replacethis[1-3]" can be arbitrary unrelated words, the quickest/simplest way to do this globally would be:
:%s/replacethis1\|replacethis2\|replacethis3/copythis/g
(Note that you need to use \| to get the pipes to function as "or". Otherwise, vim will look for the literal | character.)
I've been struggling with this for a long time too, I think I just worked out the cleanest way:
Use whichever command is cleanest to put copythis into register r:
/copythis
"rye
Then go to the replacement and replace it with the contents of r:
/replacethis
cw<CTRL-R>r<ESC>
Then you can just n.n.n.n.n.n.n. for the rest of them, or if they're wildly different just go to the beginning of each and hit .
The key is replacing and pasting in one step so you can use . later.
:%s/copythis/replacethis/g
To replace all occurrences of copythis with replacethis. Or you can specify a range of line numbers like:
:8,10 s/copythis/replacethis/g
Note, the /g on the end will tell it to replace all occurrences. If you leave that off it will just do the first one.
create this mapping:
:map z cwcopythis^[
( ^[ is the escape character, you can type it in vim using Ctrl+V Ctrl+[ )
go to each word you want to replace and press z
if u need to do essentially the same action multiple times - swap 1st word of one line with second word of the next line, I say you could record a macro and call it whenever you need to
Have you tried string replacement?
%s/replacethis/copythis
A host of other parameters are possible to fine-tune the replacement. Dive into the Vim help for more details. Some more examples here.
You can remap e.g. the m key in normal mode to delete the word under the cursor and paste the buffer: :nnoremap m "_diwP.
Then you can just copy the desired word, move the cursor anywhere onto the to-be-replaced word and type m.
EDIT: Mapping to m is a bad idea since it is used to mark locations. But you can use e.g. ; anyway.

Resources