11

For example, when I yank some text from vim, then :wq, and open a new file. When I try to paste the text I just yanked, it doesn't work. So how to yank text between files?

1
  • I'm confused what's the problem?
    – FDinoff
    Jul 30, 2013 at 22:03

4 Answers 4

13

use "+y to send the yanked text to the clipboard, and "+p to paste it into the new file.

So if I have a file named A that contains abcdef and I (in visual mode) select def and then press "+y while in normal mode. I have just copied def to the system clipboard. Now I can open up a file named B and (while in normal mode) press "+p the text def will be pasted into file B.

5
  • Note that this only works when your copy of Vim is compiled with clipboard support. Not sure? Try :echo has('clipboard'). If 1, you have it. If 0, you don't.
    – ravron
    Jul 30, 2013 at 21:54
  • @Riley, you can also find out with :version and then see if clipboard has a little plus sign (+) in front of it. Nov 27, 2013 at 4:26
  • definitely, but I find that it's a bit of a pain to try to read through that feature list. Your call though!
    – ravron
    Dec 2, 2013 at 0:42
  • when I go to normal mode I lose the highlighted text. Mar 12, 2020 at 20:07
  • fyi on ubuntu you need to now (20.0) install vim-gtk to get the clipboard option
    – Goblinhack
    May 28, 2021 at 8:06
9

Don't quit the editor after writing with :wq

Instead, just write the file with :w and then edit the new file with :e file. Or Edit both at the same time by splitting with :sp file and ^W to switch between the screens.

3
  • if you have X11/windows, you could use "+y or "*y to yank. and same register to paste.
  • don't do :wq and then vim newfile. do :e newfile in same vim. then you could yank and paste between buffers. just press y and p
  • if you work on tty or via ssh, and don't want to do (2) either, you could save yanked part to a file, in newfile read the file.

I recommend the (2) option.

2
  • What's the difference between :e and :o?
    – OneZero
    Jul 30, 2013 at 21:54
  • @OneZero did you try :h them or test the both? open your vim, then press Q, now you type e or o, you see difference.
    – Kent
    Jul 30, 2013 at 22:04
2

Vim has the capability of viewing multiple files at once. Use the :sp *file_name* "split" or :vs *file_name* "vertical split" commands to view another file in the same terminal.

Navigation between open file windows is simple: press Control_key + wand then either 'h', 'j', 'k', or 'l' to move the cursor to the file window to either the left, the bottom, the top, or the right of the current window. Simply 'yank' the text that you mean to copy in one terminal window, move to the terminal window containing the file that you mean to copy into as described above, and 'put' the text at the cursor location as usual.

By the way, you may use the :wqa command to close all open terminal windows and write changes made to the open files to the disk.

Not the answer you're looking for? Browse other questions tagged or ask your own question.