Take the 2-minute tour ×

Is there any way to copy all lines from open file to clipboard in VI editor. I tried yG but its not using clipboard to store those lines.

So is it possible?

share|improve this question
add comment

13 Answers

up vote 140 down vote accepted

You should yank the text to the * or + registers:



  • gg
    • gets the cursor to the first character of the file
  • "*y
    • Starts a yank command to the register * from the first line, until...
  • G
    • go the end of the file
share|improve this answer
Nice...I never knew about the <kbd> tag... –  Jason Punyon Oct 25 '09 at 4:42
How do you mark up the text so it looks like keys? I haven't seen that yet on SO. –  Jergason Oct 25 '09 at 4:44
Vim can only access the system clipboard if it is compiled with xterm_clipboard enabled. To find out whether you have this feature, run :version in vim, and look for the +xterm_clipboard flag. If it is preceded by a minus sign, then you won't have this functionality. If this is the case, you might want to compile vim yourself, or run gvim which usually has this feature enabled. –  nelstrom Oct 26 '09 at 15:12
@nelstrom: you are right its not installed .. I cannot recompile vim now... I will give it a try.. maybe later .. thanks everyone :) –  Xinus Nov 6 '09 at 15:56
The problem with this approach is that it moves the cursor and scrolls the window around. Twice. The other answer has far fewer keystrokes and drawbacks :) –  sehe Sep 13 '11 at 7:50
show 8 more comments

to yank all lines

share|improve this answer
+1 IMO much easier than accepted answer. 6 keystrokes to 4... –  Lieven Keersmaekers Nov 19 '10 at 8:22
Plus it won't change cursor location. –  progo Dec 3 '10 at 10:56
VIM says "E488: Trailing characters". However :%y worked. –  Gee Jul 26 '11 at 2:30
better than accepted answer –  Mustafa Kamal Jan 20 '12 at 9:30
I needed to set clipboard=unnamed, but then this worked great! –  cdunn2001 Mar 5 '12 at 19:20
show 4 more comments

The clipboard is buffer +. To copy to clipboard, do "+y[movement]. So, gg"+yG.

Similarly, to paste from clipboard, "+p

share|improve this answer
Just to add to this (4 years later...), this has a slight drawback in that your cursor position will jump to the top of the file. To avoid this (e.g. if you are mapping this to some shortcut in your vimrc), you can do something like: mqgg"+yG`q –  Tom Lord Jul 16 '13 at 15:08
add comment

(in reply to @rshdev, and to avoid having to recompile vim with +xterm_clipboard per @nelstrom in comments on OP)

there's a program called xclip that works like putclip on Ubuntu 11:

:%!xclip -sel clip

it's not installed by default. to install, use:

sudo apt-get install xclip
share|improve this answer
add comment

On Ubuntu 12 you might try to install the package vim-gnome

sudo apt-get install vim-gnome

I tried it, because

vim --version

told me that it would have the flag xterm_clipboard disabled (indicated by - ), which is needed in order to use the clipboard functionality.

-> installing the vim-gnome package on Ubuntu 12 also installed a console based version of vim, that has this option enabled (indicated by a + before the xterm_clipboard flag)

share|improve this answer
unix.stackexchange.com/questions/25965/… is there any other option anybody knows? –  Sandip Pingle Feb 6 at 10:51
add comment

on Mac

  • copy selected part: visually select text(type v or V in normal mode) and type :w !pbcopy

  • copy the whole file :%w !pbcopy

  • past from the clipboard :r !pbpaste

share|improve this answer
add comment

If you're using Vim in visual mode, the standard cut and paste keys also apply, at least with Windows.

  • CTRLA means "Mark the entire file.
  • CTRLC means "Copy the selection.
  • ESC means "De-select, so your next key press doesn't replace the entire file :-)

Under Ubuntu terminal (Gnome) at least, the standard copy also works (CTRLSHIFTC, although there doesn't appear to be a standard keyboard shortcut for select all (other than ALTE followed by A).

share|improve this answer
But if you aren't using mswin.vim, then ctrl-a increments the next number on the current line. –  Mark Rushakoff Oct 25 '09 at 5:41
@Mark, this behavior is from a standard Vim install so I assume that's the default. All bets are off if the environment is configured differently, though if that were the case, I suspect the OP would know what they're doing :-) In either case, OP stated in a comment they were on Ubuntu so the Windows part of my answer probably doesn't apply. The Gnome terminal stuff would. –  paxdiablo Oct 25 '09 at 5:47
add comment

:%y a Yanks all the content into vim's buffer, Pressing p in command mode will paste the yanked content after the line that your cursor is currently standing at.

share|improve this answer
add comment


:set go=a


See :help go-a:

'a' Autoselect:  If present, then whenever VISUAL mode is started,
 or the Visual area extended, Vim tries to become the owner of
 the windowing system's global selection.  This means that the
 Visually highlighted text is available for pasting into other
 applications as well as into Vim itself.  When the Visual mode
 ends, possibly due to an operation on the text, or when an
 application wants to paste the selection, the highlighted text
 is automatically yanked into the "* selection register.
 Thus the selection is still available for pasting into other
 applications after the VISUAL mode has ended.
     If not present, then Vim won't become the owner of the
 windowing system's global selection unless explicitly told to
 by a yank or delete operation for the "* register.
 The same applies to the modeless selection.
share|improve this answer
add comment

There wasn't a concept of "clipboard" in Bill Joy's vi so I don't think there is a built-in way to do it.

gVim's automatic copy-anything-highlighted-to-the-clipboard feature is easiest or use an external program via :!

For Cygwin's vim I use


Maybe Ubuntu has a CLI app like putclip??

share|improve this answer
great suggestion, thanks! there is such a program, called xclip. for details, I posted a separate answer (it would have been too confusing to read from a comment, without the extra newlines and code formatting). –  Stew Mar 2 '12 at 15:39
add comment

This is what I do to yank the whole file:

share|improve this answer
I'm afraid this does not answer the question, as you are yanking to the default register, not to the clipboard. –  Conspicuous Compiler Oct 21 '10 at 3:14
@ConspicuousCompiler it would if he had the clipboard set to copy the yanked text into + though. –  TankorSmash Jan 14 at 23:32
add comment

you can press gg to locate your curser to the start of the file,then press yG to copy all the content from the start to end(G located) to buffer.good luck!

share|improve this answer
add comment

Here's a map command to select all to the clipboard using CTRL+a:

" select all with control-a
nnoremap <C-a> ggmqvG"+y'q

Add it to your .vimrc and you're good to go...

share|improve this answer
add comment

Your Answer


