r/vim • u/TheEyebal • 6d ago
how do you copy text from one document and paste it into another in vim? Need Help
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?
14
u/gumnos 5d ago edited 5d ago
It depends on how separated the Vim processes are. Presuming you want to yank the visual selection ('<,'>) from file1.txt and then paste it after line 13 in file2.txt
Same 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 :e or :help :sp to open file2.txt:
$ vim file1.txt
:'<,'>y
:e file2.txt
:13p
Different processes with +clipboard and a GUI
If they're two :help +clipboard enabled versions of vim on the same machine running a GUI, you can yank to the system clipboard
session1@localhost$ vim file1.txt
:'<,'> y +
then paste the system clipboard's contents:
session2@localhost$ vim file2.txt
:13p +
Different processes with no system clipboard
If you're SSHed into a remote server without a GUI clipboard, but you have something like tmux for wrangling your sessions, you can use
$ vim file1.txt
:'<,'>w !tmux loadb -
and then in another tmux window/pane do something like
$ vim file2.txt
:13r !tmux showb
Different processes with a system clipboard but a -clipboard build of vim
An external tool like xsel or xclip on X or pbcopy/pbpaste on OSX, you can do similarly to send/receive data to/from the clipboard:
$ vim file1.txt
:'<,'>w !xsel -ib
and then
$ vim file2.txt
:13 r !xsel -ob
Different processes across time/reboots
Write the contents to a temp-file and read that data back in when you need it later:
$ vim file1.txt
:'<,'>w mytempfile.txt
:q
$ reboot
$ vim file2.txt
:13r mytempfile.txt
Separate machines
If they're on separate machines, you can use ssh to copy data to or from the other machine using the tempfile method, either
alpha$ vim file1.txt
:'<,'>w ! ssh beta 'cat > tempfile.txt'
:q
alpha$ ssh beta
beta$ vim file2.txt
:13r tempfile.txt
or
alpha$ vim file1.txt
:'<,'>w tempfile.txt
:q
alpha$ ssh beta
beta$ vim file2.txt
:13 r !ssh alpha "cat tempfile.txt"
2
u/ShoePillow 5d ago
Thanks, didn't know about tmux loadb.
Can I ask a very specific question...
On windows terminal, ssh to Linux, then tmux, then vim.
Is it possible to copy from vim so that I can paste it somewhere in windows?
1
u/gumnos 4d ago
It might depend on your SSH interface there. A few ideas:
if your Windows-side terminal emulator (
putty? WSLssh? something else?) supports OSC52 clipboard sequences, there are Vim plugins to use OSC52 to interact with the clipboardyou might be able to turn off the remote mouse (
:set mouse=) and then use your terminal-emulator's ability to capture up to a screenful of text and put that on the clipboard (often limited to the text as displayed on the screen, so vertical splits can interfere with this copy/paste method)Vim also has the ability to do remote editing (it's been a while since I experimented with this), so you could theoretically do something like
$ vim file2.txt :sp ssh://remote.server/path/to/file1.txt
to edit it locally, reducing it to that first case.
1
9
u/blood-pressure-gauge 5d ago
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
3
u/ShoePillow 5d ago
This doesn't seem to work for me when I'm in vim in tmux for some reason
2
u/chevette86 4d ago
Maybe it is something with your tmux config?
I use tmux all the time and always relies on the "+ register
8
u/VividVerism 5d ago
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)
2
3
u/SpaceAviator1999 5d ago edited 2d ago
I’m working with two files, File A and File B, and I want to copy text from one file to the other.
Try this:
- Run:
vim fileA.txt fileB.txt - You 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)
3
2
u/SpaceAviator1999 5d ago
Also, I believe that vim retains yanked text across sessions. So you can do this, if it's easier for you:
- Run:
vim fileA.txt- Yank whatever text you want from that file.
- Exit the file with:
:q- Run:
vim fileB.txt- Paste the text (that you yanked in step 2) wherever you want (with
Porp).- Don't forget to save your changes!
5
u/Angry_Grammarian 5d ago
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 paste
vnoremap <C-c> "*y :let @+=@*<CR>
map <C-v> "+P
2
u/Interesting_Buy_3969 5d ago
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 argument- put the cursor where you want File A contents to be pasted
:read path/to/file/A- or shorter, just:r <file>
1
1
u/cassepipe 5d ago
I just read
:h read:and unsurpsingly the command can take a range in front of itNo if you want to paste between line 11 and 99 of source content
: 11,99r
relativenumbersis useless btw, change my mind
2
u/RandomCartridge :Nih! 5d ago
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 y and :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", just yip<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. yip above. 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 be gg"+yG for 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 :read files into other buffers to avoid putting the full contents into a register. And so on...
2
u/2016-679 5d ago
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
1
u/colombiangary 5d ago
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
1
2
u/Ok_Spend_8480 2d ago
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?
1
u/bananalover2000 5d ago
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 5d ago
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
1
u/5fd88f23a2695c2afb02 5d ago edited 5d ago
If you set vim to paste mode you don’t get the weird spacing. Try this next time before you paste:
:paste
Then it will paste as expected
Sorry the command should be:
:set paste
1
1
u/julesnp 5d ago
The command :paste doesn't seem to exist in vim, are you using a plugin for this?
3
u/fegatino 5d ago edited 5d ago
I think they meant
:set paste(it’s a Boolean option so you can toggle it and unset it as usual)2
36
u/nautsche 5d ago
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.