Programming in C language: Under vi (editing source program)

Source: Internet
Author: User

 

Connect

Step 5: Learn multiple operation commands

 

To avoid repeated operations, vi has designed multiple operation commands: Operation count + operation command. This greatly improves the editing efficiency.

 

Example 1: delete 50 rows. We can perform dd 50 times in a row. However, we should use multiple operation commands and enter 50dd!

 

Example 2: To copy 50 rows below the current row, type:

 

1. 50

 

2. yy

 

3. move the cursor to 50th rows.

 

4. p

 

This command is very useful and tests the programmer's eyesight. For example, we often need to write a while, for, and other loop statements or a function. We often copy the previous while, for, and other loop statements or functions and make some modifications. The number of statements copied depends on the programmer's eyesight. For example, 35 copies are required. If the programmer thinks that this statement is 30, the programmer needs to copy 5 more statements. If the programmer has 40 statements, five will be deleted after the copy. If programmers can see 35 copies at a glance, it will be as happy as winning the prize. After all, accurate eyesight can reduce extra operations.

 

Example 3: Change hello myself to hi world.

 

1. move the cursor to hello

 

2, 2

 

3. cw

 

4. hi world

 

5. Esc

 

2 indicates that two words will be modified.

 

Multiple operations are repeated once. If you do not want to perform multiple operations, you can also perform one operation at a time. However, if you have performed more than two or more operations, select multiple operations, this greatly improves the editing efficiency.

 

Step 6: vi command line

 

Vi not only performs various operations on the editing object, but also on the command line of vi. This command line is generated by typing ":" in the last line of the file screen.

 

In our first step, the q command is a command of the command line.

 

Q !, X, x! All commands are command line commands.

 

The following describes a command to delete a useful line.

 

Assume that the file content is:

 

Hello world

 

Hello myself

 

Hello c

 

The current cursor is in the first line

 

 

1. Delete the second row to the third row as required.

 

Method 1: move the cursor to the second line and enter dddd. This is a method described above.

 

Method 2:

 

1 ,:

 

2, 2, 3d

 

In this way, enter the d command in the command line. Indicates that all rows from the second row to the third row must be deleted. This method is also called the absolute deletion method.

 

Method 3:

 

1. move the cursor to the second line.

 

2 ,:

 

3,., + 1d

 

This method is also a command line command. Deletes a row (+ 1) from the current row (.) to the end of the current row. This method is called the relative deletion method. Indicates that the row to be deleted is relative to the current row.

 

Method 4:

 

1. The current cursor is in the first line.

 

2 ,:

 

3, + 1, + 2d

 

In this method, all rows from the bottom of the first row to the second row under the first row are deleted.

 

Method 5:

 

1 ,:

 

2, 2, $ d

 

This method deletes the second row and the last row of the file. $ Indicates the last row of the file. This command is also very useful.

 

If we do not know which row is in the current line, we can type ctrl-g. At this time, the command line will prompt: the cursor is at line 1 of 3-33% --, you can see The number of rows in The current row and The number of rows in The entire file. 1 indicates the current row, and 3 indicates all rows.

 

Ctrl-g is also a common command.

 

 

In the long-term practice of vi, the relative method is used to delete statements within 20 rows, and the absolute method is used to delete a large number of statements.

 

 

2. replace all hello with hi

 

1 ,:

 

2, 1, $ s/hello/hi/g

 

