Vim command to paste over highlighted word - vim

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

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.

Replace word with what's ready to put in vi/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.

Pasting inside delimiters without using visual selection

In Vim, let's say I want to replace the contents of one String with the content of another.
Original input
var1 = "January"
var2 = "February"
Desired output
var1 = "January"
var2 = "January"
What I would usually do is:
Move cursor to line 1
y i " (yank inner quotes)
Move cursor to the destination quote in line 2
v i " p (visual select inner quotes, paste)
While this works well, I generally try to avoid visual mode when possible, so I am not completely satisfied with my step 4 (v i " p).
Is there any way to specify a "destination paste area" without using Visual mode? I suspect it might be something chained to g, but I can't think of anything.
There are many ways of doing this however using visual mode is the easiest.
Use the black hole register to delete the content then paste. e.g. "_di"P
Do ci"<c-r>0. <c-r> inserts the contents of a register
Simply paste and then move over a character and delete the old text. e.g pldt"
However visual mode still has my vote. I find that the concerns most people have is that using visual mode + paste is that the default register is swap with the selected text and it doesn't repeat well. Good news everybody! The 0 register always stores the last yank. The bad news is visual mode still doesn't repeat well. Take a look at this vimcast episode, Pasting from Visual mode, for more information. It mentions a few plugin that help with this.
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.
It's not particularly pretty, but here goes:
Go to line one and yi"
Move to line two
Type "_di"hp
That deletes what's in the quotes, but sends the deleted text to a black hole register. Then it moves the cursor back one, and pastes what you yanked from line one.
All in all, you can start on the first line and type yi"j"_di"hp. Why is it that people find vim intimidating? ;)
Alternatively, yank the first line as normal, then drop to line two and type ci"<Ctrl+p> and select the previously yanked text from the menu.
Excerpt from my .vimrc:
" Delete to 'black hole' register
nnoremap <leader>d "_d
vnoremap <leader>d "_d
So, you just need to \di"P as your last step (assuming you use \ as a <leader>).
There is a plugin that addresses this problem precisely. It was presumably born out of the same feeling you're feeling, namely that Visual mode often feels less than ideal to advanced Vim users.
operator-replace – Operator to replace text with register content
With operator-replace installed the replacing goes like this.
Yank the word inside the quotes.
yi"
Move to the target line
j
Replace inside the quotes with the yanked text. (I've set up gr as the replace operator: :map gr <Plug>(operator-replace). Pick your own mapping.)
gri"
Check it out! This is part of the truly excellent textobj/operator frameworks. I couldn't work without them.

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.

How to efficiently paste over multiple lines again and again with the same text in Vim?

Objective
Yank a line and use it to overwrite some of the lines following it.
Assumption
It is preferable in this case to manually select the lines to apply the substitution to. In other words, automated find and replace is not desired.
Analogy
Think of this process as creating a “stamp” from a line of text and going through a list of items—each item being a line of text following the “stamp” line—and deciding whether that line should be overridden using the contents of the “stamp” or not (in the former case, replacing the line with the “stamp”, of course).
This last step of triggering the replacement of the line under the cursor with the contents of the stamp, should be as easy as possible; preferably, as easy as pressing . (repeat last change) or ## (execute the contents of macro register #).
Issue
The straightforward workflow is, of course, as follows.
Position the cursor on the line to be copied (using movement commands).
Enter line-wise Visual mode (via the V command).
Copy selected text (using the y command).
Manually position the cursor onto the line to be replaced (using movement commands).
Enter Visual mode again to select the text to be replaced (using the V command).
Paste over the selection (using the p command).
However, this approach does not work when the replacement has to be done multiple times. Specifically, replacing the text on step 6 overrides the (unnamed) register containing the line initially copied and intended to be used as a “stamp”.
What I have tried
I have tried using "_y to either yank or delete into the _ register, avoiding the loss of the contents of the stamp, but I am looking for something that ends up being quick and comfortable to type as I manually go through the list and apply replacements where I see fit.
What I would prefer not to use
I would rather not use macros or “remaps” for this, if I can help it.
Illustrative sample file
See the sample starting file below, followed by the desired final stage, for further clarity.
Sample file, starting condition
At this stage, I select the blueberry and make it my “stamp”.
blueberry
apple
banana
coconut
apple
banana
coconut
apple
banana
coconut
Sample file, desired final state
After having moved through the list, I have applied some replacements, “stamping” over some lines, making them the same as the “stamp” blueberry line.
blueberry
apple
banana
blueberry
apple
banana
coconut
apple
banana
blueberry
To make your workflow work as expected, you need to paste from the previous yank register "0, rather than the default register.
So use Vy (or yy, which is the same) to yank the first line as before, then position the cursor over the line you want to replace, and do
V"0p
this replaces the current line with the previously yanked text, but doesn't overwrite the yanked text. I hope I understood you correctly!
EDIT 1: repeating using a macro
I was surprised that this operation isn't repeatable using ., but this is presumably due to the use of visual mode. To repeat the operation using a macro, do this:
qqV"0pq
The macro can then be repeated by pressing #q or ##.
EDIT 2: repeating using .
Here's an attempt at making it repeatable using . by not using visual mode. After yanking the stamp line and moving the cursor, do this:
"_S<c-r>0<delete>
which uses the insert mode <c-r> command to insert the contents of register 0. Note that the <delete> is necessary because the stamp line contained a carriage return. If it did not (i.e. yanking using y$ rather than yy) the <delete> could be omitted.
I don't think you are going to reach your goal without at least a little bit of "remapping".
I've been using this one for a "long" time:
vnoremap <leader>p "_dP
p and P still work as usual and I simply hit ,p over a visual selection when I want to repeat the same paste later. You could also map a single function key to make the whole thing quicker.
Also, do you know about the c flag for substitutions?
:%s/coconut/blueberry/c
will ask for your confirmation for each match.
Many answers here outline the general keys or commands. I've turned them into my ReplaceWithRegister plugin, which also handles many corner cases, and allows quick repeat via the . command. I also use your described create stamp and replace technique often, and found my script indispensable. Should you not like it, the plugin page also has links to alternative plugins.
A really easy solution: just put this script in your .vimrc, then toggle off the "buffer-overwriting" side-effect behavior of the delete key by typing ,, (two commas) to enter "no side effects" mode.
In this mode your workflow now works exactly as you described: yank whatever you like, then select, paste, and delete freely and repeatedly -- your buffer always remains intact. Then type ,, again if you wish to restore vim's normal buffer-altering behavior.
The script is the accepted answer here:
vim toggling buffer overwrite behavior when deleting
One can resort to Ex commands to achieve the said workflow.
For a single substitution, yank the “stamp” line (with yy, Vy,
:y, or otherwise), then repeatedly use the combination of the
:put and :delete commands:
:pu|-d_
Like any other Ex command, this one can be easily repeated with the
#: shortcut (see :help #:) — unless another Ex command was issued
in the meantime (in which case that command would be repeated instead).
Of course, you can also record the above Ex command as a macro and
invoke it that way, too.
Starting with your cursor at the start of the line to be duplicated:
y$ to yank the whole line (excluding the linefeed).
j and k to advance to the next line to be replaced (repeating as needed)
Replace the line with your yanked text
C<c-r>0<esc>0 (first time)
. (subsequent times)
If there are more lines to be replaced, goto 2.
The cursor will remain in column zero after each step.

Resources