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
.
Author by
Chad Birch
Updated on July 08, 2022Comments
-
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 about 15 yearswindows 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 over 14 yearsMm, ctrl-c has quite a different meaning to a terminal user.
-
darkAsPitch over 14 yearssure, 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 over 13 yearsWhat does the + do, please?
-
VoY over 13 yearsMight as well not use vim at all, if you want to use shortcuts like these.
-
Luc Hermitte over 13 yearsThis is a register related to the clipboard. The same one that the OP used in his question. =>
:h :y
,:h registers
-
valk almost 13 yearsThanks this works for me, not :%y+. But how do I paste it on localhost from the remote? :)
-
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 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 over 12 yearsIf 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 over 12 yearsDo 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 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 about 11 yearsCTRL-A increment the number under the cursor.
-
Admin about 11 yearsEven though this is not exactly an answer to the question, it was the answer to mine. Thank you.
-
Thusitha Sumanadasa over 10 yearsThis
:%y+
gives meE850: Invalid register name
.What's the wrong?? -
Luc Hermitte over 10 yearsCheck
: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 about 10 yearsIt'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 about 10 yearsMay be. By default, the unnamed register is indeed not shared with other application.
-
geoyws over 9 yearsSo 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 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 over 9 yearsI need to add a zero to paste it at the start of the file:
:0r filename
-
Utku about 8 years@LucHermitte I am getting
E488: Trailing characters
. What might be the problem? -
Luc Hermitte about 8 years@Utku, have you copy pasted
%y+
, or did you type it directly on the keyboard? -
Utku about 8 years@LucHermitte I typed it. I'm in OSX if that makes any difference.
-
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 about 8 years@LucHermitte Yep, I had included the colon. It doesn't work when I paste
:%y+
as well. -
Luc Hermitte about 8 yearsYou could try step after step. First see if commands like
:yank
or:delete
work as expected. Then, if you can inject range:%
, or1,$
,1,.
, ... Then if you can inject a register (a space will be required between the command name and registers likea
) -
verboze over 7 yearsI used to do
gg"+YG
but I never liked it because I lost the original line I was on. This is perfect! -
LearnOPhile about 6 yearsIt is possible to avoid stretching all the way to the
+
sign, see my answer below as of how. -
Sander Vanhove over 4 yearsThis 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 over 4 years@SanderVanhove, I use it a lot with
:delete
as well to completely clear the current buffer. -
Jan Kundrát about 4 yearsWelcome 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 almost 4 yearsstackoverflow.com/a/829944/6723250 has the same info with some more details
-
xeruf almost 4 yearsstackoverflow.com/a/829944/6723250 has the same info with some more details