I understand this command in this way: from the first line to the last line, I found that (s) hello uses hi to replace all (g. In this way, we will not forget it in the future.

 

 

Step 7: read and write files

 

The preceding operations are based on statement-level and word-level editing. It is very useful when we are new to vi. It is also useful and sufficient when we write a small number of C languages. However, when we write more and more C-language programs, we will see more and more others c-language programs, we will often copy other people's programs and their own programs, will copy a certain stage of the process, used in your own program. At this time, we will see a file-level program "Copy, paste" operation.

 

For example, we want to copy the open_deal function in a. c to B. c.

 

1, vi a. c

 

2,/open_deal

 

3, n

 

4, ctl-g

 

5. Write down the current row, assuming 243

 

6. move the cursor to the last line of open_deal.

 

7. ctl-g

 

8. Write down the current row, assuming 343

 

9 ,:

 

10,243,343 w open. c

 

11, q!

 

12, vi B .C

 

13. move the cursor to the line where open_deal is to be placed.

 

14 ,:

 

15, r open. c

 

16 ,:

 

17, x!

 

In this example, there are two key points. One is the 243,343 w open. c. operation instructions. Write the open_deal function program of the 243rd to 343 rows into the open. c file.

 

Another key point is r open. c. This operation description reads open. c under the current row.

 

We copied and pasted functions through w and r.

 

In many cases, we will add the entire file r and modify it.

 

In addition, in many cases, we will start to look for our past programs, find a function we want, and use some statements. However, we do not know in that file. At this time, we can open multiple files at the same time and check whether there is anything we want in one file. Once we find out, we will know that this function is in that file.

 

For example, assume that our directory contains six files: a1.c, a2.c, a3.c, b1.c, b2.c, and b3.c. We remember that there is a function named open_deal, and we want to transform this function, however, we do not know what file this function is in. In this case, we can perform this operation.

 

1, vi *. c

 

2,/open_deal

 

3,: n

 

4,/open_deal

 

Until it is found.

 

Here there are two key points. One is vi *. c, which indicates editing all. c files; the other is: n (next) to read the next file.

 

After finding open_deal, you can use the w command to write the function into a file for future r use.

 

 

To learn vi, you must take steps from simplicity to complexity, from step 1 to Step 7. Don't worry, just eat a fat man. The first step to the fourth step is the entry and foundation. It takes half a day to get started, the Operation exercise is estimated to take two to three days, and the next step is about three days. Someone is there. It is estimated that it is not a problem to master vi within one week. However, it is difficult to give time to be proficient in vi. I can say for sure that if you can master these seven steps, almost all the C language programming is fine (editing ).

 

 

Iv. Vi command Summary

 

I will summarize the commonly used commands of vi to facilitate your query. (I will not list other commands because they are rarely used. If you want to learn them, you can go to the Internet to query them .)

 

1. Access and exit vi

 

1) edit an object by using the vi file name.

 

2) vi *. c: edit multiple files.

 

3): q exits vi (save and exit, and the file has not been edited ).

 

4): q! Exit vi (save and exit, and the file has been edited ).

 

5): x exits vi (save and quit ).

 

6): x! Exit vi (save and exit, the file has been edited ).

 

 

2. Add, modify, delete, query, and cancel commands

 

1) I insert before the current character, and Esc exit the insert status

 

2) I insert at the beginning of the row, and Esc exits the insert status

 

3) a is inserted after the current character, and Esc exits the insert status.

 

4) When A is inserted at the end of A row, Esc exits the insert status.

 

5) o is inserted under the current row, and Esc is exited.

 

6) O is inserted on the current row, and Esc is exited.

 

7) r: modify the current character

 

8) cw modifies the current word, and uses spaces, (), [], and so on as the boundary. Esc exits the Insert state.

 

9) s. modify the current character and insert it. Esc exits the insert status.

 

10) x Delete the current character.

 

11) dd deletes the current row

 

12)/query, followed by the query content

 

13) n meets the query's next content

 

14) u abandoned the previous operation

 

3. copy and paste

 

1) copy the current row by yy

 

2) p paste the copied row to the current row

 

4. move the cursor, number of rows, and number of operations

 

1) press the cursor in the direction indicated by the cursor key to move up, down, left, and right.

 

2): 1 move the cursor to the first line of the file

 

3) G move the cursor to the end of the file

 

4). indicates the current row

 

5) + n is a numerical value, indicating the nn row after the current row

 

6) press ctrl-g to check the row number, file name, and total number of lines in the current file.

 

7) The n operator performs n operations. For example, 5yy repeats yy five times. 5x is equal to xxxxx.

 

 

 

5. Replacement

 

1) n1, n2s/string 1/string 2/g

 

Replace string 1 from line n1 to line n2 with string 2.

 

6. write and read files

 

1) The n1, n2 w file name writes the n1 row to the n2 row into the file with the file name.

 

2) r file name: load the file with the file name to the current row.

 

 

5. Mastery of editing tools

 

Many programmers are satisfied with the meeting, not the pursuit of precision; so are programming and editing. Such programmers can really switch their careers. Programmers belong to a higher, faster, and more refined profession. If they do not enter, they will leave. Otherwise, the title of programmer will be wasted. This is also true for vi. programmers should never satisfy me with this command. I know this command, because it is only a means to use it and use it well, making Program compilation faster is the most important thing.

 

