How to select all and copy in vim?

vim
138,285

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

Share:
138,285
zotherstupidguy
Author by

zotherstupidguy

all about ruby and learning new things fast fast fast........

Updated on February 20, 2021

Comments

  • zotherstupidguy
    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
    zotherstupidguy about 11 years
    mint linux for now, switching to tiny core soon i hope :)
  • zotherstupidguy
    zotherstupidguy about 11 years
    I open a console in a GUI environment, does that sound right to you?
  • zotherstupidguy
    zotherstupidguy about 11 years
    Local, 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
    romainl about 11 years
    Default 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
    zotherstupidguy about 11 years
    Thanks 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
    romainl about 11 years
    I 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
    zotherstupidguy about 11 years
    i checked out ur blog, some kana there :)
  • romainl
    romainl about 11 years
    My blog? Ah! I forgot that I put it in my profile. No, I'm French.
  • Chris
    Chris about 9 years
    the Ctrl-y shortcut text should end at the first <CR>
  • guneysus
    guneysus about 9 years
    @Chris I did not understand. Can you explain it explicitly?
  • Shreyan Mehta
    Shreyan Mehta about 7 years
    where to write this??
  • Christopher Shroba
    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, and G 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
    Shreyan Mehta about 6 years
    @ChristopherShroba oh okay got it, thanks for the comment
  • BenDavid
    BenDavid about 4 years
    add how to cp to clipboard or copypaste buffer?
  • Mark Stouffer
    Mark Stouffer about 2 years
    follow that with y to "yank" the selection into the clipboard, if you have clipboard support.