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.
Related
I frequently want to select multiple lines in vi. e.g. from line 1 to line 10.
So, what I usually do when I want to jump from line to line is I type :110 to jump to line 110, e.g.
And, when I want to select from line to line, I usually press v to get into visual mode, and then I just scroll down using k or l.
So, intuitively it makes sense to me to just press v, and then type :<line number>. but that doesn't work.
How to select from line X to line Y in vi?
Let's assume you want to highlight from line 10 to line 20. You can use:
10GV20G
Breakdown:
10 enters 10 into the buffer
G goes to the line number in the buffer
V enters visual line mode
20 enters 20 into the buffer
G goes to the line number in the buffer
Note that G means Shift+g (capital G).
Source and a : command are here.
Selection by itself isn't meaningful; you usually want to invoke a command on the selection. Many commands that work on the visual selection have a corresponding Ex command. With that, going through visual mode is unnecessary if you already know the exact ranges. The great benefit of visual mode is that you can interactively and iteratively adapt the selected area if there's no single motion or text object.
The benefit :help :range is that you can succinctly specify the lines. For example, lines 110 to 120 can be written as :110,120, but also as :110;+10.
simple just press Shift v line number gg
example: your current line to line 41
Just press Shift v 41 gg
note: you can move to selected line by press line number gg
If you set both number and relative number it becomes easy to see the target end line.
:set number relativenumber
So, let's say you are at the line 10 and the target line shows 11, you start your selection with capital V, then press 11j
V11j
If your block has blank lines before and after, just type vip (visuall inner paragraph)
So in working http://www.vimgolf.com/challenges/54862fbb3f90ac0002904cf5 one solution is:
)3:wq!<CR>
My vim cheat sheet says:
) is "end sentence". Not sure how the "3" fits in.
But "wq!" I know as write/quit.
What is this set of keystrokes doing?
Command
)3:wq!<CR>
Input
Leave only the
numbered lines.
LINE 1
LINE 2
LINE 3
That's all.
Thank you
very much.
Breakdown
) goes one sentence forward. This positions the cursor at LINE 1
3 starts a range of 3 lines for the next command.
:wq! writes the range to the file.
You should notice that when typing :, the range get's set to .,.+2
I'm not sure where to place the : as it switches from normal mode to command mode so following breakdown is equally valid
) goes one sentence forward. This positions the cursor at LINE 1
3: starts a range of 3 lines and enters command line mode
wq! writes the range to the file.
You should notice that when typing :, the range get's set to .,.+2
) puts the cursor after the current sentence, on LINE 1.
3:command is expanded by Vim to :.,.+2command, which means "execute command on the current line and the two next lines".
.,.+2wq! writes only the given lines to file, effectively removing any other line from the buffer/file.
See :help range and :help :write.
For testing purpose I created simple macro which wraps current line into single quotes and goes to next line.
Here is output from the register the macro is saved in: I'^[A'^[j
And here is testing text:
Line number 1
Line number 2
if I trigger the macro on the line number 1, cursor position should be changed to the line number 2 and the text should be changed to:
'Line number 1'
Line number 2{CURSOR_POSITION}
Instead of the expected result, vim stays in insert mode at the end of line 1 and result is following:
'Line number 1'ê{CURSOR_POSITION}
Line number 2
...where {CURSOR_POSITION} is current cursor position
Why vim place ê character at the end of first line and doesn't go to the next line?
I got same result when I ran vim with --noplugin option.
I use xterm-256color
Vim 7.4
This is kind of a bug (discussion here). I know it is stupid but this should work :-)
I'^[A'^[1j
It is because ^[j can be interpreted as a Ctrl+V Alt+J (link here).
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.
IS there a way to add 4 lines before the last line of the file in vim?
I'm new to vim and recently found out that vim does multiline search which is awesome. Now only if I could find how to add 4 lines before the last line in the file I would totally save immense amount of time
G 4 O Esc
G jumps to the end of the file. Then press O to insert a line before that last line. Now hit Enter three times and you have four lines before the last line.