The simplest way to convert a string of decimal numbers to hexadecimal numbers using VIM is as follows: % sd + printf (% X, submatch (0 )) the principle of the g 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 number. Decomposition: % s in
The simplest way to convert a string of decimal numbers to hexadecimal numbers using VIM is as follows: % s/d/+ // = printf (% X, the principle of the submatch (0)/g command is to replace a string of numbers with the output of the printf () function, printf () the function outputs The hexadecimal form of the numbers. Decomposition: % s in
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/+ match 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 "'>" are the labels 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 specified mark (mark) the row number. "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
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/+ search for 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/+" and 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