Search code examples
vim

Yank from current cursor position till end of line


I tried using Y to yank until end of line, but that just imitates yy, I searched this up, and some people seem to say that that's the intended behavior, however, the Vim cheat sheet (https://vim.rtorr.com/), and the fact that D and C "delete" and "change" till end of line, tells me otherwise. Is the cheat sheet outdated? Why is Y so special that it breaks the pattern that D and C follow? Now I know that I can remap Y to y$ like this:

nnoremap Y y$

Still, however, the fact that I have to do that, and that's not the default behavior seems to be completely counter-intuitive and does not make sense to me. If there's a good reason for this I would love to know.


Solution

  • So… the canonical way to "yank from cursor to end of line" is:

    y$
    

    where:

    • y is an :help operator, help y,
    • and $ is the motion on which it operates, :help $.

    You can also do c$ to "change from cursor to end of line" or d$ to "delete from cursor to end of line", etc.

    That form, "operator+motion", is reasonably certain to work in vi itself and lots of its clone/emulators, including Vim in all its incarnations. It is all very logical and intuitive.

    The troubles started with vi, which had C, an alternative to c$, and D, an alternative to d$, but not Y, which is only one of many inconsistencies.

    As a vi impersonator, Vim had to maintain compatibility with vi. Concretely, it meant that it couldn't change the semantics of old vi commands—so c, d, y, $, C, and D had to stay the same—but it also meant that it was free to create new commands. And creating new commands it did, like Y. Logically, it should have been made to work like y$, but the author decided against all common sense to make it work like yy instead.

    If you come from c$ and d$, then Y is weird. If you come from yy, cc, and dd, then it is C and D that are weird. So much for that famous "Vim language".

    Because that is nonsense lots of users have been adding variants of the mapping below to their vimrcs for literally decades, possibly even in vi:

    nmap Y y$
    

    as per :help Y. Which led to the current status quo: OK, Y is yy out of the box, but fixing it to be y$ is an easy one-time fix so whatever.

    Don't get me started on freaking cw.