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?

Nope
  • 34,682
  • 42
  • 94
  • 119
OneZero
  • 11,556
  • 15
  • 55
  • 92

4 Answers4

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.

Alexej Magura
  • 4,833
  • 3
  • 26
  • 40
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.

evil otto
  • 10,348
  • 25
  • 38
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.

Kent
  • 189,393
  • 32
  • 233
  • 301
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.