Reputation: 20116
I had up to now always yanked or deleted 2 lines with y2y
or d2d
.
I just discover that you can also do y1CR
(where CR is enter). Apart from number 1 appear on the command, it actually yanks two lines.
It also displays on the bottom 2 lines yanked
, which don't happens using y2y
.
I can't find any mention of enter on vim help. This lead me to two questions:
Edit: While there are many answers, no one addresses if y2j
and y1<CR>
are really equivalent on every case, if they are, why the 2 lines yanked
only appears on bottom only for the <CR>
command.
Upvotes: 3
Views: 777
Reputation: 58578
Vi commands are a combination of a count, a motion, and an action. (At least one of the latter two must be present: a motion or action. Otherwise you have a lingering count, waiting for more input.)
y1CR
means that the count is 1, the motion is "go to the start of the next line", and the action is "yank". "Go to the start of the next line (doing that just once), and yank the lines which are spanned by the motion". Of course, a count of 1 is superfluous.
With regard to motion and action being combined, you have probably noticed that they do not combine literally; certain combinations follow special rules. For instance the
w
motion goes to the start of the next word, and thed
action (delete) combines withdw
in such a way that the word under the cursor, and the space right up the the next word are all deleted. However, thecw
combination (change word) only deletes the word up to the whitespace which follows, leaving the whitespace. In that case,c
is not acting on the precise motion carried out byw
but on an adjusted motion that often makes more sense withc
.
I cannot reproduce the behavior that y2j
doesn't print the number of lines yanked. In Vim 7.3 on Ubuntu, 3yy
, y3y
, y2CR
and y2j
all report 3 lines yanked
(if at least 3 lines exist). All these commands are silent if fewer than 3 lines are yanked. (For instance, they are executed too close to the last line of the buffer, where only two or fewer lines remain, or the repetition counts are reduced to 2yy
, y2y
, y1CR
and y1j
respectively.)
Vim appears to have a three line threshold for reporting this status message. This could have changed between versions.
Upvotes: 5
Reputation: 263267
Enter moves from the current line to the following line, so its range covers two lines.
Commands like y
combined with a movement command are applied to the range of text specified by the movement command. For example, w
moves from the current position forward to the beginning of the next word, so yw
yanks all the text in that range.
Similarly, y
Enter applies to the two lines covered by the Enter command -- and since that motion is line-oriented, it applies to the two entire lines, not to some subset of them affected by the starting position.
Upvotes: 4
Reputation: 9946
another way to do that is simply y<CR>
. and i often actually use things like yj
or dk
.
Upvotes: 1
Reputation: 196556
{count}<CR>
means "go down {count}
lines".
It's really not that hard to find: see :help <CR>
.
I tend to prefer 2yy
or 2dd
; it's semantically cleaner and easier to type. From a grammatical perspective, y2y
sounds weird: "yank two times yank" whereas 2yy
sounds almost like plain (yoda) english: "two times yank".
Upvotes: 5