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

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

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.

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.

While there's a great explanation of how to exploit the system clipboard in vim, it sounds like you're just having trouble getting your vim to access the clipboard in the first place. Try installing vim-gnome, it gives you the packages you need to get to the system clipboard.
For some reason, "* didn't work for me, but the exact same command with the "+ register did.

To select the whole file you can jump to the beginning, start visual mode, jump to the end:
ggVG

This question is a few years old now, but I had this same problem on Linux Mint 18. I found using xclip worked for me. You can map the command vmap <F7> :!xclip -sel c<CR><CR> in your .vimrc to have your current selection in visual mode copied to the system clipboard.
Here is a thread containing the above (and other) solutions.

You can use
Vggy/vggy or,
VGy/VGy
To visually select any number of text and then copy it, in your case it is gg / G as you want all text on the file,
gg is to copy while your cursor is at bottom of the file, gg for go to top
G is to copy while your cursor is at top of the file
Or even you can always use
Vk(as number of time)y to copy the selected lines of text.

Related

How to select all text in vim normal mode?

Is there "keyboard-only" way to select an entire vim document in a way that is equivalent to a left-click and drag with a mouse in normal mode? Please do not confuse this with selecting all text in visual mode (ggVG). I want to be able to follow this up with a right-click paste into notepad++ (ggVG/ggVGy followed by a right-click paste in notepad++ does not copy the document). Thanks
Again, the "ggVG" commands are not working, nor are the "+y" commands (which I should have mentioned in my original post). Perhaps it is worth noting that I am working on a Windows local machine (where I have notepad++ open) and am generating the vim file on a linux virtual machine (slurm cluster). Under these working conditions, if I left-click drag over the vim doc and right-click paste in notepad++, the selected text copies over. However, the process is cumbersome for large files, hence my inquiry. Thanks again.
You should have mentioned that, of course, as it is not a meaningless detail at all.
Manual selection in a terminal can only select the text currently displayed in the viewport, which is obviously cumbersome for larger files. The only practical way to copy on the remote machine and paste on the local machine (and vice-versa) is to enable X-forwarding and build Vim on the remote machine against X libraries. This will give you what you want: a shared clipboard.
You won't be able to reach your goal in a practical way if you can't or don't want to install the necessary stuff on the remote machine.
As a lightweight alternative, you could simply scp the remote file to your local machine.
Just use (esc) :%y+. This will copy the entire document to your clipboard. Then you can go to notepad++, or whatever else you want to use, and paste it with a right click.
Explanation:
%: Tells vim the next command will be applied to all lines.
y: to all 'yank' lines
+: Copies all lines to clipboard, You can also use Ctrl + C instead. Note: + is sometimes bound as *. And sometimes both are equivalent.
Or you can also use the slightly longer way: ggVG+.
If you really want to be fancy you can remap Ctrl + A to ggVG or %y by adding this line to your .vimrc:
map <C-a> <esc>ggVG<CR>
Try to use Xshell remote login software. in there is a option called "To Text Editor".
Just open the file using "vi filename.c" it will displayed on the screen after that just made a left click on the work area and choose "To Text Editor--->all" . then these all text moved to a notepad file then u can simply copy and paste in notepad++.

How to use different registers in Vim to copy/paste from/to OS clipboard -- how exactly?

I've read this How to make vim paste from (and copy to) system's clipboard? and I know what the hotkeys are. But I'm unable to execute any of those commands. For example, what exactly should I press to call
"* or "+? In which mode also? I've tried different things and none of them worked.
I am assuming you've double-checked that :echo has('clipboard') returns 1. If it returns 0, you're out of luck since vim isn't compiled with access to the system clipboard.
If you have clipboard powers, then yanking and pasting inside of vim is done with pressing just the letter y and the letter p, in normal mode. Start doing this first, to confirm that you can yank and paste inside of vim. For instance, yank a line: yy and paste it p.
Next, confirm that you can yank inside of vim to the system clipboard with "*y. (That means pressing the double-quote (shift-'), then an asterix (shift-8), then the letter y, all in quick-ish succession. Toggle over to another app and paste with the regular control-V. (You may be able to look at the bottom left of status line to see what you are literally typing, which might help).
If that works, then the clipboard functionality is "good to go." If it doesn't you might need to tweak your .vimrc to get things working. Try setting the clipboard to unnamed: set clipboard=unnamed and retest step 3 again.

