Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy all the text from vim editor using vim command line?

Tags:

linux

vim

I want to select all the text from the vim editor, I tried the command :%y+ but getting error E850: Invalid register name. I get this command from this link. Please help me how to copy all the text from file which is open in vim. They are using yank, what is meaning of it..

like image 413
geeks Avatar asked Jun 15 '15 06:06

geeks


People also ask

How do I select all text in vi mode?

Use ggVG To Select All First, we will press the ESC key to change normal mode. Then we will move to the start of the file by using the gg keys. Then enable the visual mode with the V key and the last step is pressing G which will select from the start of the file to the end of the file.

How do I copy and paste an entire line in vi editor?

Directions: Press the ESC key to be sure you are in vi Command mode. Place the cursor on the line you wish to copy. Type yy to copy the line.


2 Answers

I had a similar problem. Don't know why you got so many down votes. The problem is that you haven't installed vim-gnome which takes about 24 MB and adds a feature to the inbuilt vim.

sudo apt-get install vim-gnome 

then your command will work. :%y+ This command will copy all the text in system's clipboard.

like image 189
Harnirvair Singh Avatar answered Sep 20 '22 00:09

Harnirvair Singh


TLDR: If you want to copy text in Vim to the system clipboard type ggVG"*y. Explanation below...

Vim runs in the terminal and, depending upon how you are using it and which type of Vim you are running, it's not really designed for you to select text with a mouse and copy and paste in the traditional way.

If you want to select all of the text using Vim then use ggVGy (note the uppercase VG in the middle). This command moves the cursor to the top of the file, enters visual mode, moves to the bottom of the file (thus, selecting all of the text) and then yanks (copies) it. You can then use p to put (paste) this code but only inside of Vim.

If you want to copy to the clipboard to use somewhere outside of Vim then try this:

First, select everything using the commands outlined above but without the final y: (ggVG). Then press "*y. This should now copy it to your operating system's clipboard and you can just paste (Ctrl/Cmd+v) anywhere you want outside of Vim. This can vary depending on what settings you have for Vim but it should work.


A brief explanation of the commands used. gg goes to the top of the file. V enters visual mode by lines. G goes to the end of the file. y yanks (copies) the text but not to the clipboard. p puts (pastes) the text.

The more advanced (i.e. cool) stuff:

" allows you to access registers. For example "a provides access to register a.

The * is the system clipboard so "* provides access to the system keyboard. Therefore, "*y yanks into the system clipboard.

like image 41
Gordonium Avatar answered Sep 17 '22 00:09

Gordonium