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?
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?
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 fileUse:
:%y+
to yank all lines.
Explanation:
%
to refer the next command to work on all the linesy
to yank those lines+
to copy to the system clipboardNB: In Windows, +
and *
are equivalent see this answer.
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
)
If you're using Vim in visual mode, the standard cut and paste keys also apply, at least with Windows.
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).
: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.
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".
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:
gg
to get cursor at first line y
--> it yanks 78 lines below your cursor and current line 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>
.
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.
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
.
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.
I couldn't copy files using the answers above but I have putty and I found a workaround on Quora.
Note: it copies all the printed characters of that session to the log file, so it will get big eventually. In that case, delete the log file and cat the target file so you get that particular file's content copied on your machine.