How to copy from one split and paste in another in byobu?

So let's say I have two splits open in byobu, side by side. Furthermore, both splits have different files open in vim. I want to highlight text from one file in one split and copy it to a separate file in the other split. Any ideas?
All the results I found while searching for this talked about using the scrollback feature to copy and paste in byobu, however, that only seems to work inside a single split; not across splits.
Looks like I posted to quickly; seemed to have found the solution. I followed the steps found here:
http://linuxcommand.org/lc3_adv_termmux.php
I followed the steps:
shift-f3 - move to split to be copied from
alt-pgup - enter copy mode
space - start selection
cursor through desired text
enter - end selection
shift-f3 - shift focus to split to copy to
ensure receiving vim is in insert mode
alt-insert - paste selected text
If you are using an X Window Server, an alternative mouse-based solution to using the scrollback mode (which involves remembering a lot of keystrokes) is:
Zoom in on the current pane (Shift-F11), bringing this pane to the foreground.
You can now select the relevant text with your mouse without the vertical split getting in the way.
Unzoom the pane (Shift-F11 again)
Switch to other pane or wherever else you want to paste.
Middle click paste.
If your Vim supports the system clipboard (i.e. if vim --version output shows +clipboard), you can copy into the system clipboard from the first Vim and paste from it into the second one. This releaves us of the need to ensure the receiving Vim is in insert mode and has paste set appropriately.
The trick is to use the "+ register. So when you do the copy, prefix whatever yanking command you want to use with "+; and do likewise prefix the put command you use in the receiving Vim with it.
If you're on an X11 system, you can also use the "* register, which is X's "PRIMARY" selection buffer -- the one where text goes if you just highlight it, and which you can paste by pressing the middle button.
See :help gui-selections. GUI selection support generally requires a Vim other than "vim-tiny"; on Debian and Ubuntu the vim-gtk and vim-gnome packages are good choices.

selecting in visual mode to paste outside vim window

I need to paste some selected block in visual mode to outside of vim. Currently I need to select this block manually from mouse to paste outside of vim.
As selecting texts in visual mode is easier ,it would be efficient to select some text for pasting outside of vim.
You could yank the text into the + (plus) register, that is mapped to the system clipboard. Just select the text in the mode you like and then type "+y.
Disclaimer: Linux
So what I have noticed is you need clipboard support compiled in to your vim. I ended up compiling my own vim, which had the clipboard support. To check run vim --version and look for a +clipboard or a -clipboard, if it's + then yay you have it, if it's - then you need to compile vim yourself or download a version with clipboard support compiled in. Then the answers people have said seem to work. For me "*y copies into the buffer that is pasted by pressing the middle button, and "+y copies into the buffer which is the normal control + c or on the terminal control + shift + c so what I put into my vimrc was
map <C-c> "+y
that way doing control + c me paste it somewhere else by pressing the exact same command
:wq
If you are using GUI-based gvim, simply yank your text into the "clipboard register" by prefixing your yanking command with "+. That is, when you have finished selecting your text in visual mode, press "+y to yank your text then it will be in your system clipboard.
If you are using text-based vim and your vim has clipboard access to your current system, it's just the same as gvim. If your vim has no clipboard access, try to establish the clipboard connection as described in this page:
http://www.quora.com/How-can-you-copy-all-contents-of-a-text-file-opened-in-vim-through-Putty-on-a-Windows-desktop-to-Windows-clipboard
You can bind contents of the visual selection to system primary buffer (* register in vim, usually referred as «mouse» buffer) by using
set clipboard^=autoselect
You have to identify the register that vim is using to get the outside clipboard.
First copy any text outside vim and then inside vim do the command :registers and look for the text you copied, once you have identified the register simply use it every time you need to copy and paste from the outside:
for example:
Im using gvim in Windows7 and the register used by vim to get the external clipboard is
*"
then in vim select the text and do
*"y to copy (yank) and paste outside as usual
and to paste inside vim from outside do *"p
you can also do a map to the register to easy copy/paste

