Yank entire file

85,642

Solution 1

I use the following instruction: :%y+

Solution 2

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

Solution 3

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

Solution 4

Another method is this:

ggyG

Go to the top, and yank to the bottom.

Solution 5

I use the following instruction: :%y.

Share:
85,642
Chad Birch
Author by

Chad Birch

Updated on July 08, 2022

Comments

  • Chad Birch
    Chad Birch almost 2 years

    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?

  • davr
    davr about 15 years
    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.
  • Xiong Chiamiov
    Xiong Chiamiov over 14 years
    Mm, ctrl-c has quite a different meaning to a terminal user.
  • darkAsPitch
    darkAsPitch over 14 years
    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....
  • VoY
    VoY over 13 years
    What does the + do, please?
  • VoY
    VoY over 13 years
    Might as well not use vim at all, if you want to use shortcuts like these.
  • Luc Hermitte
    Luc Hermitte over 13 years
    This is a register related to the clipboard. The same one that the OP used in his question. => :h :y, :h registers
  • valk
    valk almost 13 years
    Thanks this works for me, not :%y+. But how do I paste it on localhost from the remote? :)
  • hhh
    hhh over 12 years
    @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
    hhh over 12 years
    ...not over different windows in window manager. + at the end yanks it to external clipboard (apparently what the op tries to do)
  • adharris
    adharris over 12 years
    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
  • Gambhiro
    Gambhiro over 12 years
    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"
  • darkAsPitch
    darkAsPitch about 11 years
    @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?
  • Alexcp
    Alexcp about 11 years
    CTRL-A increment the number under the cursor.
  • Admin
    Admin about 11 years
    Even though this is not exactly an answer to the question, it was the answer to mine. Thank you.
  • Thusitha Sumanadasa
    Thusitha Sumanadasa over 10 years
    This :%y+ gives me E850: Invalid register name .What's the wrong??
  • Luc Hermitte
    Luc Hermitte over 10 years
    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.
  • masterxilo
    masterxilo about 10 years
    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?).
  • Luc Hermitte
    Luc Hermitte about 10 years
    May be. By default, the unnamed register is indeed not shared with other application.
  • geoyws
    geoyws over 9 years
    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?
  • Annika Backstrom
    Annika Backstrom over 9 years
    @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 "+
  • Stefan van den Akker
    Stefan van den Akker over 9 years
    I need to add a zero to paste it at the start of the file: :0r filename
  • Utku
    Utku about 8 years
    @LucHermitte I am getting E488: Trailing characters. What might be the problem?
  • Luc Hermitte
    Luc Hermitte about 8 years
    @Utku, have you copy pasted %y+, or did you type it directly on the keyboard?
  • Utku
    Utku about 8 years
    @LucHermitte I typed it. I'm in OSX if that makes any difference.
  • Luc Hermitte
    Luc Hermitte about 8 years
    @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+)
  • Utku
    Utku about 8 years
    @LucHermitte Yep, I had included the colon. It doesn't work when I paste :%y+ as well.
  • Luc Hermitte
    Luc Hermitte about 8 years
    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)
  • verboze
    verboze over 7 years
    I used to do gg"+YG but I never liked it because I lost the original line I was on. This is perfect!
  • LearnOPhile
    LearnOPhile about 6 years
    It is possible to avoid stretching all the way to the + sign, see my answer below as of how.
  • Sander Vanhove
    Sander Vanhove over 4 years
    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 :)
  • Luc Hermitte
    Luc Hermitte over 4 years
    @SanderVanhove, I use it a lot with :delete as well to completely clear the current buffer.
  • Jan Kundrát
    Jan Kundrát about 4 years
    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.
  • xeruf
    xeruf almost 4 years
    stackoverflow.com/a/829944/6723250 has the same info with some more details
  • xeruf
    xeruf almost 4 years
    stackoverflow.com/a/829944/6723250 has the same info with some more details