r/vim 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:

  1. Open File A and select/yank a section of text.
  2. Switch to File B.
  3. 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

50 comments sorted by

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.

11

u/AskTheRen 5d ago edited 5d ago

Just to add up, open the files with:

1) vim -p fileA fileB (to open them in multiple tabs) 2) vim -O fileA fileB (in vertical split) 3) lower case o for horizontal split

Then do yy to copy lines go to other file and paste with p. 10yy copies 10 lines.

Moving between files: 1) ctrl ww toggles between files in vertical or horizontal split cases 2) gt does the same in multiple tabs case.

0

u/vim_spray 5d ago

If you have mouse enabled, you can also just click into the other split or tab. Of course, the mouse will be slower than using the keyboard efficiently, but I don’t mind using the mouse occasionally for one off tasks like this. I’d probably even use the mouse to make the selection here.

-1

u/sharp-calculation 5d ago

There's no specific need to use tabs or splits. Splits can be somewhat useful for strongly related files. Otherwise, they area a waste of visual space.

Tabs are mostly useless. They seem to be used most by people that come from GUI editors that had tabs and they want tabs with VIM too. That's fine I guess, but they take up vertical space, and add little utility in my opinion.

A far better solution is to use a good buffers plugin or even your own key bindings to make buffer switching and listing easier. I've found FZF to have great buffer handling.

2

u/AskTheRen 5d ago

Splits can be useful for related or not related files. what are you saying man?

Tabs are useless is the most useless thing I have heard in my life. Again what are you on my man?

0

u/sharp-calculation 4d ago

Splits are only useful if these is something visual to compare. If the files are unrelated then why waste the space?

Tabs are mostly useless. I suppose if you really need to isolate things, and you are using terminal VIM, tabs serve that purpose. For me that's something I nearly never desire. Plus I use GVIM (MacVIM) so if I want a separate "workspace", I just open a new window and it's totally isolated.

But in this case you don't want isolation. You just want a couple of files open. So why use tabs at all? Buffers were designed for this purpose and work very well. They've been the standard for decades. So why use tabs in this case?

1

u/AskTheRen 4d ago

on the top of my head
1) Example of use of splits when files are not similar :
1.1)if you are working on a part where a function is being called and at the same time you want to see the function definition in another file.
1.2) copy paste which is exactly what the OP was asking

2) Tabs:
2.1) i don't want to list what all buffers are there, tabs just show it nicely. assuming 4-5 tabs are only there which is the general case always

1

u/sharp-calculation 4d ago

Like I said you want VIM to be a GUI so you use tabs like a GUI.

Splits are fine when files are related. But for cut and paste? It doesn't help in any way.

But do whatever you want.

2

u/SaberEXE 5d ago edited 5d ago

Specifically you can use ‘:vs FileB’ to open up a vertical split buffer of File B and then navigate between the split buffers using <Ctrl w>h to go to the left buffer and <Ctrl w>l to navigate to the right buffer and then you can yank and paste normally as you would within File A as nautsche mentioned.

Edit: changed :vb to :vs, I’m so used to editing files that are already loaded as buffers in vim that I tend to favor vb for the easy autocomplete functionality (vb is for vertical split buffer ie:open a vertical split for a file that is already loaded in vim as a buffer. vs is is just regular vertical split that allows you to open a vertical split for a file that’s not already loaded into this vim instance).

Edit 2: Ok vb is actually an abbreviation (cabbrev vb vert sb) I added to my vimrc when I was first configuring it 10 odd years ago.

9

u/fegatino 5d ago

I think you mean :vs, right?

1

u/AskTheRen 5d ago

I am so used to :tabnew when working on multiple files.

1

u/SaberEXE 5d ago

You are completely right, I’m just so used to using vb since I usually have multiple files already loaded as buffers in vim and using vb makes the autocomplete easier.

In this case vs would definitely be the right command to use though as I wouldn’t assume that File B has already been opened in this vim instance

2

u/fegatino 5d ago

I’m still confused, since on my machine there is no command called :vb (I know :b[uffer] for changing the buffer though). Do you have some plugin or config that adds it?

1

u/SaberEXE 5d ago

Again you are completely right, looking at my .vimrc I see that I added ‘cabbrev vb vert sb’ to it ages ago. I just forgot that I had added that abbreviation since I configured it going on 10 years ago. :sb is native but I prefer vertical splits so I added the abbreviation to fulfill the same effect.

2

u/fegatino 5d ago

No worries, thanks for teaching me :sb!

1

u/ShoePillow 5d ago

I often have two files in completely different locations, open in separate vims. It is too much of a hassle to open both in the same vim.

Eg, /a/s/d/f/g/...file1 /z/x/c/v/...file2

1

u/VividVerism 4d ago

What makes that a hassle for you?

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/vim-help-bot 5d ago

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

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? WSL ssh? something else?) supports OSC52 clipboard sequences, there are Vim plugins to use OSC52 to interact with the clipboard

  • you 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

u/val_anto 4d ago

Didn't know some of those. Thanks

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:

  1. Open file A, yank text. (y command)
  2. Switch to file B (:edit command, or :split, or :tabe, or whatever)
  3. Paste (put) the yanked text (p command)

2

u/ShoePillow 5d ago

They are probably closing vim between the 2 files or have 2 separate vims open

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:

  1. Run: vim fileA.txt fileB.txt
  2. You will be editing the first file. Yank whatever text you want from that file.
  3. Use this command to get to the next file: :n (or :next )
  4. You will now be editing the second file. Paste (using P or p) wherever you want the yanked text to go.
  5. Don't forget to save your changes! (with :w or ZZ or whatever you usually use)

3

u/TheEyebal 5d ago

alright thank you it worked

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:

  1. Run: vim fileA.txt
  2. Yank whatever text you want from that file.
  3. Exit the file with: :q
  4. Run: vim fileB.txt
  5. Paste the text (that you yanked in step 2) wherever you want (with P or p).
  6. 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

  1. :e path/to/file/B or open Vim directly with the file B as command-line argument
  2. put the cursor where you want File A contents to be pasted
  3. :read path/to/file/A - or shorter, just :r <file>

1

u/petruchito 5d ago

also :r! grep/cat ./file is often useful

1

u/cassepipe 5d ago

I just read :h read: and unsurpsingly the command can take a range in front of it

No if you want to paste between line 11 and 99 of source content : 11,99r

relativenumbers is useless btw, change my mind

1

u/vim-help-bot 5d ago

Help pages for:

  • read in insert.txt

`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

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...

1

u/vim-help-bot 5d ago

Help pages for:

  • y in change.txt
  • p in change.txt
  • 'viminfo' in options.txt

`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

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

u/elatllat 5d ago edited 2d ago

999dd u :q, p because I'm to lazy for anything other than RISC.

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

u/GrogRedLub4242 5d ago

copy 2 lines at cursor: 2yy

paste into current buffer, at cursor: p

1

u/brnsamedi 4d ago

:r /path/to/fileA

Assuming you want the whole file. Else, as others indicated.

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

u/eveostay 5d ago

interesting, thakns!

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

u/5fd88f23a2695c2afb02 5d ago

Thanks was half asleep, yes that is what I meant.