How to clear the line number in Vim when copying?

I copy some code from one part of one file to another part in vim, I find that, there are line numbers in each line and the format is gone, how to set correct format as origin ?
like this:
40 root /opt/release/current/public;
67 41 passenger_enabled on;
68 42
If you have line numbers, I'm quite sure you are not using Vim's yank/put operations (these will never copy the linenumbers, foldcolumn, icons etc) because in terms of the edit buffer, they don't exist.
My guess is you are working in a terminal emulator and using the mouse to copy stuff to the clipboard, which possibly selects the 'extraneous' room of the screen (including virtual spaces at the end, line numbers, fold markers etc)
You might have luck setting
:se mouse+=a
in order to get the behaviour of the mouse like you expect it. Otherwise, do the selection with V<movement>...y (y for yank, which corresponds to 'copy')
Then on the destination use p (put at cursor), or P (put before cursor)
Let me know if that helped or you need more info
In case anyone wants a quicker way (on Linux anyways), I have noticed in vim you can hold down ctrl and drag over the region you want to copy and you'll avoid the line numbers and select the part you want.
Steps:
ctrl and drag over area
release ctrl
copy (either keyboard shortcut or right click)
In normal mode, type :se nonu
This is the easiest way to remove the line numbers and you will be able to copy the text without the line numbers.
A permanent solution for this is to add the below code at the end of your .vimrc file located in your home directory.
se mouse+=a
By adding this you will be able to select only text and not the line numbers as shown in below image:
If you are not getting your .vimrc file in your home directory (i faced this problem), type the command :scriptnames in vi editor, it will display the location of your .vimrc file. Reference
On Mac: I found out that you can select the desired area with Option+Command and copy paste it to another editor.
All the previously entered solution are very good one. However, sometimes your are using vim on a remote server (so you cant copy to your clipboard using "+y). Often terminals support copy paste operation.
I use vim to output visual selection to a new shell where I can copy text using terminal feature:
:'<,'>w ! bash -c cat
Then I can easily copy the output.
Same pattern for pasting in vim:
:r ! bash -c cat
Then I paste and send EOF to cat using Ctrl+d. This method also avoid reindenting the text you paste (Note: you can disable automatic indentation using :set pi!).
Have a look at the pastetoggle option sometimes set to F11.
As an alternative you could always write the section you want to copy into a temporary file
(ma, goto end line then use :'a,.w tempfile) then read it into the second file.
For further investigation you might want to look at the autoindent option.
On Windows VIM GUI: :set nu and then hold down Ctrl-Shift while
highlighting the desired text with the mouse. This yanks the line numbers
into the buffer.
On Windows using Putty, I have noticed in vim you can hold down ALT and drag over the region you want to copy and you'll avoid the line numbers and select the part you want.
Steps:
Press and Hold ALT
Drag over area
Release ALT
Copy using CTRL+Shift+C (or right click if that is turned on)
Variations:
The specific key may be different based on your specific set up.
If ALT does not work, try the following keys instead:
CTRL
CTRL+Shift
ALT+Shift
Note:
If anyone knows of any other key combinations that have worked for them let me know and I will update this answer with them.
I mapped the below command to a key.
It strips the whitespace around the copied line numbers.
It ignores the line text and any blank lines behind.
:1,$s/^\s*[0-9]\+\s//\|1,$s/^\s*[0-9]\+\n/\r/<cr>
You can also consider using sed and pbcopy to copy the lines to a clipboard, where you can paste to another terminal or apps outside of vim.
sed -n <line start #>,<line end #>p <file name> | pbcopy
simplest way just: $ cat "your file"
and then just copy it from your terminal without numbers

Resources