1. Insert a row number in the text. a friend recently mentioned that an editor has a plug-in that can insert a row number and asked Vim if it is possible to insert a row number in the article. % $ ^ * #8 ~ #$ @#!...... Let's take a look at how many methods can be used to insert the row number or number sequence in vim! We recommend that you read this article on the Vim official website. Part of the content overlaps with this article. However
1 insert row numbers in the text
A friend recently mentioned that an editor has a plug-in that can insert row numbers and asked Vim if it is possible to insert row numbers in the article. % $ ^ & * #8 ~ #$ @#!...... Let's take a look at how many methods can be used to insert the row number or number sequence in vim!
We recommend that you read this article on the Vim official website. Part of the content overlaps with this article. But this is a Chinese XD.
We aim to add a line number or number sequence before each row in the current editing text and separate them with spaces, for example:
This is the first line
This is the second line
This is the fourth line
After the row number is inserted, it will become:
1. this is the first line.
2. this is the second line.
3
4. this is the fourth line.
But sometimes we need to number some rows. The following example numbers all non-empty rows after the second row in sequence:
This is the first line
1. this is the second line.
2. this is the fourth line.
To distinguish a row number, we call it a numerical sequence.
Go to the topic.
1.1 use the line () function
I believe this is the first solution that most people think ?? Use the line () function to generate a row number. use the "s" command to insert a row number.
"Add row numbers to all rows
"Use a suitable range or regular expression to limit the scope of the function.
: G/^/s // \ = line ('.').''/
It is easy to use. The disadvantage is that it is not flexible enough to display the row number of a specified row.
1.2 Use the ex command
Vim provides some ex commands that can print row numbers, such as, "=", "p #", and "#". To capture Vim command output, we use the redir command.
"Add row numbers to the first 30 rows
: Redir @ a | 1, 30 # | redir END
: 1, 30d | put! A
Logically, it is easy to understand: 1, 30 # display the first 30 rows and their row numbers. Save it to register a, delete the first 30 rows, and paste the Register content.
There is no obvious advantage. The disadvantage is the same as above. This method is more convenient than other methods if you save the content of the current editing area to a new file instead of changing the content of the current editing area.
1.3 use the range () function
Range () is a new function used to generate a list containing a number sequence. Of course, it can also be used to generate row numbers:
"Add row numbers to the first 30 rows
: For I in range (31)
: Call setline (I, I. ''. getline (I ))
: Endfor
For the usage of range () and setline (), see the help documentation.
The range () function is much more flexible than the previous two solutions. in addition to representing row numbers, it can also be used to compile numerical sequences, which also requires some programming basics.
1.4 addition
This is a simple and flexible solution. Well, maybe the first solution we thought of was not to use the line () function.
"Add row numbers to all rows
: Let I = 1
: G/^/s // \ = I. ''/| let I = I + 1
Note: the two commands (: s,: let) after g must be written together. This is a common numbering method that can handle most of the numbering problems.
1.5 Use
Vim provides Ctrl-A and Ctrl-X to add and subtract numbers under the cursor (or the first one on the right of the cursor. Press Ctrl-A to add one at A time, and press Ctrl-X to subtract one. If you are using Windows, it is very likely that Ctrl-A has been mapped to all. Input
: Nunmap
You can use nnoremap to remap to other keys. Please refer to the following documents:
: H CTRL-A
: H CTRL-X
: H unmap
: H o_v
Take a look at the Operation steps in normal mode:
First, manually add the sequence number and space of the first line.
2. enter 0 "ayw to put the serial number in register.
Third, move to the next row and enter:
Qq0 "aP0 ^ A" ayawjq
The preceding command stores the command sequence in register q. ^ A: press Ctrl-.
Add a row number to the next 28 rows by @ q.
Adding spaces above is an important step (because we have used yaw to copy numbers and spaces). here we will explain the third step:
'QQ'
Start recording macro and save it to register q
'0'
Set the cursor position to the beginning of the line
'"AP'
Put the sequence number and space saved above
'0 ^'
Return to the beginning of the line and press Ctrl-A to add one.
'"Ayaw'
Copy the current number and space to register
'Jq'
Move to the next row (so that we can ensure that 28 @ q is to operate on the following 28 rows rather than 28 operations on the current row), and then end the macro.
In combination with the g command, you can specify the row number. The following example numbers all rows starting with a letter. In order to facilitate the assignment of registers, the effect is the same as that in normal mode above. Note that the input method of ^ A in the following two operations is different from that of pressing the combination key directly above. press Ctrl-V first (if not, try Ctrl-Q ), press Ctrl-:
"Reset register a first.
"Number of all rows starting with a letter
: Let @ a = '0'
"J command removed
: Let @ q = '0 "aP0 ^ A" ayaw'
: G/^ \ a/norm @ q
If the separator you use is not a space, you can manually mark it:
"In this example, the colon is used as the separator.
: Let @ a = 0
: Let @ q = '0 "aPa: ^ [0 ^ Amm" ayv 'M'
: G/^ \ a/norm @ q
Macro commands are one of the most powerful tools of Vi. in Vim, macros are more powerful than ever. Macros are not the most efficient way to do this, but they are much more fun to write macros than other scripts. This solution can be applied to multiple situations after modification. The disadvantage is that debugging is performed multiple times before writing a feasible macro.