How to select all and copy in vim?
Solution 1
There are a few important informations missing from your question:
- output of
$ vim --version
? - OS?
- CLI or GUI?
- local or remote?
- do you use tmux? screen?
If your Vim was built with clipboard support, you are supposed to use the clipboard register like this, in normal mode:
gg"+yG
If your Vim doesn't have clipboard support, you can manage to copy text from Vim to your OS clipboard via other programs. This pretty much depends on your OS but you didn't say what it is so we can't really help.
However, if your Vim is crippled, the best thing to do is to install a proper build with clipboard support but I can't tell you how either because I don't know what OS you use.
edit
On debian based systems, the following command will install a proper Vim with clipboard, ruby, python… support.
$ sudo apt-get install vim-gnome
Solution 2
In normal mode:
gg"+yG
In ex mode:
:%y+
Solution 3
@swpd's answer improved
I use ,
as a leader key and ,a
shortcut does the trick
Add this line if you prefer ,a
shortcut
map <Leader>a :%y+<CR>
I use Ctrl y
shortcut to copy
vmap <C-y> y:call system("xclip -i -selection clipboard", getreg("\""))<CR>:call system("xclip -i", getreg("\""))<CR>
And ,v
to paste
nmap <Leader>v :call setreg("\"",system("xclip -o -selection clipboard"))<CR>p
Before using this you have to install xclip
$ sudo apt-get install xclip
Edit: When you use :%y+
, it can be only pasted to Vim vim Ctrl+Insert
shortcut.
And
map <C-a> :%y+<Esc>
is not conflicting any settings in my Vimrc.
Solution 4
ggVG
will select from beginning to end
zotherstupidguy
all about ruby and learning new things fast fast fast........
Updated on February 20, 2021Comments
-
zotherstupidguy about 3 years
how to select all and copy in vim insert mode? and is there another way to do it in normal mode?
I have tried visual mode and gg and shift + gg to select all and then yank, however that doesn't transfer it to the clipboard to be able to paste it in another application like skype or chrome browser.
I am sure this is a common task, and there are a lot of varieties by smarter ppl than me out there, please feel free to share yours.
-
zotherstupidguy about 11 yearsmint linux for now, switching to tiny core soon i hope :)
-
zotherstupidguy about 11 yearsI open a console in a GUI environment, does that sound right to you?
-
zotherstupidguy about 11 yearsLocal, for remote i yet to use vim :) will need some guides how to submit my vimrc to remote host, ftp a shellscript up and let it do the magic or wht? i am thinking a simple solutuion where i can apply to any machine and it will be vim ready for me... I dont know what tmux is? what is that?
-
romainl about 11 yearsDefault Vim is a "tiny" and sucky version. Get yourself a proper Vim (
vim-gnome
) and you'll be able to do"+y
and"+p
. -
zotherstupidguy about 11 yearsThanks again Sir :) another VIM adventure for me :)) I dream about vim commands lately!!! there is no turning back for me :D they should teach this in elementary schools for children EVERYWHEREE!!!!!!!!!!!!!
-
romainl about 11 yearsI had a Vim-induced nightmare, once: in the dream, I did a cleanup of my
~/.vimrc
but I forgot a useless option somewhere in there. Discovering my mistake was upsetting enough to actually wake me up in sweat. -
zotherstupidguy about 11 yearsi checked out ur blog, some kana there :)
-
romainl about 11 yearsMy blog? Ah! I forgot that I put it in my profile. No, I'm French.
-
Chris about 9 yearsthe Ctrl-y shortcut text should end at the first <CR>
-
guneysus about 9 years@Chris I did not understand. Can you explain it explicitly?
-
Shreyan Mehta about 7 yearswhere to write this??
-
Christopher Shroba about 6 years@ShreyanMehta: first you need to be in normal mode (the mode vim starts in). If you press
i
, you enter "insert mode", which is the mode where you can type text into your file. To exit insert mode, press the escape key, and you'll be back in normal mode. From normal mode, just type the keys in this answer.gg
moves the cursor to the beginning of the file,"+
tells vim the next command should use the clipboard buffer,y
tells vim to "yank" some text, andG
tells vim the text to yank is everything from the current position (the beginning of the file) to the end of the file. -
Shreyan Mehta about 6 years@ChristopherShroba oh okay got it, thanks for the comment
-
BenDavid about 4 yearsadd how to cp to clipboard or copypaste buffer?
-
Mark Stouffer about 2 yearsfollow that with
y
to "yank" the selection into the clipboard, if you have clipboard support.