If I want to copy text from a file in vi to another file, I have to highlight the text, Control-Shift-C it, quit the first file, open the second, and then paste it via Control-Shift-V. It feels like there must be an easier way to do this - that is, keyboard commands only. Any suggestions?

3 Answers 3


While editing a file you can use :e filename to open another file and :rew to return to the original file like this:

  1. open original file vi foo
  2. yank text e.g. yy (yank a line)
  3. open a second file to edit ESC :e bar
  4. put your text p (then save :w)
  5. go back to the first file ESC :rew
  • personally I like using vim's tabs... e.g. :tabe filename then using gt to switch tabs. also you can enter visual mode with v then use y to yank multiple lines. or use m'y (mark for beginning of yank) and yy to yank multiple lines. Aug 21, 2010 at 2:17

vim can be set up with X clipboard support. Pass --with-x=yes to configure if your distribution does not bundle vim with X support. By default the X clipboard is available as the * buffer and the X cut board (selection) is available as the " buffer, so selecting a region and typing "+y will yank the selection to the X cut board, and "*y will yank to the X clipboard. Similarly, "*p will paste from the X clipboard and "+p from the X selection.

You can set the default (anonymous) buffer in vim to be the X clipboard by running set clipboard=unnamed. Then you can just y and p from the X clipboard directly.



  1. Open your file: vi foo
  2. In your file, open the second: ESC :open bar
  3. Return to the first: ESC :prev
  4. To copy the content of the file: ESC :1,$ y
  5. To go the next file: ESC :n
  6. Paste it: ESC p

Maybe there's a shorter way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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