Tim
Tim

Reputation: 14164

Yank n lines upwards without moving

To yank 7 lines downward without moving the cursor, I can 7yy. Is it possible to do the same upwards, not using macros or remapping?

Upvotes: 13

Views: 6004

Answers (3)

Peter Rincker
Peter Rincker

Reputation: 45177

You can use the :yank command with a range to accomplish this effect.

:.-6,.yank

The range explanation:

  • . or the dot means current line
  • .-6 means current line minus 6
  • .-6,. is current line minus 6 to the current line
  • This can be abbreviated .-6 to just -6 giving us -6,.yank
  • the current line is also assumed in the end of the range so -6,yank
  • the yank command can be shortened to just :y giving us -6,y

Final command:

:-6,y

For more help:

:h :yank
:h [range]

Upvotes: 31

sidyll
sidyll

Reputation: 59307

You could simply yank to a motion and then return the cursor to the position using either '[ or '].

The yank for 6 lines up, plus the current gives 7 in total:

y6u

Then, use some lesser known marks:

'[ -> to the first character on the first line of
      the previously yanked text (or changed)
`[ -> to the first character of the previously yanked text
'] -> to the first character on the last line of yanked text
`] -> to the last character of the preciously yanked text

So:

y6u']
y6u`]

Are two solutions you could use depending on what exactly you want. The former moves the cursor back to the first character on the line your cursor was, and the latter moves to the last character on that line.

But there is another mark that might be handy: '^. It means the last position the cursor was when leaving insert mode.

'^ -> moves to the beginning of the last line when leaving insert mode.
`^ -> moves to the exact position where insert mode was last left.

Then here are two other solutions:

y6u'^
y6u`^

That's not the end! If you pretend to continue inserting text, you can use the gi command. It moves you to the `^ mark and enter insert mode. Then we have a fifth solution:

y6ugi

I hope one of these meets your needs!

Upvotes: 6

Felix Kling
Felix Kling

Reputation: 817128

You could do the following:

6yk6j

This is will yank the 6 preceding lines and the current one) but the courser will move. 6j jumps back to the previous position.

Upvotes: 5

Related Questions