Xinus
Xinus

Reputation: 30563

Copy all the lines to clipboard

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?

Upvotes: 810

Views: 1412745

Answers (29)

Elijah M
Elijah M

Reputation: 845

WSL

On Windows Subsystem for Linux, run this command in normal mode:

:%w !clip.exe

Upvotes: 0

King Saozer
King Saozer

Reputation: 113

. On linux, I recommand at first to press esc key in order to quit the insert mode of vim. Now use ggVG to select the content then make % y + in order to store in linux clipboard. For QWERTY keyboard, you should press SHIFT for up letters (shift+5 for %).

. On SSH mode for windows Powershell, you can just use SHIFT + MouseLeft to select the content, then CTRL+SHIFT+C to store in windows clipboard. At end use traditional CTRL+V in your destination.

Upvotes: 0

Rook
Rook

Reputation: 62578

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)

Upvotes: 982

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

Upvotes: 1

smeric
smeric

Reputation: 91

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.

Upvotes: -1

Nike
Nike

Reputation: 1307

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.

Upvotes: 0

Nicolas Pardo
Nicolas Pardo

Reputation: 41

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.

Upvotes: 1

aiutopia.dev
aiutopia.dev

Reputation: 852

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)

Upvotes: 12

aks
aks

Reputation: 526

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

Upvotes: 4

Lefty G Balogh
Lefty G Balogh

Reputation: 1898

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.

Upvotes: 1

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827942

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

Upvotes: 813

Pham Luan
Pham Luan

Reputation: 609

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

Upvotes: 49

SergioAraujo
SergioAraujo

Reputation: 11830

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.

Upvotes: 0

Abdul Basit
Abdul Basit

Reputation: 150

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

Upvotes: 8

Zack
Zack

Reputation: 1287

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.

Upvotes: 98

Abhinav Deshpande
Abhinav Deshpande

Reputation: 589

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

Upvotes: 5

go2null
go2null

Reputation: 2308

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

Upvotes: 3

arfo
arfo

Reputation: 321

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

Upvotes: 4

Tordek
Tordek

Reputation: 10882

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

Upvotes: 68

Kapeel Kokane
Kapeel Kokane

Reputation: 39

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

Upvotes: -3

Brian
Brian

Reputation: 31302

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

Upvotes: 112

gameboy90
gameboy90

Reputation: 303

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!

Upvotes: 8

jahroy
jahroy

Reputation: 22692

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

Upvotes: 1

EmilianoGNFNR
EmilianoGNFNR

Reputation: 623

This is what I do to yank the whole file:

ggVGy

Upvotes: 55

kev
kev

Reputation: 161954

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.

Upvotes: 7

Ma&#39;moon
Ma&#39;moon

Reputation: 51

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

Upvotes: 5

Stew
Stew

Reputation: 4535

(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

Upvotes: 13

rshdev
rshdev

Reputation: 383

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

Upvotes: 4

paxdiablo
paxdiablo

Reputation: 882466

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

Upvotes: 7

Related Questions