How do i select all text in Vi/Vim?

205

64

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?

Daniel t.

Posted 2010-12-30T17:05:25.073

Reputation:

Answers

169

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

Posted 2010-12-30T17:05:25.073

Reputation: 1 809

10@Tuxmentat can you please explain what each key does in :%y+ – Jas – 2014-06-22T09:41:46.900

10This answer would be better if it explained what each key does for both sequences. – AsksAnyway – 2014-07-27T14:13:01.370

6

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 which specifies the whole file. 'y' yanks everything, and '+' specifies the clipboard register. :help <command> helped me figure this out.

– Hawkeye Parker – 2014-08-27T09:53:22.437

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 – 2014-09-24T11:47:36.253

@HawkeyeParker probably here's is some secret anyway because with this logic the :%p+ should have worked too, but it doesn't ☹ – Hi-Angel – 2014-10-07T08:14:02.300

If you are in Windows and have set clipboard=unnamed in your .vimrc, you can simply use :%y – Zenadix – 2014-12-09T15:15:43.977

11i must be doing something wrong. i type :%y+, and i get E488: Trailing characters – johny why – 2016-07-27T20:51:21.890

27I 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 – 2016-08-04T20:50:43.353

2

@johnywhy run :version and see if +clipboard is there (source)

– Alexander Malakhov – 2016-09-02T09:54:33.003

2@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 – 2017-06-23T12:33:45.180

@oligofren Thanks, good to know and gvim worked. The original poster was asking about VI though, so not sure these are ideal solutions. – EntangledLoops – 2017-06-23T20:12:55.200

@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 – 2018-02-14T07:53:19.603

This solution is not working. It's not as well as its votes! – Alex Jolig – 2018-06-26T18:45:58.990

2@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 – 2018-07-11T11:01:36.683

I use ":1,$ y"

Which means : <- Command mode, 1 <- line number 1 $ <- last line y <- yank – mujjiga – 2018-07-16T10:00:12.507

I worked like a charm on gvim windows. @Tuxmentat – Pie – 2019-05-28T01:35:46.290

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 – 2019-09-21T01:30:25.223

4+1 for the first method, another benefit is that it doesn't move the cursor position. – Andy E – 2013-03-12T12:41:32.800

106

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

Posted 2010-12-30T17:05:25.073

Reputation: 1 291

3Exactly 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 – 2015-04-21T13:54:48.203

3I personally use ggVG, but gg0vG$ might be more appropriate since it more closely replicates the 'normal' Ctrl+A operation. – Sheharyar – 2016-09-22T12:02:14.540

41

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.

silviud

Posted 2010-12-30T17:05:25.073

Reputation: 541

+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 – 2017-10-14T16:36:09.683

1To 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 – 2010-12-30T17:31:34.303

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 – None – 2010-12-30T17:34:24.207

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. – NJInamdar – 2013-07-24T04:51:23.870

17

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

user402791

Posted 2010-12-30T17:05:25.073

Reputation: 171

9

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

Posted 2010-12-30T17:05:25.073

Reputation: 516

1This is the best shortcut than any other codes. I have tested this and it works like a charm. – itsraghz – 2019-12-23T07:19:29.207

7

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

Posted 2010-12-30T17:05:25.073

Reputation: 9 044

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 – 2010-12-30T20:24:31.500

You're right. I knew that too, but typed the wrong thing. ☹ – frabjous – 2010-12-30T22:21:01.423

3

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.

Chris

Posted 2010-12-30T17:05:25.073

Reputation: 31

1

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

Posted 2010-12-30T17:05:25.073

Reputation: 381

1

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

Posted 2010-12-30T17:05:25.073

Reputation: 11

0

petrus

Posted 2010-12-30T17:05:25.073

Reputation: 101

3that's the opposite of what OP wants -.- – Rápli András – 2014-10-09T18:27:03.093

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

Posted 2010-12-30T17:05:25.073

Reputation: 171

"* is the what was selected and "+ is what was copied. http://vimdoc.sourceforge.net/htmldoc/gui_x11.html#x11-selection

– Mikel – 2010-12-30T20:25:01.437

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

user359247

Posted 2010-12-30T17:05:25.073

Reputation: 1