Copy all the lines to clipboard
Asked Answered
D

28

789

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

So is it possible?

Despairing answered 25/10, 2009 at 4:33 Comment(2)
Similar questions - https://mcmap.net/q/55275/-yank-entire-file & #3997578Kedah
If you're on Windows and yank doesn't work, see the 17th answer from the top: #1620518Scottiescottish
M
804

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

gg"*yG

Explanation:

  • gg to get the cursor to the first character of the file
  • "*y to start a yank command to the register * from the first line, until...
  • G to go the end of the file
Maudemaudie answered 25/10, 2009 at 4:37 Comment(19)
How do you mark up the text so it looks like keys? I haven't seen that yet on SO.Mango
Hello... its not working?clipboard data remains unaltered. when I type gg cursor goes to first line... but when I type "*yG ..cpu beeps at * ..so is it normal or some problem with my vim? I am on Ubuntu..Despairing
Do you have Deadkeys enabled? (e.g., when you hit 'a you get á) If so, you need to press space after ".Cruciferous
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.Pirozzo
@nelstrom: you are right its not installed .. I cannot recompile vim now... I will give it a try.. maybe later .. thanks everyone :)Despairing
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 :)Pratte
On Ubuntu, if your vim is compiled without the <code>+xterm_clipboard</code> flag, you can install vim-gnome which is compiled with this flag. (bugs.launchpad.net/ubuntu/+source/vim/+bug/837882)Arboreous
@AntonyThomas, for OS X you need to compile Vim with clipboard support, the easiest method I could find, is to use homebrew, with the following formula: brew install https://raw.github.com/Homebrew/homebrew-dupes/master/vim.rbDott
To enable xterm_clipboard flag in Ubuntu, install vim-gnome packageKine
To copy selected lines, select the lines with Shift+V and then do "*yKine
CMS - Your answer has "accumulated" a few caveats. Can I suggest these be edited into your answer? Also, does your answer apply to both vi & vim or just vim, as the commentor conspicuous-compiler suggested on ldigas answer (currently) below.Kedah
This answer works for Putty, whereas @Rook's answer doesn'tTransfigure
@Pratte the :y+ doesn't work in other scenarios like Vintageous in Sublime Text. gg"*yG works for me in that case.Onomatology
@BentOnCoding,what's the replacement of VIM?Ipa
As for Mac(High Sierra 10.13.6 (17G65)), I have vim without clipboard support, the '+' register working for me, so the code is gg"+yGSashenka
Everytime I use VIM I feel I am entering secret cheat codes <= <= X O => =>Cultigen
This only seems to get me a single line of text to the clipboard, not all text. I have +xterm_clipboard enabled in vim on Ubuntu 18.04. Why is this not working ever? It only copies and pastes line 358 of 363 lines... why that line?Marika
Without vim, c-a,c-c will work. Whats the point of vim, if I have to press this many keys.Francophobe
thanks a lot, I found a lot of approaches but this is the best, simple and easy!!!Micronutrient
H
948

Use:

:%y+

to yank all lines.

Explanation:

  • % to refer the next command to work on all the lines
  • y to yank those lines
  • + to copy to the system clipboard

NB: In Windows, + and * are equivalent see this answer.

If anyone is getting E850: Invalid register name error, try :%y"+ to copy all content to the clipboard and "+p to paste it (thanks to @deadLock)

