Paste multiple times - vim

What is the best way replace multiple lines with the contents of the clipboard?
The problem I'm having is when I yank a line and paste it over another line the "yank" is replaced with the line I just replace. Now, if I want to replace another line with the same line I have to go back up and yank it again.
There's got to be a better way to do this.

I have this in my .vimrc:
xnoremap p pgvy
(note: this will work only with the default register, but this mapping is easy to remember). Writing a more elaborate version would be possible. Also, you still can use P to get the old behaviour.

"0 should have the contents of your yank. It's a bit more tedious to type, but "0p should do what you want.
Alternatively, don't select-and-replace the old lines up front. If you find those lines with a search, just hit n. over and over (after an initial p), then when they're all pasted, do ndd followed by as many n.s as necessary.
The biggest mental switch I've needed to make when moving to Vim is to figure out how to apply group edits sequentially. I.e. rather than doing a bunch of edits on a line and then doing a bunch of the same edits on another line, I'll do the first edit on a bunch of lines (using . to great effect), then the second edit on a bunch of lines, etc. Alternatively, the use of macros may help as they are fantastic, but sometimes a little more tedious to get working correctly with "complex" changes.

I often use another registry, copy the line you need to some named registry "ay and then paste from there "ap

When you paste over a selection in Vim it will replace the default register with the contents of the selection. If pasting over a selection is wiping out the contents of the clipboard register then very likely you have the following line in your .vimrc
set clipboard=unnamed
One option is to remove that and use the explicit clipboard register "+
Another option is to use any of the other explicitly named registers (a-z). After the first paste yank the line back into "c for example and then use "cp to paste from there on out.

Instead of using copy/paste, it is often better to use a text object command such as ciw to change the inner word. This method has the advantage of being easily repeatable using the . repeat command.
yiw Yank inner word (copy word under cursor, say "first").
... Move the cursor to another word (say "second").
ciw<C-r>0 Change "second", replacing it with "first" ( is Ctrl-R).
... Move the cursor to another word (say "third").
. Change "third", replacing it with "first".

use np where n is the number of how much time you want to paste the lines eg 3p will paste 3 lines.

Related

Vim: How do I paste a column of text (from clipboard) after a different column of text?

