The simplest way to convert a string of decimal numbers to hexadecimal numbers using Vim is as follows:
: % S/\ D \ +/\ = printf ("% x", submatch (0)/g
The principle of this command is to replace a string of numbers with the output of the printf () function. The output of the printf () function is the hexadecimal form of the numbers.
The decomposition is as follows:
% S Replace (: Help: S) in the entire file)
\ D \ + matches one or more numbers (: Help/\ D: Help/\ +)
\ = Replace with the expression result (: Help/\ W)
Printf outputs in the specified format (: Help printf ())
Submatch () returns the specified matching string (: Help submatch () in the S command ())
G replaces all matching results in the row (: Help: s_flags)
It seems that the clever use of replacement commands can accomplish many unexpected functions!
The following tips are available in the vim email list.
The C language program is used as an example. Assume that the code we finally want to complete is as follows:
# Define bit_mask_1 (1 <0)
# Define bit_mask_2 (1 <1)
# Define bit_mask_3 (1 <2)
# Define bit_mask_4 (1 <3)
# Define bit_mask_5 (1 <4)
# Define bit_mask_6 (1 <5)
# Define bit_mask_7 (1 <6)
# Define bit_mask_8 (1 <7)
# Define bit_mask_9 (1 <8)
# Define bit_mask_10 (1 <9)
# Define bit_mask_11 (1 <10)
# Define bit_mask_12 (1 <11)
# Define bit_mask_13 (1 <12)
# Define bit_mask_14 (1 <13)
# Define bit_mask_15 (1 <14)
# Define bit_mask_16 (1 <15)
# Define bit_mask_17 (1 <16)
# Define bit_mask_18 (1 <17)
# Define bit_mask_19 (1 <18)
# Define bit_mask_20 (1 <19)
# Define bit_mask_21 (1 <20)
# Define bit_mask_22 (1 <21)
# Define bit_mask_23 (1 <22)
# Define bit_mask_24 (1 <23)
# Define bit_mask_25 (1 <24)
# Define bit_mask_26 (1 <25)
# Define bit_mask_27 (1 <26)
# Define bit_mask_28 (1 <27)
# Define bit_mask_29 (1 <28)
# Define bit_mask_30 (1 <29)
# Define bit_mask_31 (1 <30)
# Define bit_mask_32 (1 <31)
We do not need to write a row. We only need to write the first line first, as shown below:
# Define bit_mask_1 (1 <0)
Then, we return to normal mode, input "y31p" on this line, copy this line, and paste it 31 times. In this way, we get a total of 32 rows above the content.
Use the "v31j" command to select the 32 lines, and then use the replacement command twice:
: '<,'> S/bit_mask _ \ Zs \ D * \ ze/\ = line (".")-line ("'<") + 1
: '<,'> S/\ Zs \ D * \ ze) $/\ = line (".")-line ("'<")
In this way, we get the expected results.
This method can also be used for automatic addition of the array subject and automatic numbering of chapter in the text. If you can use a regular expression to accurately locate the number you want to automatically number, you can use this method to automatically number.
Take the first command as an example. The second command is similar to the first one:
: '<,'> S/bit_mask _ \ Zs \ D * \ ze/\ = line (".")-line ("'<") + 1
This command is replaced in the selected area. Search for strings starting with "bit_mask _" followed by any number and place the matching position on the number, then, replace the matched numbers with the numbers calculated by the following expression.
The following is the meaning of each element in this command:
'<,'> Region selected (: help' <,: help'>)
S is replaced in the selected area (: Help: S)
\ Zs indicates that the matching starts from this (: Help/\ ZS)
\ D * search for any number (: Help/\ D)
\ Ze indicates that the match ends here (: Help/\ ze)
\ = Specifies the following expression (: Help: S \ =)
Line (".") the row number of the current cursor (: Help Line ())
Line ("'<") the row number of the first row in the selected region (: Help Line ())
"'<" And "'>" is the TAG set by VIM after we use the "V" and "V" commands to select a visual area, identifies the start and end of the visual area.
"Bit_mask _ \ Zs \ D * \ ze" is a regular expression used to find strings starting with "bit_mask _" followed by any number. "\ Zs" and "\ ze" are used to specify the start and end positions of the match, because we only intend to replace the number in "bit_mask_0, therefore, only the matching area is set to a number during search.
Because we need to replace the numbers of different rows with different values, we need to use an expression to calculate the value after replacement. When the replacement string of the ": s" command starts with "\ =", it indicates that the result calculated using an expression is replaced. The expression here is "line (".")
-Line ("'<") +
1 ", where the" line () "function is used to obtain the row number, which can obtain the row number of the current row and the row number of the specified mark. "Line (". ")" is used to obtain the row number of the row where the current cursor is located, and "line (" '<")" is used to obtain the row number marked by "' <. The difference between the two row numbers plus 1 is the value we want to replace.
In the above example, we use the replacement function of VIM to implement efficient code writing. Another method is introduced to implement the same function.
Let's first look at the example:
Uniqueid2 = lview. focuseditem. subitems. opindex (0). text;
Parent = lview. focuseditem. subitems. opindex (0). text;
Children = lview. focuseditem. subitems. opindex (0). text;
Login = lview. focuseditem. subitems. opindex (1). text;
Txtcust. Text = lview. focuseditem. subitems. opindex (2). text;
Txtproj. Text = lview. focuseditem. subitems. opindex (3). text;
Txtbdate. Text = lview. focuseditem. subitems. opindex (4). text;
Txtddate. Text = lview. focuseditem. subitems. opindex (5). text;
Txtedate. Text = lview. focuseditem. subitems. opindex (6). text;
Txtpm. Text = lview. focuseditem. subitems. opindex (7). text;
Txtlang. Text = lview. focuseditem. subitems. opindex (8). text;
Txtvendor. Text = lview. focuseditem. subitems. opindex (9). text;
Txtinvoice. Text = lview. focuseditem. subitems. opindex (10). text;
Txtpmfund. Text = lview. focuseditem. subitems. opindex (11). text;
Txtprojfund. Text = lview. focuseditem. subitems. opindex (12). text;
Txta_no.text = lview. focuseditem. subitems. opindex (13). text;
Txtnotes. Text = lview. focuseditem. subitems. opindex (14). text;
Txtstatus. Text = lview. focuseditem. subitems. opindex (15). text;
We need to replace the number in the brackets in the above Code with an ascending sequence starting from 0, for example:
Uniqueid2 = lview. focuseditem. subitems. opindex (0). text;
Parent = lview. focuseditem. subitems. opindex (1). text;
Children = lview. focuseditem. subitems. opindex (2). text;
......
To meet the preceding requirements, you can also use the following command:
: Let n = 0 | G/opindex (\ Zs \ D \ +/S // \ = N/| Let N + = 1
This command is similar to the command described in the previous article. It also uses the vim replacement function and expression. The difference is that it does not need to select the replacement region in advance because it does not use line () "function to calculate the offset of the current position, but directly use variables to assign values.
The following describes the components of this command:
Let assigns a value to the variable (: help let)
| Used to separate different commands (: Help: bar)
G: Execute the specified ex command (: Help: G) in the line matching the following pattern)
\ Zs indicates that the matching starts from this (: Help/\ ZS)
\ D \ + query one or more numbers (: Help/\ D)
S replaces the matching mode (: Help: S)
\ = Specifies the following expression (: Help: S \ =)
Therefore, the execution process of this command is:
1. Assign the Variable N 0;
2. Search mode "opindex (\ Zs \ D \ +", replace the matching mode string with the value of Variable N;
3. Add 1 to Variable N;
Note: "|" is used to separate different commands.
In addition, if the match mode string is omitted in the substitute command, it uses the previously defined match mode string, in this example, "opindex (\ Zs \ D \ +" defined by the "Global" command ".
In addition to the method described above, there is also a Vim plug-in specifically to add and subtract numbers and dates. You can download this plug-in at the following URL:
Http://vim.sourceforge.net/scripts/script.php? Script_id = 1, 670
Or
Http://mysite.verizon.net/astronaut/vim/index.html#VISINCR