Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 6 down vote accepted

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
share|improve this answer
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. – xenoterracide Aug 21 '10 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.

share|improve this answer


  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?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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