Handcraft answered 25/10, 2009 at 4:40 Comment(21)
Plus it won't change cursor location.Darter
@guangnan: These registers don't exist in vi; only in vim. Try typing :help to ensure you're actually in vim.Utu
I needed to set clipboard=unnamed, but then this worked great!Phytogeography
@ConspicuousCompiler I have the error as Guagnan, and confirmed as VIM, not sure what to do :(Parisi
@ZachL You are likely running a version of vim without xterm clipboard support or connected via a session without clipboard support. You can see the options that were used when compiling vim by typing :version. Odds are, you'll need to install a different package with vim compiled with X support.Utu
I'm going to use this solution... but I'm going to make a keyboard shortcut to it. This is a better solution, but it's not easier to type. I've been gg"+yG ing for years now and never been a great solution for me. I'm going to add nmap <C-a><C-c> to my vimrc.Unreasonable
This worked after I installed the vim-gnome package as described in this answerSequela
Vim says E850: Invalid register name:(Guesthouse
Works fine for me on mac with VIM 7.3Husserl
This solution only works if vim was compiled with the clipboard option (vim --version will show +clipboard in that case). If it wasn't, Brian's solution (:%w !pbcopy) is a solution for Mac. Alternatives are available for other operating systems (e.g. xclip).Micmac
This answer worked for me - the accepted answer didn't. On OSX, vim 8.0.46Barranquilla
Can anyone kindly explain that when to use "+ and when to use + (as in this case) to refer to a register?Hilde
@Hilde "+ is a specific register, which means "the system clipboard register", so use "+ when you want to copy something to paste it outside of vim.Psychophysics
For easy copying, for your vimrc you can just do: nnoremap <leader>Y :%y+<CR> (uses <kbd>Space</kbd> + <kbd>Y</kbd> as that's what I set my leader to, change shortcut as you want).Psychophysics
Didn't work on CentOS 7 VPS from Vultr. I get E850: Invalid register name. vi --version shows me -clipboard but I don't know how to enable it and turn it to +clipboardWingback
If anyone gets E850 install sudo apt-get install vim-gnomeRebhun
This gets me the same 363 lines yanked and it will only paste line 358... WHY!?!Marika
If anyone getting E850: Invalid register name try :%y"+ to copy all content to the clipboard and "+p to paste itAllie
@deadLock, wow, solid comment. You can add that as an answer as it's very relevant for newer Vim versions, and it will give you more visibilityRemembrancer
Not working on my remote debian 10 when trying to get the copied lines to my local ubuntu 20.04... No errors either...Nutter
:'<,'>w !pbcopy - to copy a selected line on mac.Lupe
M
804

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

gg"*yG

Explanation:

  • gg to get the cursor to the first character of the file
  • "*y to start a yank command to the register * from the first line, until...
  • G to go the end of the file
Maudemaudie answered 25/10, 2009 at 4:37 Comment(19)
How do you mark up the text so it looks like keys? I haven't seen that yet on SO.Mango
Hello... its not working?clipboard data remains unaltered. when I type gg cursor goes to first line... but when I type "*yG ..cpu beeps at * ..so is it normal or some problem with my vim? I am on Ubuntu..Despairing
Do you have Deadkeys enabled? (e.g., when you hit 'a you get á) If so, you need to press space after ".Cruciferous
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.Pirozzo
@nelstrom: you are right its not installed .. I cannot recompile vim now... I will give it a try.. maybe later .. thanks everyone :)Despairing
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 :)Pratte
On Ubuntu, if your vim is compiled without the <code>+xterm_clipboard</code> flag, you can install vim-gnome which is compiled with this flag. (bugs.launchpad.net/ubuntu/+source/vim/+bug/837882)Arboreous
@AntonyThomas, for OS X you need to compile Vim with clipboard support, the easiest method I could find, is to use homebrew, with the following formula: brew install https://raw.github.com/Homebrew/homebrew-dupes/master/vim.rbDott
To enable xterm_clipboard flag in Ubuntu, install vim-gnome packageKine
To copy selected lines, select the lines with Shift+V and then do "*yKine
CMS - Your answer has "accumulated" a few caveats. Can I suggest these be edited into your answer? Also, does your answer apply to both vi & vim or just vim, as the commentor conspicuous-compiler suggested on ldigas answer (currently) below.Kedah
This answer works for Putty, whereas @Rook's answer doesn'tTransfigure
@Pratte the :y+ doesn't work in other scenarios like Vintageous in Sublime Text. gg"*yG works for me in that case.Onomatology
@BentOnCoding,what's the replacement of VIM?Ipa
As for Mac(High Sierra 10.13.6 (17G65)), I have vim without clipboard support, the '+' register working for me, so the code is gg"+yGSashenka
Everytime I use VIM I feel I am entering secret cheat codes <= <= X O => =>Cultigen
This only seems to get me a single line of text to the clipboard, not all text. I have +xterm_clipboard enabled in vim on Ubuntu 18.04. Why is this not working ever? It only copies and pastes line 358 of 363 lines... why that line?Marika
Without vim, c-a,c-c will work. Whats the point of vim, if I have to press this many keys.Francophobe
thanks a lot, I found a lot of approaches but this is the best, simple and easy!!!Micronutrient
E
112

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

Endgame answered 12/4, 2013 at 12:38 Comment(5)
Works smoothly. An explanation of the commands would be really nice.Ellita
@Geo pbcopy is a Mac utility. On Linux, xsel might work instead, or clip on Windows.Remaremain
This won't work if the file is longer than the height of your terminal.Showily
@SridharSarnobat :%w !pbcopy still works if the file is longer than the height of the terminal.Arsenite
Oh sorry, I meant over ssh where the client is a Mac and the server is Linux (which I assumed was what the OP was looking for but now that I think about it, that's an unrelated problem). In my defense, I'd avoid vim for copying and pasting for local files, but that's my personal taste. Good tips.Showily
D
95

Another easy way to copy the entire file if you're having problems using VI, is just by typing "cat filename". It will echo the file to screen and then you can just scroll up and down and copy/paste.

Dispatcher answered 14/11, 2016 at 17:17 Comment(5)
E603: :catch without :tryAplanospore
And if you want to do that without leaving vim, you can run the following command: :!cat %. And to copy the file's contents programmatically to the clipboard on Mac OS: :!cat % | pbcopyMislay
Needed to copy file from Ubuntu VM on Windows to Debian in GCP opened in browser window... :/ Variations of vim yanking did not work, this worked to paste to Windows then upload file to GCP.Mcalister
This is straight forward approach, I like it.Xanthate
"just" scroll up and down a thousand lines.Backcross
C
67

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

So, gg"+yG will copy the whole file.

Similarly, to paste from clipboard, "+p

Cruciferous answered 25/10, 2009 at 4:37 Comment(1)
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`qPickard
T
54

This is what I do to yank the whole file:

ggVGy
Transported answered 20/10, 2010 at 18:55 Comment(4)
I'm afraid this does not answer the question, as you are yanking to the default register, not to the clipboard.Utu
@ConspicuousCompiler it would if he had the clipboard set to copy the yanked text into + though.Lawless
@Lawless not the quickest (:%y) nor neatest (at least ggyG is better) method though.Decury
@ConspicuousCompiler what is the difference between default register and clipboard?Capri
R
49

You can use "cat" command to open file and use mouse to copy

Rhodic answered 2/4, 2018 at 12:50 Comment(5)
But that wont be the "vi" way, if file is huge then also it will be a problem... This might work but not a suggested way...Unlucky
reset the terminal beforehand, then select all and copy. This works through ssh session.Paulina
Fastest way for large files (under 10 thousand lines) in different remote servers if xterm_clipboard is unavailablePlantagenet
easy and simple solutionDiego
simple and works if want to copy from remote linux server (via mac terminal) into any app in the mac laptopDecanter
C
12

(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
u

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

sudo apt-get install xclip
Cementation answered 2/3, 2012 at 15:35 Comment(2)
This works with vi as well, and not just vim. ESC :version On Linux you are probably not running "vi" even if that is what you typed.Halfblooded
To avoid having to type u after the command, just insert a w after the % like so :%w !xclip -sel clipCacus
A
12

On Ubuntu 12

you might try to install the vim-gnome package:

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)

On Arch Linux

you may install vim-clipboard for the same reason.

If you run neovim then you should install xclip (as explained by help clipboard-tool)

Agential answered 14/3, 2013 at 18:18 Comment(1)
unix.stackexchange.com/questions/25965/… is there any other option anybody knows?Bronwyn
S
8

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!

Shortwinded answered 7/11, 2013 at 9:27 Comment(0)
L
8

Do copy the whole file inside the vim or its tabs

y G 

then move to a tab and paste by

p

and to cut the whole file use

d G
Levy answered 23/12, 2017 at 18:9 Comment(0)
T
7

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).

Tropophilous answered 25/10, 2009 at 5:23 Comment(2)
But if you aren't using mswin.vim, then ctrl-a increments the next number on the current line.Broil
@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.Tropophilous
G
7

gVim:

:set go=a

ggVG

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.
Garin answered 7/2, 2012 at 12:52 Comment(0)
S
5

:%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.

Stater answered 2/7, 2012 at 23:4 Comment(0)
M
5

I tried a few of the commands that people have mentioned above. None worked. Then I got hold of the simplest of them all.

Step 1: vi <filename>
Step 2: Right click on the title bar of the Putty window
Step 3: Select "Clear scrollback" (to avoid copying the rest of your SSH session)
Step 4: Right click again and select "Copy all to clipboard".

Malvoisie answered 24/6, 2016 at 7:38 Comment(3)
For 15000 line file, this didn't grab it all. I needed to increase my Putty's Window > Settings > lines of scrollback to 40000 (also available by right-clicking the title bar). It also helps to use cat instead of vi since that scrolls through all the text.Scottiescottish
I ended up always losing lines from the top and bottom of the file this way. What I do now is cat, then middle-click at the start and end of the file to extend selection.Scottiescottish
When I tried this I only got the 111 lines on the screen but there are 174 lines in my file, so Noumenon's cat answer worked for meTorry
N
4

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

:%!putclip
u

Maybe Ubuntu has a CLI app like putclip??

Noctule answered 25/10, 2009 at 14:58 Comment(1)
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).Cementation
N
4

Well, all of these approaches are interesting, however as lazy programmer I use yank all line by using combination of number + y

for example you have source code file with total of 78 lines, you can do as below:

  1. gg to get cursor at first line
  2. insert 78 + y --> it yanks 78 lines below your cursor and current line
Nobie answered 15/8, 2015 at 19:48 Comment(0)
B
4

I have added the following line to my .vimrc

nnoremap <F5> :%y+<CR>

This allows me to copy all text in Vim to the clipboard by pressing F5 (in command mode).

Birkett answered 20/4, 2019 at 9:29 Comment(2)
E850: Invalid register name on Ubuntu 18.04 and Vim 7.4Lease
@Lease I tried this on Windows. Maybe the <CR> part is different on UbuntuBirkett
R
3

If your fingers default to CTRL-A CTRL-C, then try the mappings from $VIMRUNTIME/mswin.vim.

" CTRL-C and CTRL-Insert are Copy
vnoremap <C-C> "+y

" CTRL-A is Select all
noremap <C-A> gggH<C-O>G
inoremap <C-A> <C-O>gg<C-O>gH<C-O>G
cnoremap <C-A> <C-C>gggH<C-O>G
onoremap <C-A> <C-C>gggH<C-O>G
snoremap <C-A> <C-C>gggH<C-O>G
xnoremap <C-A> <C-C>ggVG

I have them mapped to <Leader><C-a> and <Leader><C-c>.

Rior answered 27/11, 2015 at 17:48 Comment(0)
S
1

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...

Slippage answered 21/6, 2013 at 0:27 Comment(0)
T
1

I know ten years on this should be settled but the first two answers did not work for me so I kept digging. On a Redhat (remote server) - Windows 10 (local machine), if you cannot select the whole thing with a mouse, you are stuck because the usual copies do not work between the remote and the local machine clipboards.

So, to copy on the remote Linux and to paste on the local Windows, specify the primary buffer with the * and do a nice double yank

Use gg" * yy.

Tyrus answered 10/1, 2019 at 16:50 Comment(2)
Ubuntu Vim 7.4 doesn't work, it only yanks the very first line, but doesn't put all lines into clipboardLease
Ubuntu Vim 7.4 doesn't work, it only yanks the very first line, but doesn't put all lines into clipboard in :version I have "-xterm_clipboard". After installed vim-gnome Vim 8.0 with :version +xterm_clipboard also doesn't workLease
S
1

You can use a shortcur, like this one:

noremap <F6> :%y+<CR>

It means, when you push F6 in normald mode, it will copy the whole file, and add it to the clipboard. Or you just can type in normal mode :%y+ and then push Enter.

Success answered 10/6, 2020 at 1:21 Comment(0)
B
1

easy-peasy way:

if you are in windows and want to copy whole text to your local clipboard not the remote server buffer you are working with, no need for any extra tool, just:

  1. zoom out up to see your whole file text on your screen
  2. with mouse select all of them and then right click to copy to your local clipboard

Note1: nowadays on many kind of terminals (like window terminal) you can zoom in/out with ctrl++/ctrl+- and reset zoom size with ctrl+0

Note2: if your vim is numbering lines of file you can :set nonumber

Barong answered 11/12, 2022 at 23:30 Comment(0)
M
0

I have created a function to perform this action, place it on your ~/.vimrc.

fun! CopyBufferToClipboard()
    %y+
endfun
nnoremap <Leader>y :call CopyBufferToClipboard()<CR>
command! -nargs=0 CopyFile :call CopyBufferToClipboard()

OBS: If you are using neovim you also need some clipboard manager like xclip. for more information type in neovim :h checkhealth

It is also important to mention that not always a simple y will copy to the clipboard, in order to make every copy feed + wich is "Clipboard Register" try to set: :set clipboard=unnamed,unnamedplus. For mor information see: :h unnamed.

Here more information on vim wikia.

Martijn answered 16/2, 2018 at 13:7 Comment(0)
W
0

While many of the above answers are excellent, none of those solutions worked for me because I'm using the default VIM installation which came with Ubuntu 16.04, and it didn't have the clipboard option installed by default. I also wanted to paste the text into an external program.

Solution that worked: Ubuntu's default terminal allows you to highlight the entire contents by pressing Edit then Select All.

Worshipful answered 8/4, 2021 at 22:31 Comment(0)
P
-1

I use Vim in PowerShell and when I needed to copy a short block of code to windows clipboard;

I use :set nonumber to remove line numbers, then select the code visually with cursor and right click. Then I paste it to where ever I want with Ctrl + V.

Paramnesia answered 9/12, 2021 at 20:23 Comment(0)
A
-2

I couldn't copy files using the answers above but I have putty and I found a workaround on Quora.

  1. Change settings of your PuTTY session, go to logging and change it to "printable characters". Set the log file
  2. Do a cat of the respective file
  3. Go to the file you set in step #1 and you will have your content in the log file.

Note: it copies all the printed characters of that session to the log file, so it will get big eventually. In that case, delete the log file and cat the target file so you get that particular file's content copied on your machine.

Appassionato answered 31/10, 2017 at 6:50 Comment(0)
E
-3

Click the left mouse button, drag across the section you want to copy and release. The code automatically gets copied to clipboard.

Eckenrode answered 20/8, 2014 at 11:35 Comment(3)
The file could be huge, and dragging the whole text with a mouse is not productive (nor a vi way).Wither
Actually this is a good answer and works for me. In my system, environment is v restrictive. I have to cat the output on the screen, and then use mouse to drag, select and release.. then I copy to Windows Notepad++.. and works great. Nothing else works.Auctioneer
It is not copied to clipboard but rather to primary selection, which is not the same.Puto

© 2022 - 2024 — McMap. All rights reserved.