I use gvim in windows. How do I copy text from the current position to the end of the line in vi and paste it in another file opened in vi?
4 Answers
The normal-mode command to move to the end of the line is $
.
You can copy to the end of the line with y$
and paste with p
.
To copy/paste between different instances, you can use the system clipboard by selecting the *
register, so the commands become "*y$
for copying and "*p
for pasting.
$
move-to-linebreak
$
y$
yank-to-linebreak
y,$
"*y$
select clipboard-register yank-to-linebreak
",*,y,$
"*p
select clipboard-register paste
",*,p
Check :h registers
for more information.
-
Thanks. but i want to paste the contents to 'another file' opened in vi. This works with same file only. Commented Oct 14, 2011 at 7:14
-
4It will work for any file open within the same instance. You can copy to system clipboard by selecting the
*
(or+
) register.– Don RebaCommented Oct 14, 2011 at 7:17 -
Why the system registers can't work... I can't copy the context in
file1
tofile2
. I still need to use the mouse. What could be the reason probably?– AlstonCommented Jan 22, 2015 at 3:48 -
10I usually use
D
to cut from current position to the end andp
to paste(recover) the original data, and then move to somewhere else to paste the yanked data. Simply because it is far easier to typeD
thany$
.– neevekCommented Jan 27, 2015 at 14:11 -
1
If you don't want to include the line break with the yank, you can use yg_
. (Or in your case, "*yg_
)
Basically, just recognize there's a difference between $
and g_
movement-wise. It's helped me on numerous occasions.
-
4Is there anyway to swap
g_
and$
? I find myself rarely if ever needing to include the line break in motions. Commented Aug 19, 2013 at 5:20 -
2YES! the extra newline is driving me crazy. This is the info needed. Commented Jul 22, 2015 at 22:21
-
6@JonathanDumaine in your
$MYVIMRC
add the linenmap $ g_
Commented Jan 25, 2016 at 17:23 -
-
@KhalilRavanna True but isn't that already covered by
yy
? Commented Dec 26, 2020 at 2:57 -
@KhalilRavanna You just got done saying that the default functionality of
Y
yanks the whole line, start to end (in your previous comment).Y
does not yank from the cursor to the end of the line unless you remap it as suggested by this answer. Commented Dec 30, 2020 at 0:02 -
Lol you're right I checked this comment without thinking and had internalized this suggestion somehow. Going to delete all these comments to remove possible confusion for future users. I recommend you do the same. Thanks for correcting my momentary stupidity :) Commented Dec 31, 2020 at 21:41
A different solution: Dp
and paste it with p
. In fact this first deletes to the end of line and re-pastes it at the same location. Paste it somewhere else with p
.
-
5Why not Du and leave the file unmodified? (note: this include new line)– geneCommented Jun 29, 2019 at 22:44
y$
in normal mode