127

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?

1
  • do y$ in normal mode Commented Sep 22, 2022 at 0:35

4 Answers 4

199

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.

7
  • 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
  • 4
    It will work for any file open within the same instance. You can copy to system clipboard by selecting the * (or +) register.
    – Don Reba
    Commented Oct 14, 2011 at 7:17
  • Why the system registers can't work... I can't copy the context in file1 to file2. I still need to use the mouse. What could be the reason probably?
    – Alston
    Commented Jan 22, 2015 at 3:48
  • 10
    I usually use D to cut from current position to the end and p to paste(recover) the original data, and then move to somewhere else to paste the yanked data. Simply because it is far easier to type D than y$.
    – neevek
    Commented Jan 27, 2015 at 14:11
  • 1
    @PhilipRego, it's y$, without semicolon.
    – Don Reba
    Commented May 18, 2017 at 18:59
61

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.

4
  • 4
    Is 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
  • 2
    YES! 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 line nmap $ g_
    – thedanotto
    Commented Jan 25, 2016 at 17:23
  • @thedanotto you beautiful person Commented Jan 28, 2016 at 20:40
12

Add this line to your .vimrc

" Make Y yank till end of line
nnoremap Y y$

More at my vimrc.

3
  • @KhalilRavanna True but isn't that already covered by yy?
    – NReilingh
    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.
    – NReilingh
    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
3

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.

1
  • 5
    Why not Du and leave the file unmodified? (note: this include new line)
    – gene
    Commented Jun 29, 2019 at 22:44

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