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.
Related
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.
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.
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
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.
I've got some method calls all over the place in a large file, I'd like to match these lines, select and then yank them as one so I can put them all in a single place.
I can find all the lines I want with :g/>set but how do I visually select each line?
You can't have multiple visual selections in Vim.
But you can clear a register and append all the matching lines to it:
:let #a = ''
:g/>set/y A
then create an empty buffer (or navigate to an existing one):
:vnew
and paste from register a:
"ap
But you probably want something like TagList or TagBar.
edit
:[something]y a
means "yank into register a".
:[something]y A
means "append to register a".
What I usually do is :
Remove all the lines without the pattern :v/pattern/d
Select the whole new file with ggyG
Paste somewhere the result with p
Use undo a few times with u to get the file back to its initial state
This is a bit cumbersome, I would welcome a simpler solution.