adding four lines before last line of the file in gvim - vim

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.

Related

How to show line numbers in Vim every N lines?

I found a NeoVim plugin that does this: https://github.com/IMOKURI/line-number-interval.nvim
But is there a way to show line numbers every N lines in Vim? I would like to try out line numbers every 5 lines, to clear some clutter from the margin.

vim - when triggering a macro, normal mode isn't switched from insert mode (xterm)

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).

Deleting lines upward in vim Linux

How to do that in vim, lets say im in line 100 and i want to delete 20 lines upward? how to do that in vim linux?
You can do it relative if you know the number of lines:
d20k
Or absolute if you know the line number:
d80G
d = delete, 20k = 20 lines up, 80G = goto line 80
Or without taking you current position into account:
:80,100d
Personally, I use visual mode a lot becuase it gives me a nice feedback:
hit V to enter visual line mode
move 20 lines up with 20k
adjust selection with k and j if necessary
press d to delete selection
In normal mode you can use d and the movenment key k to delete upwards. And then you prefix that command with the number of times you want to repeat the command like 100dk.
If you just want to delete to the beginning you can use gg along with d, gg sends the cursor to the first character in the file. So ggd will delete the first line to the line you are standing on.
You can do it upwards with the range.
:-20,.d
In this you can specify the range of line to delete it .
Will deletes 20 lines upwards to the current and this is kind of an ex-mode command.

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.

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