149

I often write something in gVim, then need to copy-paste it into another application.

Is there an easy way to yank the entire file? I usually do something like this

ggVG"+y

(Go to top, visual-line mode, go to bottom, yank)

But is there a better way that I'm missing out on?

0

13 Answers 13

235

I use the following instruction: :%y+

19
  • 6
    What does the + do, please?
    – VoY
    Dec 16, 2010 at 8:57
  • 8
    This is a register related to the clipboard. The same one that the OP used in his question. => :h :y, :h registers Dec 16, 2010 at 17:20
  • 7
    @VoY: You need to have compiled the Clipboard -property into Vim to get this working "+clipboard". It means "everything yank to external-clipboard-in-some-window-manager". This property is not de-facto but it comes with some graphical vims apparently de-facto such as gVim. Please, correct me if I am wrong (just noticed this thing when had to use yank in Windows gVim so I cannot be totally sure of course but I do believe this is the property).
    – hhh
    Dec 13, 2011 at 23:33
  • 9
    If the + register is not working for you in ubuntu 11.04+, try installing gVim (apt-get install vim-gnome). This will provide the +clipboard property into the terminal version of vim as well
    – adharris
    Dec 16, 2011 at 20:29
  • 4
    I used to do gg"+YG but I never liked it because I lost the original line I was on. This is perfect!
    – verboze
    Sep 23, 2016 at 20:05
79

ggyG (go to the first line, yank to the last line)

Edit: Ah, system clipboard. Doesn't exactly roll off the fingers, but: gg"+yG

3
  • 1
    Thanks this works for me, not :%y+. But how do I paste it on localhost from the remote? :)
    – valk
    Jul 12, 2011 at 5:39
  • 1
    So basically this command goes up to the top of the document, and does something, yank to the bottom of the document? What does " do here?
    – geoyws
    Sep 17, 2014 at 10:45
  • 2
    @Geoyws " says you want to work with a register. "+ is a register that is tied to the system clipboard. "+yG yanks everything until the bottom of the file into this register. :help "+ Sep 18, 2014 at 12:48
16

A working solution in old vi is :r filename in the new file.

2
  • 6
    Even though this is not exactly an answer to the question, it was the answer to mine. Thank you.
    – user827080
    May 16, 2013 at 16:14
  • 2
    I need to add a zero to paste it at the start of the file: :0r filename Jan 25, 2015 at 12:57
15

Another method is this:

ggyG

Go to the top, and yank to the bottom.

1
12

I use the following instruction: :%y.

1
  • 3
    ...not over different windows in window manager. + at the end yanks it to external clipboard (apparently what the op tries to do)
    – hhh
    Dec 13, 2011 at 23:36
7

ggyG

(Go to top, yank to bottom)

1
5
:0,$ y

I dunno which way is easier.

1
  • 1
    :0,$ should be the same as :%
    – tristram
    Jun 7, 2022 at 14:30
3

Or simply in your .vimrc:

nmap <silent> <F5> ggVG"+y

So you can just use one key :)

3

In OSX:

ggVG
!tee >(pbcopy)

Which I find nicer than:

ggVG
:w !pbcopy

Since it doesn't flash up a prompt: "Press ENTER or type command to continue"

2

:%y without + works over the entire system if you are using neo-vim (nvim).

This lets us avoid stretching our fingers to the + - acctually making this shortcut better than ggyG.

0

Depending on how often you do it, just map a key for this and optionally add to the vimrc

:nnoremap <F5> :%y

or

:nnoremap <leader><whatever> :%y

or whatever key you know is safe and is fastest—then add to a mappings source or whatever. The advantage to this over ggyG is that it's not moving the cursor (faster) and to retain your cursor position you'd have to add a <Ctrl-o><Ctrl-o>

People often forget that commands are session based unless in the vimrc. I often know when I'm going to do a lot of something but don't need it as default and just :[mode]remap <whatever> <whatever>

I can close and re-open vim if I need to.

If <Ctrl-A><Ctrl-C> works for you then you’re using mswin.vim in your source and losing out on the power of increment <Ctrl-a> and decrement <Ctrl-x> operations (among other things). I started in windows and stopped using mswin.vim along time ago. I personally feel it's best to grok Vim the right way and then add in the crutches you really need :P

1
  • 2
    Welcome to Stackoverflow. The editor supports formatting, so please consider wrapping your code examples (such as the commands you used) in backticks (`) or code blocks. Apr 16, 2020 at 21:28
-2

It's dirty but you don't have to use the shift key at all and only 3 different keys which may be faster:

gg1111yy

(Assuming the file is shorter than 1111 lines)

-3

On Windows I often just do CTRL-A, CTRL-C to copy all to the windows clipboard... Can't get easier than that!

I'm using a standard gvim 7.1 from the website...

(BTW: also works on my mac with MacVim and that funny mac-key+A, mac-key+C)

6
  • 1
    windows gvim adds some extra shortcuts that aren't in the other versions, to try and make it fit in better with windows users. ctrl+a, ctrl+c, are two examples of those.
    – davr
    May 6, 2009 at 17:53
  • 10
    Mm, ctrl-c has quite a different meaning to a terminal user. Sep 17, 2009 at 21:01
  • sure, but when in windows, do as the windows do :) Just saying, gvim and MacVim honor the platforms standard clipboard shortcuts - there is nothing wrong with using them either.... Sep 18, 2009 at 5:23
  • 4
    Might as well not use vim at all, if you want to use shortcuts like these.
    – VoY
    Dec 16, 2010 at 11:47
  • 2
    CTRL-A increment the number under the cursor.
    – Alexcp
    Apr 29, 2013 at 18:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.