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.
Related
I am reading a file which has 2 unwanted spaces in each line.
import uvm_pkg::*;
`include "uvm_pkg.svh"
And it continues till last line
How to delete first 2 white spaces in each line in gvim?
Enter :%s/^ //, which substituttes 2 leading whitespaces (^ ) with nothing (the nothing between the second and third /) on every line (%).
position cursor at line 1, column 1 (press gg0)
enter visual block mode (Ctrl+v)
select first two columns of all lines by moving the cursor to the bottom and one to the right (Gl)
delete selection (x)
:%s/^ //
substitutes the two first leading spaces on every line of the buffer with nothing, effectively deleting them.
Note that, in this case, you don't even need the replacement part because it is implied: :%s/^ .
See :help :s and :help :range for the %.
I have relative line numbers turned on.
I can yank line 10 using :10y.
But how to I yank say the 5th line below the current line without jumping to said line, yanking and jumping back (i.e. 5jY5k).
If I had this file:
2 describe 'foobar' do
1 it 'should be cool' do
46 # do stuff
1 end
2 end
I am on line 46 and I want to yank relative line 1 or 2, either above or below.
You can use +n and -n for relative addresses:
:+2y " Two lines after the current line
:-2y " Two lines before the current line
And you can also combine this:
:-2,+2y " Two lines before the cursor and two lines after
Also see this answer for some more examples and :help [range] for the Vim documentation.
Given text like this:
This is line one.
This is line two.
I would like to change it to this:
This is line one. This is line two.
If my cursor is at the end of line one, how is this done with vim, regardless of what line two starts with? Also, are there different ways to do it with/without whitespace?
Put your cursor anywhere on line 1, then type J.
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)
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.