240

Using VI tool for editing config files.

How can I select all the text in a file (around 1000 lines), copy it, then paste into Google Docs?

peterh
  • 2,553
  • 10
  • 31
  • 49

12 Answers12

202

The simplest and fastest way is to use: : % y + and then go over to Google Docs (or wherever) and paste. Explanation:

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

Another way is g g " + y G but you will likely admit that the above is faster and easier.

Tuxmentat
  • 2,139
  • 1
  • 12
  • 8
  • 4
    +1 for the first method, another benefit is that it doesn't move the cursor position. – Andy E Mar 12 '13 at 12:41
  • 10
    @Tuxmentat can you please explain what each key does in `:%y+` – Jas Jun 22 '14 at 09:41
  • 10
    This answer would be better if it explained what each key does for both sequences. – AsksAnyway Jul 27 '14 at 14:13
  • 7
    My best guess: the ':' means we're using an "ex mode" command (I think). Anyhow, it's a command. '%' is a special [command-line range specifier](http://vimdoc.sourceforge.net/htmldoc/cmdline.html#cmdline-ranges) which specifies the whole file. 'y' yanks everything, and '+' specifies the [clipboard register](http://vimdoc.sourceforge.net/htmldoc/change.html#registers). :help helped me figure this out. – Hawkeye Parker Aug 27 '14 at 09:53
  • ggVG, then "+y is my fav, and then of course i dont use "+ because i mapped y to "+y (among others) that way i just have to do ggVG and then y – osirisgothra Sep 24 '14 at 11:47
  • @HawkeyeParker probably here's is some secret anyway because with this logic the `:%p+` should have worked too, but it doesn't ☹ – Hi-Angel Oct 07 '14 at 08:14
  • If you are in Windows and have `set clipboard=unnamed` in your .vimrc, you can simply use `:%y` – Zenadix Dec 09 '14 at 15:15
  • 15
    i must be doing something wrong. i type `:%y+`, and i get `E488: Trailing characters` – johny why Jul 27 '16 at 20:51
  • 42
    I get "E850: Invalid register name". Second method worked fine for copying, but doesn't get to system clipboard on Ubuntu 16.04 (for me, at least---I have a .vimrc that may be affecting things). – EntangledLoops Aug 04 '16 at 20:50
  • 2
    @johnywhy run `:version` and see if `+clipboard` is there ([source](http://stackoverflow.com/a/7943665/264047)) – Alexander Malakhov Sep 02 '16 at 09:54
  • 3
    @EntangledLoops you will get that error if clipboard support is not compiled in. Try downloading `gvim`, or better, NeoVim (for all its performance improvements) – oligofren Jun 23 '17 at 12:33
  • @oligofren Thanks, good to know and gvim worked. The original poster was asking about VI though, so not sure these are ideal solutions. – EntangledLoops Jun 23 '17 at 20:12
  • @EntangledLoops, I have a quite virgin vimrc and still got the same problem, but if you use split screen by `:sp file2_name`, you can past it with p. And I still think there has to be a better way to do it. – user10089632 Feb 14 '18 at 07:53
  • This solution is not working. It's not as well as its votes! – Ghasem Jun 26 '18 at 18:45
  • 3
    @alexjolig @entangledloops Not sure if that command for older versions, but for recent versions of Vi (7.4 onwards), this answer is wrong in the sense that it requires `: % + y` and not `: % y +` (note the position of the plus symbol). Hope that saves someone a few agonising minutes. [credits to RL] – Yannis Jul 11 '18 at 11:01
  • I use ":1,$ y" Which means : <- Command mode, 1 <- line number 1 $ <- last line y <- yank – mujjiga Jul 16 '18 at 10:00
  • I worked like a charm on gvim windows. @Tuxmentat – Pie May 28 '19 at 01:35
  • 1
    I don't know why this has so many upvotes, it doesn't answer the question as stated. This is how you "copy all to clipboard", not "select all". – EntangledLoops Sep 21 '19 at 01:30
  • :%y+ does not work for me. I receive E850: Invalid register name – Alec Dec 22 '22 at 15:42
129

Many given replies also yank the selection, which I was not looking for.

I'm using ggVG for this purpose (needs to be run on normal mode). This command only selects the whole text inside the document.

I've even remapped Ctrl+A for this purpose, since I don't really need to increment an integer with the shortcut (default Ctrl+A map).

Just add this into your .vimrc:

map <C-a> <esc>ggVG<CR>

It has <esc> first, to make sure the user is in normal mode.

Arda
  • 1,531
  • 1
  • 12
  • 11
  • 4
    Exactly what I was looking for, and it's nice to see that Ctrl+A isn't occupied in vim by default. Hope there are no side-effects. – Nikos Alexandris Apr 21 '15 at 13:54
  • 5
    I personally use `ggVG`, but `gg0vG$` might be more appropriate since it more closely replicates the 'normal' Ctrl+A operation. – Sheharyar Sep 22 '16 at 12:02
49

You can use cat file and then select output and copy and paste if you need to paste it into your browser.

For vi this is how you can select all text and write it into a new file:

shift v  -- visual mode
shift g -- jump to eof
"*y -- yank select text
:e my_new_file -- create a new file
"*p -- paste into a new file

In theory this should work on both Linux and Windows - I tried it on a Mac but it doesn't work.

Gaff
  • 18,569
  • 15
  • 57
  • 68
silviud
  • 621
  • 4
  • 3
  • 1
    To paste into the Web browser `cat file` is the way to go. The `shift v` method only copies to Vi's internal buffer. – Aleksandr Levchuk Dec 30 '10 at 17:31
  • not if you use the system clipboard which uses the * registry - but this works on X only and I heard on windows - so if you ssh you need the -X - to check if vim has support for this into vim -- :set clipboard+=unnamed –  Dec 30 '10 at 17:34
  • Is there any alternative to do this without creating a new file.if we are editing live on server we need to keep the backup code on local machine so that we can revert back. – Nagama Inamdar Jul 24 '13 at 04:51
  • +1 for cat and select in putty. Of course, will not work for huge files and is not the most user friendly method. But for copying small script files which goes outside screen viewport is good enough. – Arnis Juraga Oct 14 '17 at 16:36
22

USE ggVG. type "gg" to go at top of the test Then type VG.

user402791
  • 221
  • 2
  • 2
14

I am using Vim 7.4 in CentOS-7 environment. Which worked me for selecting all the text is

:%y

Then just p in the next file where I want a full copy.

Or

You can use cat command.

cat copyfile > pastefile

This git repo has some other useful commands too.

Sachith Muhandiram
  • 624
  • 1
  • 8
  • 19
9

gg"+yG

or

gg"*yG

depending on whether + or * is the system clipboard. (On many unixes, + is the mouse selection buffer for middle-mouse-clicking, and * is the system clipboard).

frabjous
  • 10,755
  • 3
  • 34
  • 27
  • 1
    I think it's the other way around: "* is selection and "+ is clipboard. http://vimdoc.sourceforge.net/htmldoc/gui_x11.html#x11-selection – Mikel Dec 30 '10 at 20:24
  • You're right. I knew that too, but typed the wrong thing. ☹ – frabjous Dec 30 '10 at 22:21
5

For a Mac, use pbcopy (pasteboard copy):

cat file.txt | pbcopy

The contents of file.txt are now on the clipboard for pasting into another application (e.g. browser).

You can also paste the contents of the clipboard into a file using pbpaste:

pbpaste > file.txt

While this doesn't involve vi specifically it does achieve the same goal on a Mac.

bwDraco
  • 45,747
  • 43
  • 165
  • 205
Chris
  • 51
  • 1
  • 1
2

If you're using a linux desktop, you could load it into the clipboard using xclip or xsel. For something that size you might just want to use the upload feature in google docs.

Cakemox
  • 381
  • 2
  • 5
2

Another way would be:

You press v key on your keyboard and turn VIM to VISUAL

Then select all text by scrolling down

^+ INSERT to copy

SHIFT +INSERT to paste the text wherever you want on Google Docs.

user237678
  • 21
  • 1
0

Without using vi, you can upload text to google docs using their API and cURL.

petrus
  • 101
  • 2
0

See http://vim.wikia.com/wiki/Accessing_the_system_clipboard for options on how to do this. (if compiled in "* should refer to the system clipboard). There are also instructions there for how to use xsel with vim.

kasterma
  • 191
  • 1
  • 5
  • "* is the what was selected and "+ is what was copied. http://vimdoc.sourceforge.net/htmldoc/gui_x11.html#x11-selection – Mikel Dec 30 '10 at 20:25
0

Use the following command.

cat <your file name>

It will echo the content of file. Now select, scroll, copy, paste.
Game Over

Ex.:

cat bobis.txt
Excellll
  • 12,627
  • 11
  • 51
  • 78