zotherstupidguy
zotherstupidguy

Reputation: 3024

How to select all and copy in vim?

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.

Upvotes: 49

Views: 186073

Answers (8)

TransferOrbit
TransferOrbit

Reputation: 227

If you’re actually in insert mode as you state, then do the following:

CTRL+o :%y

then hit enter. All the text in the buffer will be copied to the default register and you are left, of course, in insert mode.

Upvotes: 0

doug
doug

Reputation: 21

My approach is to 'cat' the file content then make a selection with the mouse and scroll finally copying to the clipboard with Mac+C / Ctrl+C or even right click and then selecting 'copy'.

Upvotes: 2

tmaj
tmaj

Reputation: 34947

For VS Code + vim you can:

"vim.useSystemClipboard": true,

and then

ggVGy
  • gg - go to the beginning of the file
  • V (capital) - start linewise visual mode
  • G (capital) - go to the end of file
  • y - yank (copy)

This works for me across applications in Ubuntu 22.04 and in Windows 11.

gg"+yG is more correct because it explicitly targets the clipboard (rather than a vim register), but "vim.useSystemClipboard": true, + ggVGy seems to work across applications so I use that since it's easier for me to type.


BTW. If using VS Code + vim then

"vim.useCtrlKeys": false,

allows using Ctrl+A.

Upvotes: 2

Seb
Seb

Reputation: 5842

If you just want a simple way to copy file content from inside the terminal (you mention "copy text to another application"), then the cat command is easy to select/copy the output:

cat <filename>

Upvotes: 4

TastyCode
TastyCode

Reputation: 5799

ggVG 

will select from beginning to end

Upvotes: 13

guneysus
guneysus

Reputation: 6502

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

Upvotes: 6

romainl
romainl

Reputation: 196466

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

Upvotes: 18

swpd
swpd

Reputation: 1151

In normal mode:

gg"+yG

In ex mode:

:%y+

Upvotes: 45

Related Questions