I'm testing proxies with my script that looks like that:
$proxy = "http://name:pass#133.245.122.91:80";
$proxy2 = "http://name:pass#133.245.229.241:80";
$proxy3 = "http://name:pass#133.245.113.197:80";
...
$proxy100 = "http://name:pass#133.245.212.197:80";
I get new proxies by email so can I copy new proxies and insert it instead of the old ones by Vim:
"http://name:pass#133.245.122.91:80";
"http://name:pass#133.245.229.241:80";
"http://name:pass#133.245.113.197:80";
...
"http://name:pass#133.245.212.197:80";
Right know I'm doing it as was described on this page How do I paste a column of text after a different column of text in Vim?
Use visual block (ctrl-v) to cut the letter column. Then move to the
first line of the number column. Move to the end and make one space.
Then paste the letter column.
I'm curious, how it can be done without extra step, just paste data from clipboard?
The short version: you can't. There are ways around it, but they aren't necessarily simpler. Longer version follows.
Vim has three ways of marking regions of text: linewise (you start this mode when you press V), characterwise (triggered when you press v), and blockwise (when you press Ctrl-v). The marked region is copied to a register, and this register has an attribute, the "type", that reflects the way you did the marking, linewise, characterwise, or blockwise. What happens when you paste from a register depends on this type.
Now, when you copy from system's clipboard the result is stored in the * register, and the type is always set to linewise. Thus you can't paste a column mode "without extra step". You can however set the type of the * register to blockwise before pasting:
call setreg('*', #*, 'b')
Thus, replacing the list of your proxies would go something like this:
copy the new list to clipboard, from the mail message
run :call setreg('*', #*, 'b') to set the type of the * register to blockwise
go to the old list, press Ctrl-v and mark it; assuming there's nothing else in the file aside from the proxies, a Vim golfer's way of doing that might be something along the lines of:
f" - go to the first "
Ctrl-v - start marking
?;Enter - go to the last ;
paste the new list over the selection, with "*p.
You can simplify the last step a little, by making the * and + registers always refer to the same value. To do that, add this to your vimrc:
set clipboard=unnamedplus,autoselect,exclude:cons\\\\|linux
With this setting the incantation becomes:
copy the new list from mail
run :call setreg('+', #+, 'b')
go to the old list and mark it with Ctrl-v as above
press p to paste the new list over it.
You don't need this dance if you have the new list in a file that you can open with Vim:
open the file with the old list
open the file with the new list in a separate copy of Vim
mark the new proxies with Ctrl-v and yank them with y
in the other Vim mark the old list with Ctrl-v and paste the new one over it with p.
This still involves using the system clipboard under the hood, but the second copy of Vim takes care of setting the type of the relevant register to blockwise.
I don't know any direct way to do this. If it is really important to you, you will probably need some set up before you do the actual editing, which only adds to the amount of typing you have to do (however you can add commands to your vimrc to make it permanent). You might set up some keyboard macro, or use the following map command:
:imap <CR> <Esc>j011lC
Now move to the first " sign and press C, then start pasting (only works in a terminal). Whenever you paste a newline, the map will move you to column 11 in the next line.
Remember to :iunmap <CR> when you are done.

Paste to end of the file in VIM without moving cursor

In a text document, I'm [visually or otherwise] selecting several lines, cutting them with d... I'd like to paste these lines to the end of the file without moving the cursor. Is there a relatively simple way to do this?
Use the Implicit Mark from Last Jump
You can use the implicit mark (e.g. ') to return your cursor to the location it occupied just before the last jump. For example:
Gp''
This will (G)o to the end of the file, (p)aste the contents after the last line, and then return to your position at the time you typed G.
There are a few ways:
Marks
Set a mark, do your paste, then jump back to the mark
m':$pu<cr>``
Visual mode
Visually select your lines, copy them, append, and then restore visual selection (optionally delete)
y:$pu<cr>gv
Append to the file
Visually select your lines, use :w to append to the file, and then reload the file. (Note: will move the cursor to the start of the visually selected lines)
:w >><cr>:e!
Create your own command/mapping
You can create your own command and/or mapping that will use winsaveview() and winrestview() to append then restore the cursor.
You can define a mapping which marks the current location, pastes at the end of the buffer using :$put then returns to the original cursor location using the mark.
This works because :put allows a line number prefix (the last line being representable as $). From :help put:
:[line]pu[t] [x] Put the text [from register x]
This would map it to <leader>p:
:nnoremap <leader>p :mark '<cr>:$put<cr>`'
It sets the ' mark at the cursor, pastes at the end, then returns to the ' mark with `
Depends on how you mean "without moving the cursor".
This will paste at the bottom of the current file and then allow you to continue where you cut the lines from.
split window with :split
move to bottom with shift+g
paste with p
close the duplicate split view (zz or :q)
If you dont like the split view, you can use the ctrl+o to jump back after G
move to bottom with shift+g
paste with p
jump back with ctrl+o

VIM: Is there a way to make vim not copy whenever you delete text

I love vim, but one of my problems with it is that whenever I use x or d to remove text, it copies the deleted text into the buffer/clipboard.
More specifically, it's a problem when I have some text copied, and then I need to delete a second block of text before pasting the first text.
Is there a command that deletes text but does not copy it? If not, is there a custom command for this?
Thanks!
Use black hole register
"_d
Setup your own mapping in vimrc like ,d to save some typing
There are more ways than one to solve the problem. One has already been suggested by Vimsha.
Another way to solve the problem is to copy your important text to a named register. Say you wanted to 5 lines of text to be deleted from one place and copied in a different place.
"a5dd will delete 5 lines of text and save them in the a register.
Do various other operations.
Now move to the location where you want to copy them.
"ap or "aP will copy the 5 lines of text from the a register to the current location.

How to copy/paste text from vi to different applications

Is it possible to copy/paste text without using :vs? If I have two vi windows open, I can copy/paste text with a mouse. How can I do it with a keyboard?
I found two existing questions that are similar to this, but neither one answers my question.
how to copy codes in vi to clipboard
Copy and paste content from one file to another file in VI
I'm sure there are many ways, but I do it using marks and registers.
Marks
You can place a mark anywhere in a file using m followed by the name of the mark you want to use.
You can use any letter between a and z (capital and lowercase) to name your marks.
You can go to the line that contains a mark with the ' key.
For example, mx marks a line with mark x and 'x moves the cursor to the line containing mark x.
You can go to the exact location of a mark using the backtick key: `
To yank from the current cursor location to the line containing mark x, for example, you would enter y'x
Registers
In order to use the clipboard, you need to use registers, which represent places you can store the text you yank.
Just like you can use different marks for each character, you can name the registers you yank text to.
You refer to a register by using the " key when yanking/putting.
For example "ay'x would yank the text between the cursor and the line containing x to register a.
The clipboard is represented by a special register: either * or + depending on your environment.
To yank the text between the cursor and the line containing mark x to the clipboard, enter the following: "+y'x
This says: use buffer + (the clipboard) to store the text between the cursor and the line containing mark x.
Once you do this, your text will be in the clipboard. You can use CONTROL-V to paste it into other apps.
NOTE: In some environments, the clipboard is represented by the buffer named *.
This may sound overwhelming, but once you get used to it, it's VERY powerful.
I use this hundreds of times every day.
If you're editing a file that has several key points of interest, you can mark each part of the file with different marks and quickly move your cursor between the code you need to edit.
Likewise, if you have several pieces of text that you need to repeatedly copy, you can store each one in a different register to make your pasting more efficient.
You can copy/paste by using the + register (read more: Accessing the system clipboard)
"+gyywill yank a line, and put it into the + register. You can paste in your other window with "+p in normal mode, or Ctrl+r + while in insert mode.
If you don't wish to use split windows, there really is no other way to paste between windows apart from using the system clipboard.
#up exhausted the subject. I can just add that most of the combination related is with associated with system key combination find you in config for Gvim (eg. windows mapping for CTRL+C CTRL+V etc. is in mswin.vim)

How to Delete (desired text), delete (undesired text), and paste (desired text) in Vim

I don't know if its a retarded problem but it's a funny dilemma. When I want to delete text that I want to place somewhere else, but that place has other bunch of text that I don't want, I would delete that text, but in the process I copy a new clipboard so the previously deleted text disappear.
Any suggestions to solve this?
A few possible solutions:
Delete the undesired text first :)
or
When deleting the desired text store it in a register other than the default register e.g. to delete the desired text to the end of the current line and store it in register b:
"bd$
Then delete your undesired text.
Then paste the contents of register b:
"bp
or
Delete the undesired text to the black hole register as suggested in the answer linked to by Yarek T using:
"_d
maybe this question might shed some light onto your problem. 54255
It uses the "black hole buffer" to delete lines without adding them to the yank buffer.
Another solution is to use the number registers. When you delete a chunk of text it is moved into register 1, the current contents of register 1 is moved into register 2, etc. The contents of register 9 are discarded. However this only works for changes longer than a line, so small deletes are not captured.
So you can delete the first region, delete the second region, then paste from register 2.
Personally I prefer to use registers a-z, but the numbered registers are useful if you delete some text and then realise you forgot to specify a register.
Do :help "1 for more information.
You can also see what is currently in all the registers, including 1-9, with :registers
Type:
:registers
And you'll get a list of registers that contain all previous deletions. You can always pick one to paste. E.g. for registers:
"1 Item1^J
"2 Item3^J
"3 Item2^J
pick the second one and paste it with:
"2p
Try the yankring plugin.

Resources