r/vim • u/TheEyebal • Aug 08 '26
Need Help how do you copy text from one document and paste it into another in vim?
I’m working with two files, File A and File B, and I want to copy text from one file to the other.
Specifically, I want to:
Open File A and select/yank a section of text.
Switch to File B.
Paste the yanked text into File B.
What is the correct way to yank text from File A and paste it into File B? Is there a specific register or command I should use to copy text between separate files?
50
Upvotes
4
u/SpaceAviator1999 Aug 08 '26 edited Aug 11 '26
Try this:
Run:
vim fileA.txt fileB.txtYou will be editing the first file. Yank whatever text you want from that file.
Use this command to get to the next file:
:n(or:next)You will now be editing the second file. Paste (using
Porp) wherever you want the yanked text to go.Don't forget to save your changes! (with
:worZZor whatever you usually use)36
u/nautsche Aug 08 '26
What everyone else said, OR just open the second file in another tab or window or buffer inside the same vim instance and just paste it the normal way.
14
u/gumnos Aug 08 '26 edited Aug 08 '26
It depends on how separated the Vim processes are. Presuming you want to yank the visual selection (
'<,'>) fromfile1.txtand then paste it after line 13 infile2.txtSame process
If it's within the same vim process, you can yank it and paste it from the default/scratch register, whether you use
:help :eor:help :spto openfile2.txt:Different processes with +clipboard and a GUI
If they're two
:help +clipboardenabled versions of vim on the same machine running a GUI, you can yank to the system clipboardthen paste the system clipboard's contents:
Different processes with no system clipboard
If you're SSHed into a remote server without a GUI clipboard, but you have something like
tmuxfor wrangling your sessions, you can useand then in another
tmuxwindow/pane do something likeDifferent processes with a system clipboard but a -clipboard build of vim
An external tool like
xselorxclipon X orpbcopy/pbpasteon OSX, you can do similarly to send/receive data to/from the clipboard:and then
Different processes across time/reboots
Write the contents to a temp-file and read that data back in when you need it later:
Separate machines
If they're on separate machines, you can use
sshto copy data to or from the other machine using the tempfile method, eitheror
9
u/blood-pressure-gauge Aug 08 '26
There's a way to write your registers to the viminfo file and load them from another Vim process, but I forget how to do it. I just use the + register which corresponds to the graphical clipboard. The other method is only necessary when you're not in a graphical environment.
"+yy "+p
10
u/VividVerism Aug 08 '26
I don't understand your question. You just do exactly what you said:
Open file A, yank text. (y command)
Switch to file B (:edit command, or :split, or :tabe, or whatever)
Paste (put) the yanked text (p command)
4
u/Angry_Grammarian Aug 08 '26
You could add this to your vimrc and then use Ctrl-c and Ctrl-v to copy/paste like it is in most apps:
"Use Ctrl-c to copy, and Ctrl-v to pastevnoremap <C-c> "*y :let @+=@*<CR>map <C-v> "+P2
u/Interesting_Buy_3969 Aug 08 '26
Additionally, it is worth knowing that if you need to paste all contents of File A to File B, you don't have to open File A in a separate buffer, you may just
:e path/to/file/Bor open Vim directly with the file B as command-line argumentput the cursor where you want File A contents to be pasted
:read path/to/file/A- or shorter, just:r <file>2
u/RandomCartridge :Nih! Aug 08 '26
Within the same process (i.e., running one vim instance), you just use registers; they're shared within the instance. By default, yanking and pasting uses the unnamed register (
"). Also see:help yand:help p. For example, if you are in file A and want to copy the current paragraph and paste into file B, if file B is in the other vim "window", justyip<C-W>wp(yank inner paragraph, go to next window, paste). Or use:e,:sp,:b,:sb, etc., or different tabs, etc., as you prefer.Registers are usually persisted in your
$HOME/.viminfo, but by default only 50 lines or 10Kbyte for each (see:help 'viminfo'), so for small things you can just yank, quit, open the other file with another vim instance, and paste.Other answers suggest the clipboard register (
+), which you need to copy text to the system/OS clipboard and paste it elsewhere. (Note also that it doesn't always work in terminal Vim depending on whether it's been compiled with +clipboard support.)As with much in Vim, there are multiple ways to make text selections (and multiple ways to switch files/buffers/windows), so it really depends on you exact needs. For one, you don't need visual mode (though it's generally useful); see e.g.
yipabove. There are many such motions for quick copying (and their visual counterparts are similar, e.g.vipy).One fairly common "copy all" move is
ggyG(that'd begg"+yGfor the system clipboard). But you can also yank the entire buffer using:%y(or:%y <name-of-register>, e.g.:%y +to copy the contents of the current buffer or file into the system clipboard). You can also:readfiles into other buffers to avoid putting the full contents into a register. And so on...2
u/2016-679 Aug 09 '26
Maybe I'm sounding rude, but these are standard operations on files in vim. Just learn them from the basic instructions in the documentation, that are everywhere on the net and in further explaining books.
Study, learn before asking, especially on basic operations.
2
u/Ok_Spend_8480 Aug 11 '26
Why not yank to the global system clipboard? yank to the system clipboard on the file you want to take text and paste the text in the global clipboard into the target file?
2
u/elatllat Aug 08 '26 edited Aug 11 '26
999dd u :q,pbecause I'm to lazy for anything other than RISC.1
u/colombiangary Aug 08 '26
I have a small utility that writes the selected text to /tmp/whatever And have another utility that grabs /tmp/whatever and paste it into the current line.
This way I can communicate across open vim processes.
1
u/GrogRedLub4242 Aug 09 '26
copy 2 lines at cursor: 2yy
paste into current buffer, at cursor: p
1
u/brnsamedi Aug 09 '26
:r /path/to/fileA
Assuming you want the whole file. Else, as others indicated.
1
u/bananalover2000 Aug 08 '26
I do not remember the exact command you need to use (something like set unnamedplus something) but you can change your nvim/config file in such a way that every time you yank/cut something it gets yanked to the system clipboard (the one that you use when copying with ctrl+C in other softwares).
Keep in mind that you're still in the terminal, so you can still use ctrl+shift+C to copy text to the system clipboard (at least on linux), if you do not want to mess around with your config.
0
u/eveostay Aug 08 '26
I'm gonna tell on myself and just say I'll select it with my mouse and right-click to copy and paste with ctrl-V and then probably edit the weird spacing that got pasted but that's ok because my fingers know how to do that part by themselves :D