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?

Chad Birch
  • 73,098
  • 23
  • 151
  • 149

13 Answers13

235

I use the following instruction: :%y+

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
  • 6
    What does the + do, please? – VoY Dec 16 '10 at 08:57
  • 8
    This is a register related to the clipboard. The same one that the OP used in his question. => `:h :y`, `:h registers` – Luc Hermitte Dec 16 '10 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 '11 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 '11 at 20:29
  • Do I need to set other options to yank text with non-english UTF8 chars? Currently it yanks as "működésbe lépnek" – Gambhiro Dec 26 '11 at 12:04
  • 1
    This `:%y+` gives me `E850: Invalid register name` .What's the wrong?? – Thusitha Sumanadasa Oct 29 '13 at 08:57
  • Check `:h registers`, §7. You'll see that The `+` register is only available when vim is compiled with `+xterm_clipboard` feature present. I don't know which distribution of vim you should install (as I always recompile the source code to have a full feature version of gvim). You can try `:%y*` instead. It may work. Anyway, the previous comments give more explanations. – Luc Hermitte Oct 29 '13 at 15:42
  • It's noteworthy that no register is needed at all. :%y works fine here in gvim. (Maybe only because I have set clipboard=unnamed, which shares the default clipboard with that of the OS?). – masterxilo May 15 '14 at 09:26
  • May be. By default, the unnamed register is indeed not shared with other application. – Luc Hermitte May 15 '14 at 12:08
  • @LucHermitte I am getting `E488: Trailing characters`. What might be the problem? – Utku Apr 23 '16 at 07:04
  • @Utku, have you copy pasted `%y+`, or did you type it directly on the keyboard? – Luc Hermitte Apr 23 '16 at 13:35
  • @LucHermitte I typed it. I'm in OSX if that makes any difference. – Utku Apr 23 '16 at 13:36
  • @Utku. That's rely odd. And I've no idea why you have this issue. I guess that if you pasted it you'll obtain the same problem? (BTW, in my previous comment, I've forgotten the colon -> `:` -> `:%y+`) – Luc Hermitte Apr 23 '16 at 13:39
  • @LucHermitte Yep, I had included the colon. It doesn't work when I paste `:%y+` as well. – Utku Apr 23 '16 at 13:41
  • You could try step after step. First see if commands like `:yank` or `:delete` work as expected. Then, if you can inject range: `%`, or `1,$`, `1,.`, ... Then if you can inject a register (a space will be required between the command name and registers like `a`) – Luc Hermitte Apr 23 '16 at 13:47
  • 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 '16 at 20:05
  • It is possible to avoid stretching all the way to the `+` sign, see my answer below as of how. – LearnOPhile Apr 06 '18 at 07:25
  • 1
    This makes me wonder how `:%` could be useful for other commands as well! I only use it for `:s` but this opens up a lot of possibilities :) – Sander Vanhove Feb 13 '20 at 23:06
  • @SanderVanhove, I use it a lot with `:delete` as well to completely clear the current buffer. – Luc Hermitte Feb 13 '20 at 23:18
77

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

Annika Backstrom
  • 13,937
  • 6
  • 46
  • 52
  • 1
    Thanks this works for me, not :%y+. But how do I paste it on localhost from the remote? :) – valk Jul 12 '11 at 05: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 '14 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 "+` – Annika Backstrom Sep 18 '14 at 12:48
16

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

mouviciel
  • 66,855
  • 13
  • 106
  • 140
15

Another method is this:

ggyG

Go to the top, and yank to the bottom.

Glen Solsberry
  • 11,960
  • 15
  • 69
  • 94
11

I use the following instruction: :%y.

Cheng
  • 4,816
  • 4
  • 41
  • 44
  • 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 '11 at 23:36
7

ggyG

(Go to top, yank to bottom)

Francisco Canedo
  • 1,980
  • 2
  • 13
  • 16
5
:0,$ y

I dunno which way is easier.

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"

James Scriven
  • 7,784
  • 1
  • 32
  • 36
3

Or simply in your .vimrc:

nmap <silent> <F5> ggVG"+y

So you can just use one key :)

dr Hannibal Lecter
  • 6,665
  • 4
  • 33
  • 42
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.

LearnOPhile
  • 1,391
  • 13
  • 16
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

D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38
Alex
  • 1
  • 2
    Welcome to Stackoverflow. The editor supports [formatting](https://stackoverflow.com/help/formatting), so please consider wrapping your code examples (such as the commands you used) in backticks (`\``) or code blocks. – Jan Kundrát Apr 16 '20 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)

Richard
  • 1,117
  • 11
  • 31
-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)

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • 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 06 '09 at 17:53
  • 10
    Mm, ctrl-c has quite a different meaning to a terminal user. – Xiong Chiamiov Sep 17 '09 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.... – Daren Thomas Sep 18 '09 at 05:23
  • 4
    Might as well not use vim at all, if you want to use shortcuts like these. – VoY Dec 16 '10 at 11:47
  • @VoY, I use vim all the time... but this has been bugging me lately: Do we think vim is so great because it is so great, or because of the cognitive dissonance of having spent so much time learning all the arcane movements and commands, that it had better be great? – Daren Thomas Mar 13 '13 at 08:06
  • 2
    CTRL-A increment the number under the cursor. – Alexcp Apr 29 '13 at 18:17