Is there a standard for the degree of mastery of an editing tool? Of course! That is, the accuracy of the editing command, that is, when to use the command. This is used as a quantifiable standard, that is, in the same final editing content, we use a minimum number of keys. This requires us to minimize the number of modifications and deletions, and write the program at a time (almost impossible at a time, but the fewer the times, the better, which forces us to consider perfection .), When modification is required, use the correct command to make the modification, so that the number of keys is the least.

 

For example, a row contains 50 characters, the current cursor is at the beginning of the word, and now hello is to be added at the end of the word. One way is to press the cursor until 50th characters, type I, type hello, and type Esc. In this method, 50 + 1 + 5 + 1 = 57 keys are required. The second method is to type A, hello, and Esc. In this method, you must press 1 + 5 + 1 = 7 Keys. Which method is better? Of course the second method is good! Because the command is designed to add at the end of the line, you choose A to select the most accurate edit command.

Www.2cto.com

For example, if the cursor is at the beginning of a row and this is a new world, change it to this is a cat. Method 1: delete a single word insert: move the cursor to new, Type xxx, Type x, type xxxxx, type I, type cat, and type Esc. In this method, a total of 11 + 3 + 1 + 5 + 1 + 3 + 1 = 25 keys are required. Method 2: move the cursor to new, type 2cw, type cat, and type Esc. In this method, a total of 11 + 3 + 3 + 1 = 18 keys are required. Method 3: delete the insert method, move the cursor to new, type D, type A, type cat, and type Esc. In this method, a total of 11 + 1 + 1 + 3 + 1 = 16 keys are required. Of course there are other methods. Here we will not illustrate them one by one. In terms of these three methods, of course, the third method is the best. It accurately uses the tail deletion command, and the total number of clicks is the least. The second method is far better than the first method. It successfully uses the combination of phrase modification and frequency to make the modification faster and more reasonable than the first method.

 

We can see that the degree of mastery of the editing command can be obtained by the number of clicks. The number of clicks is small, which indicates that the use of editing commands is reasonable, the operation of editing commands is clever, and the programming time is short, saving programming time for other work such as design and debugging. On the contrary, it means that the editing command cannot be correctly used. The programming time is long due to a large number of clicks, which reduces the design time and debugging time.

 

In addition to mastering the editing command correctly, the typing speed must be improved. Both English and Chinese characters must be played blindly. In our time, typing was not paid much attention. Some programmers turned out to be single-handed and single-finger typing ". This basic skill is absolutely not allowed now. The tolerable typing speed is equivalent to the speed of speech, or lower. However, the speed of speech cannot be interrupted.

 

Many programmers, if you stand by his side and watch his program editing, the typing hand stops and does not know what he is thinking. Is it thinking about program editing? What is the program syntax? Are you considering how to edit it? A very simple program will become very complicated and complicated when it comes to them. It seems that you are not in a rush. This is called a program, and it will take the lives of others, it's killing me!

 

A good programmer not only needs to program well, but also needs to be excellent in some basic skills, such as typing and editing. It is hard to imagine that a good programmer, with a finger, keeps thinking, constantly modifying, and makes up an excellent program. Editing should be like a cloud flow, without a pause (the pause does not take more than 2 seconds). The program will come out in one breath, and this is what we pursue.

 

 

 

In short, vi is inseparable from the C language, especially on unix operating systems. Cutting firewood requires a quick knife, and vi is the cutting C language. Every time you raise a knife to cut down the hard nut of C language, you can't say anything in your mind. When yy falls, when dd FALLS, You have to lament the magic of vi designers. He copied the past and deleted the present, and everything went back to the origin.

 

 

Postscript:

 

I can't help but look at myself. I wrote this article intermittently for nearly ten days. I only wanted to write about it. I just wrote a few command examples. The more I want to write, the more detailed I want to write everything about vi. After all, I have been writing programs for decades, but I have never read a vi book after using vi for decades. All commands are learned from my colleagues. I am a grassroots vi. Sometimes, when I was in vi, I was always thinking about why the vi designer designed this command at the time. These simple and complex, memorable and ingenious designs, ever-changing vi, I was even more excited than the C language. I have been wondering if I can compile a program one day, as the vi designer did, to make my users feel excited and excited? I did not expect that I could write these commands systematically to write my blog. I will tell some key points, key points, and experiences of vi to new users who want to learn vi and hope to give them some reference. At the same time, let's make a summary of this learning and operating vi. It's also a Report to the vi designer and a respect.

 


Author n216

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.