28

Is there a specific historical reason for this?


Background — (you can skip this part if you already understand the question.)

As intermediate/advanced vi users will know, y is the "yank" command—it yanks (copies) the text specified by the following movement command.* Thus ye yanks to the end of the word, y0 yanks from cursor position to the beginning of the line, y_ yanks the entire current line, y$ yanks from cursor position to the end of the current line, etc.

The d (delete) command and the c (change) command can both be used with all of these motions as well.

dd is a synonym for d_ and deletes the entire current line. Likewise, cc is a synonym for c_ and will change the current line (i.e. it will delete all the text and put you in insert mode at the beginning of the line).**

The "yank" command follows this convention; yy will yank the entire current line just like y_.

There is another set of synonyms: D is a synonym for d$ and will delete from the cursor position to the end of the line. C is a synonym for c$ and will change the text from the cursor position to the end of the line, placing you in insert mode to type the new text.

However, Y is another synonym for yy or y_ and will yank the entire line, not just from the cursor to the end of the line as you would expect from the C and D patterns.


I understand that in Vim it was kept this way to preserve backward compatibility with vi, as is mentioned in the Vim help under :help Y:

If you like "Y" to work from the cursor to the end of line (which is more logical, but not Vi-compatible) use ":map Y y$".

So this is a holdover from vi. Fine.

But, why was the command designed that way in the first place? Was there any logic to it ever?


*Specifically it places the text in register 0 and points the unnamed register at register 0.

**Although it's not relevant to my question, S is another synonym for cc or c_.

  • 3
    I am not sure why it was made like it. You would have to ask Bill Joy about it. Chances are however, this a logical bug, that just has never been fixed in the original vi and then made it into all the clones and eventually has therefore been demanded by POSIX. – Christian Brabandt Jan 8 '16 at 9:27
  • 3
    If you ever find the answer they also need it here. ;) – joeytwiddle Jan 8 '16 at 9:51
  • 1
    I doubt this is the actual reason, but the person who objected to Y being remapped in vim-sensible argued that, as a C programmer, they used to-end-of-line yanking far more than entire-line yanking, and therefore the default mappings are good. YMMV: mine certainly does. – Rich Jan 8 '16 at 16:20
15

I found a paper "An Introduction to Display Editing with Vi" by William Joy (vi creator) and Mark Horton (vi maintainer since 1979).

From the paper it is clear that the default Y behavior is not a mistake, but a desired feature. In the "Rearranging and duplicating text" section they mention this:

Try the command YP. This makes a copy of the current line 
and leaves you on this copy, which is placed before the current line. 
The command Y is a convenient abbreviation for yy. 
The command Yp will also make a copy of the current line, 
and place it after the current line. 
You can give Y a count of lines to yank, and thus duplicate 
several lines try 3YP.

Later in the list of commands (see the appendix in the pdf version), the description for Y is this:

Y  Yanks a copy of the current line into the unnamed buffer, 
   to be put back by a later p or P; a very useful synonym for yy.

This way I am sure that Y was a feature and not a bug.

As for inconsistency with D and C - I also have a logical explanation. If you try to think as a text editor author than your main desire would be to create new text and not delete it. They (text editor authors) want their users to write more text and more and more...

So for the syntactic sugar commands (and Y, D and C are kind of syntactic sugar because they duplicate already existing features) they choose the operations which add as much text as possible (Y duplicates the whole line) or delete as less text as possible (D and C delete until the end of line and not the whole line).

  • Okay, I like YP; that is quick and easy to type. I'm convinced. ;) – Wildcard Jan 27 '16 at 20:14
6

Y was the yank command of the first vi version (ex-1.1, January 1, 1978). This version did not have the yy cammand. ex-2.2 (May 6, 1979) did have both yy and Y. So actually yy is a synonym for Y (Y predates yy).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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