Magical Vim
content:
An example of an example of three cases of four cases of five cases about the author
In today's world, the text editor has a wide variety, and there is a "chaotic flower and charming eye". There is an old saying in China: Hand is not as good as the family, as a professionals of IT, choose an excellent editing software. The author believes that the Vim ※ under Linux will benefit your life with its powerful functions and endless charm.
Due to extensive transplantation, whether the PC's DOS and Windows is also the RISC / 6000 AIX, and even the IBM's large machine S / 390 can see the figure of Vim. However, for beginners, Vim user interfaces and use methods are very unconventional, and even think that it is more confusing, we are obeyed. In fact, the VIM editor is designed specifically for experienced users, its interfaces and usage methods provide faster speeds and stronger features. For users who are familiar with it, many of the VIM saves time and keystrokes, and can complete some other editors that cannot be completed. The best way to learn is to practice, only so, in order to truly master the essence. The examples listed in the article are all encountered in practical work. They have a certain representation, please carefully understand in the process of reading. Ok, let us travel together to travel together in the world of magic VIM! Example 1, two common instruction sequences
XP swaps the position of the two characters at the cursor. DDP exchanges the position of two lines at the cursor up and down. Example 2, repeatedly input the same character, we may want to enter the same character multiple times, Vim's insert function can complete this work command 80i = ^ ESC can enter 80 characters =, of course, 80A = ^ ESC The above functions can be completed. Note: The ^ ESC here is the ESC button on the upper left side of the keyboard. Example 3, merge two text data files, and give the scale data file 1 content as follows: 1 ----- 2 ----- 3 ---- Data file 2 content as follows: 1 = ==== 2 ===== 3 ===== Requirements the results are as follows: | -------- 1 --------- 2 --------- 3 --------- 4 --------- 51 ----- 1 ===== | -------- 1 -------- -2 --------- 3 --------- 4 --------- 52 ----- 2 ===== | ------ --1 --------- 2 --------- 3 --------- 4 --------- 53 ----- 3 = ==== Maybe you will say, this is not simple, nothing more than repeated copy, paste, any text editor can complete the above functions. However, if these two files are large, each file is thousands of lines, I am afraid simple copy, it is difficult to stick. Therefore, what we care is to find a way to go, leave the boring work to the computer, we only need to publish the instruction.
To achieve this, please perform: (1), merge two files, the result is as follows 1 ----- 2 ----- 3 ---- 1 ===== 2 ===== 3 ===== (ii), insert the marker line in the two files, it is used to distinguish between two files, this article is a whole! Character 1 ----- 2 ----- 3 ----- !!!!!!!!!!!!!!!!!!!!!!!!! 1 ===== 2 ===== 3 ===== ㈢, enter the ruler below the mark line 1 ----- 2 ----- 3 ----- !!!!!!!!!!!! !!!!!!!!!!!! | -------- 1 --------- 2 --------- 3 -------- -4 --------- 51 ===== 2 ===== 3 ===== ㈣, execute the macro command script MERGE_2R.VIM, that is, press the following key in the VIM editor: Merge_2r.vim Enter, press the = button on the keyboard, the result of the execution is as follows | -------- 1 --------- 2 --------- 3- -------- 4 --------- 51 ----- 1 ===== | -------- 1 --------- 2 --------- 3 --------- 4 --------- 52 ----- 2 ===== | -------- 1 ---------- 2 --------- 3 --------- 4 --------- 53 ----- 3 === == | -------- 1 --------- 2 --------- 3 --------- 4 -------- -5 !!!!!!!!!!!!!!!!!!!!!!!! | -------- 1 --------- 2 ---- ----- 3 --------- 4 --------- 5 ㈥, delete the last three lines, you can get the results we need | -------- 1 --------- 2 --------- 3 --------- 4 --------- 51 ----- 1 === == | -------- 1 --------- 2 --------- 3 --------- 4 -------- -52 ----- 2 ===== | -------- 1 --------- 2 --------- 3 ------- --4 --------- 53 ----- 3 ===== How is it, simple? Please try it myself. Below, I will explain the macro command script MERGE_2R.VIM. The script content is as follows: "------------------------------------------ ---------------------------------------------------------------------------------------------------------------------------------------
"Macro Function: Merge File1 and File2, Have Ruler in Every Record
Date: 2001/12/01
Author: yan shi
------------------------------------- -------------------
"1-----
"2 -----} Sample File1
"3 -----
"!!!!!!!!!!!!!!!!!!!!!!!! flag rot" | -------- 1 --------- 2-- ------- 3 --------- 4 --------- 5 Ruler
"1 =====
"2 =====} Sample file2
"3 =====
------------------------------------- -------------------
:1
: map = ma / !!!!! ^ m :. Co 'a-1 ^ m / !!!!! ^ m2 :. M'a ^ m =
Everyone in the first 14 lines is "started," indicating that the line is noted, actually does not execute, but it is convenient to read, only the last two lines are real code rows. Please note: ^ m in this example indicates the keyboard Enter key, not ^ and M two characters. In order to explain, I decompose the command line and explain one by one. First, set the first line to the current line, then execute the map command, give a large string VIM command image Character =. This big string VIM directive is divided into 9 steps:
MA marks the first line of the data file as a1 ----- 2 ----- 3 ----- !!!!!!!!!!!!!!!!!!!!! !!! | -------- 1 --------- 2 --------- 3 --------- 4 ------- --51 ===== 2 ===== 3 ===== / !!!!! ^ m find the sign line, set to the current line 1 ----- 2 ----- 3- ---- !!!!!!!!!!!!!!!!!!!!!!!! | -------- 1 --------- 2-- ------- 3 --------- 4 --------- 51 ===== 2 ===== 3 ===== Cursor moved down , That is, set the ruler to the current row 1 ----- 2 ----- 3 ----- !!!!!!!!!!!!!!!!!!!!!! ! | -------- 1 --------- 2 --------- 3 --------- 4 --------- 51 ===== 2 ===== 3 =====: .CO 'A-1 ^ m Copy the ruler row to the marker row (the first line of the data file) | ---- ---- 1 --------- 2 --------- 3 --------- 4 --------- 51 ----- 2 ----- 3 ----- !!!!!!!!!!!!!!!!!!!!!!!! | ------ 1 ---- ----- 2 --------- 3 --------- 4 --------- 51 ===== 2 ===== 3 == === / !!!!! ^ m to find the sign again, set to the current line | -------- 1 --------- 2 --------- 3 --------- 4 --------- 51 ----- 2 ----- 3 ----- !!!!!!!!!!!!! !!!!!!!!!!! | -------- 1 --------- 2 --------- 3 --------- 4 --------- 51 ===== 2 ===== 3 ===== 2 cursor down 2 rows, that is, the first line of the data file 2 is current line | ------- 1 ------------------- 3 --------- 4 --------- 51-- --- 2 ----- 3 ----- !!!!!!!!!!!!!!!!!!!!!!!! | -------- 1- -------- 2 --------- 3 --------- 4 --------- 51 ===== 2 ===== 3 =====: .m'a ^ m Move the first row of data files to the lower part of the marker line | -------- 1 --- ------ 2 --------- 3 --------- 4 --------- 51 ---- 1 ===== 2- ---- 3 ----- !!!!!!!!!!!!!!!!!!!!!!!! | -------- 1 ------ --- 2 --------- 3 --------- 4 --------- 52 ===== 3 ===== Cursor moved down , That is, the second line of the data file is the current line | -------- 1 --------- 2 --------- 3 ------- --4 --------- 51 ----- 1 =====
2 ----- 3 ----- !!!!!!!!!!!!!!!!!!!!!!!! | ------ 1 ---- ----- 2 --------- 3 --------- 4 --------- 52 ===== 3 ===== = this step It is a critical, is a typical recursive call, repeatedly completed the above step Example 4, in the file, we sometimes want to place the line number into the file, and the function provided by Vim: set NU can only display the line number , Cannot edit or place it into files, the following macro command script row_num.vim can complete this feature. "--------------------------------------------------------------------------------------------------------------------------------------------------------------------
"Macro Function: Source File Add Row_num
Date: 2001/12/01
Author: yan shi
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------
:% s / ^ / ^ I /
: $
: let end = line (".")
:1
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------
: Let Num = 1
: While Num <= End
: let line = getLine (".")
: Let Temp = Substitution (Line, $, NUM, "")
: Call setLine (".", TEMP)
:
: Let Num = Num 1
: Endwhile
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------
Note: ^ i in this example indicates the Tab key on the keyboard, not ^ and i two characters. Below, I will explain one by one for the macro command script.
:% s / ^ / ^ i / Each line of the first tab character Add a TAB character
: That of the last line of the file
: Let end = line (".") The line number ==> variable end, the function line function is to obtain the line number of the specified line, here parameters "." means the current line
: 1 to the first line of file
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------
: Let Num = 1 1 ==> Counter
: While Num <= End
: let line = getLine (".") Take the content of the forward ==> Variable line
: Let line = substeute (line, $, num, "") in front of the variable line
: Call setline (".", line) writes the contents of the variable line back to the current line
: Move a line
: Let Num = Num 1 counter adds one
: EndWhile loop execution until the end of the file
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------
About regular expressions using many tools under UNIX / Linux are powerful, flexible, because there is a regular text method and element character, which is also the essence of VIM or even UNIX / Linux system. Because it is flexible, it is more difficult to understand, if it is not really understanding, there will be a tensile mistake in the actual use. Therefore, it is necessary to spend more about this part of the knowledge. The following is explained in conjunction with specific instances. Example 5, there is a document, including the information of Chinese employees of a foreign company, the first is the name, then two spaces, followed by 15 identity card numbers. Zhang.fei 430759701022003
Diao.chan 651302801225012
Guan.yu 342869680413001
Xi.shi 120638780214006
Liu.bei 210324650708001
Now, there is a problem to solve:
According to the habit of foreigners, it should be named before, surname behind. Therefore, the name field in the file needs to be modified. The first letter of the surname and the name should be capitalized. According to the ID number, you can also judge the birthday month, add it as a new field. Depending on the ID number, you can judge gender. If you have a male, add Male, if you are female, add female. Separate men and women, male employees in front, after the employee is behind. The final result of the left alignment of each field data is as follows:
Fei.zhang 430759701022003 1970/10/22 Male
Yu.Guan 342869680413001 1968/04/13 Male
Bei.Liu 210324650708001 1965/07/08 Male
-----------------------------------------------
Chan.Diao 651302801225012 1980/12/25 female
Shi.xi 120638780214006 1978/02/14 Female
In order to complete the above functions, simply perform scripting Employee.vim, how to use: so Employee.vim Enter. The script content is as follows:
:% S / / /
:% s // (........... /) / (* /) // 1 /
:% s // ([A-ZA-Z] * /) / (/.) / ([A-ZA-Z] [A-ZA-Z] * /) /// U / 3/2 / u / 1 /
:% s / $ / xxxxxx /
:% s // ([0-9] / {6} / ([0-9] / {6} /) / ([0-9] / {3} / (xxxxxx /) // 1/2/3 / 2 /
:% s // (../ )/ (/) / (../) $ 19/1 /// 2 /// 3
:% s / $ / xxxxxx /
:% s // ([0-9] / {14} [13579] /) / (. * /) / (xxxxx /) // 1 / 2male /
:% s // ([0-9] / {14} [02468] /) / (. * /) / (xxxxx /) // 1 / 2FEMALE /
: $
: s /.*//& ------------------------------------------------------------------------------------------------------ --------
: g / female / .m $
In this script, a large number of regular expressions are used, which is only a brief introduction to the regular expressions involved. The content of the regular expression is quite, this article is impossible to occupy a large number of parametric narratives, please understand. The% address range symbol, all rows in the file, equivalent to the address range 1, $. Match with any single character (except for the stroke), such as Y.S, can match Yas Y.S or Y S, etc.. * Match with 0 or more times of the previous character, such as Y * S can match Yys YYYYYS or S, etc.. $ Match with the line. & A string that appears in the mode match, such as S / ABC / & DEF, replacing the ABC of the current row to Abcdef. [] Match the characters in [], for example [ABC] match characters A, B or C, [A-ZA-Z] match all English characters. The content that appears between / (/) / (and /) can be replaced by / NUM. / 1/2/3 Alternative / (and /) between the contents. / U write the first letter of subsequent strings. / {NUM} matches the NUM times of the previous character. Now, we explain the script and hope to help you understand the regular text law. (1):% S / / / 2 spaces that appear in each line in the file are 10 spaces. Zhang.fei 430759701022003
Diao.chan 651302801225012
Guan.yu 342869680413001
Xi.shi 120638780214006
Liu.bei 210324650708001
(2):% s // (.......... /) / (* /) // 1 / Reserved 12 characters of the lead, delete the rest, so that the first two fields It is aligned.
Zhang.fei 430759701022003
Diao.chan 651302801225012
Guan.yu 342869680413001
Xi.shi 120638780214006
Liu.bei 210324650708001
(3):% s // ([A-ZA-Z] * /) / (/./) / ([A-ZA-Z] [A-ZA-Z] * /) / / U / 3/2 / U / 1 / interchange the employee names in the file, and write the initials.
Fei.zhang 430759701022003
Chan.Diao 651302801225012
Yu.guan 342869680413001
Shi.xi 120638780214006
Bei.liu 210324650708001
⑷:% s / $ / xxxxxx / add 2 spaces and 6 X at the end of each line
Fei.zhang 430759701022003 xxxxxx
Chan.Diao 651302801225012 xxxxxx
Yu.guan 342869680413001 xxxxxx
Shi.xi 120638780214006 xxxxxx
BEI.LIU 210324650708001 xxxxxx
⑸:% s // ([0-9] / {6} /) / ([0-9] / {6} / ([0-9] / {3} / (xxxxxx /) / / 1/2/3/2 / Replace XXXXXX with the birthday date.
Fei.zhang 430759701022003 701022
Chan.Diao 651302801225012 801225
Yu.guan 342869680413001 680413
Shi.xi 120638780214006 780214
Bei.liu 210324650708001 650708
⑹:% s // (../ )/ (/ )/ (../) $ 19/1 ////// 3 will be separated by the annual month / character, and add it before 19. Fei.zhang 430759701022003 1970/10/22
Chan.Diao 651302801225012 1980/12/25
Yu.Guan 342869680413001 1968/04/13
Shi.xi 120638780214006 1978/02/14
Bei.liu 210324650708001 1965/07/08
⑺:% s / $ / xxxxxx / add 2 spaces and 6 X at the end of each line
Fei.zhang 430759701022003 1970/10/22 xxxxxx
Chan.Diao 651302801225012 1980/12/25 xxxxxx
Yu.Guan 342869680413001 1968/04/13 xxxxxx
Shi.xi 120638780214006 1978/02/14 xxxxxxx
Bei.liu 210324650708001 1965/07/08 xxxxxx
⑻:% s // ([0-9] / {14} [13579] /) / (. * /) / (Xxxxx /) // 1 / 2Male / ID number The last is odd, replacing xxxxxx Male
Fei.zhang 430759701022003 1970/10/22 Male
Chan.Diao 651302801225012 1980/12/25 xxxxxx
Yu.Guan 342869680413001 1968/04/13 Male
Shi.xi 120638780214006 1978/02/14 xxxxxxx
Bei.Liu 210324650708001 1965/07/08 Male
⑼:% s // ([0-9] / {14} [02468] /) / (. * /) / (Xxxxx /) // 1 / 2FEMALE / ID number The last is even, and the XXXXXX is replaced. Female
Fei.zhang 430759701022003 1970/10/22 Male
Chan.Diao 651302801225012 1980/12/25 female
Yu.Guan 342869680413001 1968/04/13 Male
Shi.xi 120638780214006 1978/02/14 Female
Bei.Liu 210324650708001 1965/07/08 Male
⑽: $ to the last line of the file ⑾: s /.*//& -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------- Insert a line "-" character in the last line of the file.
Fei.zhang 430759701022003 1970/10/22 Male
Chan.Diao 651302801225012 1980/12/25 female
Yu.Guan 342869680413001 1968/04/13 Male
Shi.xi 120638780214006 1978/02/14 Female
Bei.Liu 210324650708001 1965/07/08 Male
-----------------------------------------------
⑿: g / female / .m $ Moves all the female employee records to the end of the file.
Fei.zhang 430759701022003 1970/10/22 Male
Yu.Guan 342869680413001 1968/04/13 Male
BEI.LIU 210324650708001 1965/07/08 Male ------------------------------------------------------------------------------------------------------------------------------------------------ -------
Chan.Diao 651302801225012 1980/12/25 female
Shi.xi 120638780214006 1978/02/14 Female
The author is currently developing software development for a foreign company (IBM S / 390), and all work is carried out in the TSO environment. In order to test the written program, you must prepare the test data. People who have experienced large machine development will know that through TSO, input characteristic data can also be, if you want to enter 16 credit data, it is troublesome. Because the number of 16-based number is vertical arrangement, it is inconvenient when the input is entered, and it is easier to misplace. How to solve it? I tried several ways, actually proved that the most convenient use of Vim. Example 6. The following data 1234567890Abcdef turns it into a form of 13579Ace 24680BDF, so that data can be very convenient to paste into the TSO environment. The macro command script is given below Change_D.vim
------------------------------------- ---
"Macro Function: Convert Char Arrange Direction
"
"Sample: 40 50 60 ==> 4 5 6
"0 0 0
Date: 2001/12/01
Author: yan shi
------------------------------------- ---
: s /.*//&3 m /
:1
: Map = Malx $ P-`al =
described as follows:
(1): s /.*//&3 M / Add a space below the data line.
(2): 1 Back to the first character of the document.
(3): MAP = Malx $ P-`= Targe a large string VIM command image to the character =
1 MA marks the first character as a
2 L cursor right shift a character
3 x Delete the cursor character
4 moved to the next line
5 $ to the end
6 p Paste the deleted character
7 - back to the top
8 `a returns to the tag character
⑨ L cursor right shift a character
⑩ = Recursive call, repeat the above steps until all of the data is processed.