How to show line numbers in Vim every N lines? - vim

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.

Related

How to select from line to line in vi?

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)

Is there a simple way to edit a numbered list using vim markdown?

Let's say I have a numbered list like this in markdown:
1. This is a line of text.
2. This is a line of text.
3. This is a line of text.
4. This is a line of text.
Is there an easy way to insert a new line for number one and add one to all the other lines using vim? This is what I want to achieve:
1. This is a new line of text.
2. This is a line of text.
3. This is a line of text.
4. This is a line of text.
5. This is a line of text.
I want to be able to do this quickly - not having to record a macro for instance.
A completely different approach would be not to worry about manually numbering at all. Markdown doesn't care what numbers are in your source file, so
1. Foo
1. Bar
1. Baz
Renders as
Foo
Bar
Baz
I normally number all of my ordered lists with 1. and let the Markdown processor take care of actual numbering.
There's a couple ways you could do this. Probably the simplest way is to add a newline starting with a '0.'. Then, when you are happy with this list you can increment every number with vG<C-a>. (Of course, it's not the whole buffer you could use a different movement such as ip or })
If you are going to add a line many times, it might be easier to add all of the numbers at the end when you are done editing. This could be done by visually selecting the lines you want and doing g<C-a>.
I don't remember exactly when these feature's were added, but this requires a relatively new version of vim. It works for me in 8, and I remember this working for 7.4 with the right patches.
If you are on an older version of vim, you could select the lines you want and use a substitute command to add the appropriate numbers. For example:
:s/^/\=(line('.') - line("'<") + 1).'. '
This: You can use the command below after selecting those lines by V4j starting from first line:
:'<,'>s/\d/\=submatch(0)+1/ | normal! goO1. This is a new line of text.
Or: Put the cursor on first line and type:
O0. This is a new line of text.Esc0Ctrl-v4jCtrl-a
O : letter o in capital case (shift+o)
0 : digit 0

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.

vim text width reformat

I want to reformat my file so the max line width is 79. I did :set tw=79 and gggqG and the results weren't what I expected. When a line is less than 79 col, characters from the line below it move up and lines with over 79 col don't break into two lines.
edit: Well I was semi-mistaken in that it DOES break lines over 79 except in the line with asterisks.
--CONVENTIONS**************************************************************************************
In addition it still moves characters up when the line has <79.
One possible solution, although not the best one.
Undefine formatexpr and let external fold program to format your text to 79 characters width.
:set formatexpr=
:set formatprg=fold\ -w\ 79
And now:
gg # Go to beginning of file.
gq # Format until...
G # End of file.
And last remove those carriage returns (^M):
:%s/\r//g
In my test it changed some accented characters and some other lines were mangled, but try it yourself. Else you may write your own format function and use it with formatexpr option.
Put empty line between different paragraphs. Then do the reformat.

adding four lines before last line of the file in gvim

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.

Resources