vim learning the sequence generation method for advanced skills in detail
- 2020-06-23 02:23:25
- OfStack
preface
This article mainly introduces the method of inserting number sequence in vim, and shares it for your reference and study. Let's start with a detailed introduction.
Text to be processed
Let's say that the original text is
This is the first 1 line
This is the first 2 line
This is the first 4 line
This is the first 5 line
The insert line number becomes
1 This is the first 1 line
2 This is the first 2 line
3
4 This is the first 4 line
5 This is the first 5 line
There's one other desired effect
This is the first 1 line line[1]
This is the first 2 line line[2]
This is the first 3 line line[3]
This is the first 4 line line[4]
Both of the above requirements are answered below, and more complex number sequence insertions can be achieved using the method mentioned below. Includes replacing the specified character, followed by a sequence of Numbers.
Use the line ()
line('.')
You can return the current line number
:g/^/s//\=line('.').' '/
Very simple, but not very flexible, and can only be used to display the line number of the current row
Use the ex command
vim provides 1 ex commands to print line Numbers, such as "=", "p #", "#"
To get the command output from vim, you need to use redir
" For the former 5 Row plus row number
:redir @a | 1,5#|redir END
:1,5d | put! a |1d |6d
There is an extra line before and after pasting, so use d to delete it
Use range, setline, getline
range()
A list of numeric sequences can be generated
:for i in range(6)
:call setline(i, i.' '.getline(i))
:endfor
range(6)
A list of Numbers 1-5, not 6, is generated
Variable volume plus
: let i=1 | g/^/s//\=i.' '/ | let i=i+1
This approach is more flexible and allows you to control the values inserted by controlling the value variation of i
Macro and Ctrl - A
You can increment the number by pressing ES73en-ES74en and ES75en-ES76en by 1
The process is as follows:
Manually add the sequence number and space on line 1 Enter 0"ayw to put the sequence number and space in register a Move to the next line and execute the following key
qz0"aP0^A"ayawjq
perform
4@z
To execute the sequence of commands saved in register z on the following four lines
The above command sequence is explained in detail below
If there is no space for the flag, then aw cannot be used to copy an word; v is used to select the corresponding numeric portion
Note: at the command line, ES111en-ES112en requires ctrl-ES114en, ES115en-ES116en to represent this key
Macros are more powerful under vim, but it usually takes a lot of debugging to write a workable macro
External command
seq is the command that outputs a sequence under linux
harriszh Sat 22:14@ ~/trunk/go$ seq 5 -2 1
5
3
1
harriszh Sat 22:14@ ~/trunk/go$ seq 1 5
1
2
3
4
5
harriszh Sat 22:14@ ~/trunk/go$ seq 1 2 5
1
3
5
For three parameters, the intermediate parameter is step, and the two parameters are the starting and ending Numbers. step is 1
through
r !seq 1 5
To insert a sequence of Numbers in a file, and then copy it using column mode
Using printf
Insert the line number directly
:%s/^/\=printf('%-4d', line('.'))
Where - is left aligned, the default is right aligned
Inserts a sequence at the specified row
First, V selects the block to insert the serial number
1 This is the first 1 line
2 This is the first 2 line
3
4 This is the first 4 line
5 This is the first 5 line
0
Insert sequences only on non-blank lines
Put func below into.vimrc
1 This is the first 1 line
2 This is the first 2 line
3
4 This is the first 4 line
5 This is the first 5 line
1
a:0
The equivalent of
argc
,
a:1
The equivalent of
argv[1]
g:i is the global variable i
Then select the appropriate block
1 This is the first 1 line
2 This is the first 2 line
3
4 This is the first 4 line
5 This is the first 5 line
2
range and print
The following three commands all insert sequences below the specified line
:12put = range(1,5)
:call append(12, range(1,5))
:0put = range(3,7)
The first command is equivalent to the second command
0put, just before line 1
range and print combine to output formatted results
1 This is the first 1 line
2 This is the first 2 line
3
4 This is the first 4 line
5 This is the first 5 line
4
v:val represents the values retrieved from the previous list
Using for directly is easier to understand
1 This is the first 1 line
2 This is the first 2 line
3
4 This is the first 4 line
5 This is the first 5 line
5
A sequence number is appended to a particular character
1 This is the first 1 line
2 This is the first 2 line
3
4 This is the first 4 line
5 This is the first 5 line
6
The problem with the above command, however, is that if there are multiple abc on line 1, they will all be replaced with one number and not incremented
To solve the above problem, use registers
1 This is the first 1 line
2 This is the first 2 line
3
4 This is the first 4 line
5 This is the first 5 line
7
setreg() returns 0, so use the above method to call setreg to update the value of register a
vim8 ctrl - a
vim8 offers new features for Ctrl-ES233en
Such as the
my_array[0] = 0;
my_array[1] = 0;
my_array[2] = 0;
my_array[3] = 0;
my_array[4] = 0;
my_array[5] = 0;
my_array[6] = 0;
Select 0-0 with COLUMN ctrl-V, then press g ES243en-ES244en,
Select the preceding columns 0-6, and then press g ES249en-ES250en, too
The end result is
1 This is the first 1 line
2 This is the first 2 line
3
4 This is the first 4 line
5 This is the first 5 line
9
g Ctrl-A
It works by adding 1 to the first number, 2 to the second number, 3 to the third number...
Plug-in VisIncr
The main commands are I, II, IYMD, IMDY, IA, ID, IO, IR, IX
Support for Numbers, dates, alphabetic | tables, Roman numerals, base 8, base 106 increments and decrements
:IX
Original | Select, :IX |
---|---|
8 | 8 |
8 | 9 |
8 | a |
8 | b |
8 | c |
:I
Original | Select, :I |
---|---|
8 | 8 |
8 | 9 |
8 | 10 |
8 | 11 |
8 | 12 |
:I -1
Original | Select, :I -1 |
---|---|
8 | 8 |
8 | 7 |
8 | 6 |
8 | 5 |
8 | 4 |
:II
Original | Select, :II |
---|---|
8 | 8 |
8 | 9 |
8 | 10 |
8 | 11 |
8 | 12 |
:II -1
Original | Select, :II -1 |
---|---|
8 | 8 |
8 | 7 |
8 | 6 |
8 | 5 |
8 | 4 |
:IMDY
Original | Select, :IMDY |
---|---|
06/10/03 | 6/10/03 |
06/10/03 | 6/11/03 |
06/10/03 | 6/12/03 |
06/10/03 | 6/13/03 |
06/10/03 | 6/14/03 |
:IYMD
Original | Select, :IYMD |
---|---|
03/06/10 | 03/06/10 |
03/06/10 | 03/06/11 |
03/06/10 | 03/06/12 |
03/06/10 | 03/06/13 |
03/06/10 | 03/06/14 |
:IDMY
Original | Select, :IDMY |
---|---|
10/06/03 | 10/06/03 |
10/06/03 | 11/06/03 |
10/06/03 | 12/06/03 |
10/06/03 | 13/06/03 |
10/06/03 | 14/06/03 |
:ID
Original | Select, :ID |
---|---|
Sun | Sun |
Sun | Mon |
Sun | Tue |
Sun | Wed |
Sun | Thu |
:ID
Original | Select, :ID |
---|---|
Sunday | Sunday |
Sunday | Monday |
Sunday | Tuesday |
Sunday | Wednesday |
Sunday | Thursday |
:IA
Original | Select, :I |
---|---|
8 | 8 |
8 | 9 |
8 | 10 |
8 | 11 |
8 | 12 |
:IO
Original | Select, :I |
---|---|
8 | 8 |
8 | 9 |
8 | 10 |
8 | 11 |
8 | 12 |
:IR
Original | Select, :I |
---|---|
8 | 8 |
8 | 9 |
8 | 10 |
8 | 11 |
8 | 12 |
conclusion