How to copy lines above the selected line in Vim - vim

I want to copy N lines above the selected position. yNy works for copying the below N lines.
What is the command for copy N line above the selected position?

yNk will copy the line you're on and the N preceding lines.

Or, use :<range>yank (see :he range for all possible uses of range)
:-3,-1y
this does precisely what you ask: yank only (e.g. 3) lines before the current line. You could
:-1y
:-2y
to yank just the previous (or pre-previous) line etc...
:1,-1y
to yank everything till the last line
:1,.y
for that including the current line (of course, you could do that with ygg)

Related

Difference between cutting lines with 10dd and d9 in vim?

If I understand correctly both commands cut 10 lines and allow you to paste them anywhere.
Are they both the same as (n-1)dd and dn+enter where n is the number of lines to be cut?
The two relevant help section are copied below.
d
["x]d{motion} Delete text that {motion} moves over [into register
x]. See below for exceptions.
dd
["x]dd Delete [count] lines [into register x] linewise.
10dd is the second one which deletes 10 lines from you current position.
d9 does nothing. d9j (or d9<CR>) is delete from the cursor to where the cursor ends up (which is9j) is nine lines below the current one. However the j or <CR> makes it linewise so the same thing is deleted.
Both of these commands delete 10 lines. so ndd is equivalent to d(n-1)j.
d9j might be easier to type than 10dd if you have set relativenumber turned on because the difference between the line you are on and the line you want to delete to are on the left hand side of your screen.
You can use d9k to delete 10 lines up from your cursor line which you can't do with dd. Or you can use dfa to delete upto and including the next a. d{motion} is more powerful than dd because it isn't restricted to only linewise deletions.
Which one you use is up to you but certain combinations are easier depending on where your cursor is.

Avoid extra whitespace when pasting vertical selection

I often need to copy larger vertical selections e.g. when working with
(let's stay civilized) "getter/setter"-rich interfaces, e.g
A very long line with something I would like to copy: ABC$
A short line with nothing$
A very long line with something I would like to copy: ABC$
Here I have used $ to indicated the end of the line. I now make a visual
vertical selection on e.g. the A in the column 55 across all lines, yank it
and paste it with P before that column and get extra whitespace inserted in
the second line.
A very long line with something I would like to copy: AABC$
A short line with nothing $
A very long line with something I would like to copy: AABC$
Is there a way to avoid the extra whitespace? Changing characters in the vertical visual
selection doesn't seem to suffer from this issue.
This is how visual-block move works. You are copying column 55. Then pasting with P. Your second line does not extend as far as column 55. So when you paste it will extend that line for you.
Use :reg to look at your register. You will noticed A^J ^JA as the value for "", the unnamed register. The ^J symbolize line breaks. Notice the space between ^J's. This is how a visual-block mode yank works with empty space, by filling it in with spaces.

vim: yank lines above the current line together with the current line

I find myself doing Nyy very often to yank the current line and N-1 lines below. So 3yy would yank the current line and 2 more lines (so all together 3).
I know how to yank N lines above the current line (yNk), but this does not include the current line. What I want is to yank the current line and N-1 lines above. How do I do this (ideally also with the yy command)?
Edit: Apparently yNk includes the current line as well. I must have missed it. Thx for the comments.
The following will yank the current line plus two above:
2yk
Obviously changing the 2 will alter the number of lines yanked above. No number is an implicit 1, so yk is equivalent to 1yk.

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?
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]
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!
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.

How do you delete all text above a certain line

How do you delete all text above a certain line. For deletion below a line I use "d shift g"
dgg
will delete everything from your current line to the top of the file.
d is the deletion command, and gg is a movement command that says go to the top of the file, so when used together, it means delete from my current position to the top of the file.
Also
dG
will delete all lines at or below the current one
:1,.d deletes lines 1 to current.
:1,.-1d deletes lines 1 to above current.
(Personally I'd use dgg or kdgg like the other answers, but TMTOWTDI.)
kdgg
delete all lines above the current one.
Providing you know these vim commands:
1G -> go to first line in file
G -> go to last line in file
then, the following make more sense, are more unitary and easier to remember IMHO:
d1G -> delete starting from the line you are on, to the first line of file
dG -> delete starting from the line you are on, to the last line of file
Cheers.
d1G = delete to top including current line (vi)
:.,$-3d deletes from current line to 3 lines from end

Resources