Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yank n lines upwards without moving

Tags:

vim

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?

like image 751
Tim Avatar asked Mar 28 '11 14:03

Tim


People also ask

How to yank specific lines in vim?

To yank one line, position the cursor anywhere on the line and type yy . Now move the cursor to the line above where you want the yanked line to be put (copied), and type p . A copy of the yanked line will appear in a new line below the cursor.

How do I copy 5 lines from current cursor position in Linux?

Place the cursor on the line you wish to copy. Type yy to copy the line. Move the cursor to the place you wish to insert the copied line. Type p to insert the copied line after the current line on which the cursor is resting or type P to insert the copied line before the current line.


2 Answers

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]
like image 80
Peter Rincker Avatar answered Oct 20 '22 11:10

Peter Rincker


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!

like image 21
sidyll Avatar answered Oct 20 '22 12:10

sidyll