How to saved a text always in buffer in Vim> - vim

Actually I want to copy a selected text from file a in Vim and paste to multiple different parts of file b. But when I paste to file b, it just happened once and then buffer is empty. I want that part of text in buffer always as I have to paste it to some more parts and for this I have to go to file a every time I had to copy and then paste to that file b. Is there any way that I can save that text in buffer always?

You are mixing up vim terminology here. From the Vim Wiki:
A buffer is a file loaded into memory for editing.
What you mean by a buffer is actually a register. The default register gets overwritten by a lot of operations that delete text (e.g. x/c/d) because they store the deleted text in there. But you can yank 100K lines of text into a named registers like this:
"a100000yy
This yanks all these lines into register a.
Now go to file B and lets first delete the 100K lines you want to change. Put your cursor on the first line that needs to change and do:
100000dd
Then we will paste the lines from file A with:
"aP
Notice that that is a capital P.
For more information on the power of register check this link out: https://www.tutorialspoint.com/vim/vim_registers.htm

Related

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 can vim keep the content of register when pasting over selected text?

I have a line of text I have yanked yy. Now I want to use this text to replace lines at several other places. The trouble is that when I select V the line to be replaced, and paste p, the text that was selected is automatically yanked! That's what I don't want.
Changing the register does not work, because both the paste and the yank are done with the newly selected register.
What is the command to keep the content of the register when pasting over selected text?
Your original selection should remain in register 0. So you can move through the file and paste your yanked line over other lines using: V"0p
Each time you p over something it goes into the default register.
To work around this feature you have to use "_, "the black hole register", before you p. Here is a custom mapping I have in my ~/.vimrc:
vnoremap <leader>p "_dP
It deletes the selected content and drops it in the black hole register (this means that the selected text disappears forever) and puts the content of the default register in place of the previously selected text while leaving the default register intact.
I use it often when I need to replace a loooooooong url in a few places with another looooooong url and crafting a s// would be too cumbersome.

Paste multiple times

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.

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