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
While editing a file you can use :e filename
to open another file and :rew
to return to the original file like this:
- open original file
vi foo
- yank text e.g.
yy
(yank a line) - open a second file to edit
ESC :e bar
- put your text
p
(then save:w
) - go back to the first file
ESC :rew
-
personally I like using vim's tabs... e.g.
:tabe filename
then usinggt
to switch tabs. also you can enter visual mode withv
then usey
to yank multiple lines. or usem'y
(mark for beginning of yank) andyy
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.
Sure:
- Open your file:
vi foo
- In your file, open the second: ESC :open bar
- Return to the first: ESC :prev
- To copy the content of the file: ESC :1,$ y
- To go the next file: ESC :n
- Paste it: ESC p
Maybe there's a shorter way?