Programming in C language: vi (editing source program)

Source: Internet
Author: User
Tags line editor

 

I. Review and comment of the previous Article

 

1. In the previous article, the hello world Program was analyzed in depth.

 

2. What is a complete program? What are the components? Please review it!

 

3. I talked about main. This is the most intense topic. Why am I not writing int main (void )?

 

4. I also talked about the syntax, function calling,. h references, keywords, escape characters, and streams. Remember?

 

5. Do you know the difference and use of exit and return?

 

6. Think questions are arranged: three hello world! How did you complete it?

 

1) write three rows of fprintf consecutively

 

2) use a loop statement to loop through fprintf three times

 

3) hello world in the original fprintf! Add hello world! \ N hello world! \ N

 

4) Other methods.

 

Which method do you use ?.

 

The purpose of this thinking is to tell you that there are many answers to a question, but different answers must have advantages and disadvantages. We should not only give the answer, but also give the best answer, so that we can improve our programming level.

 

7. The most important thing in the previous article is to propose a quantitative standard for comparing program quality, that is, the length of the program. Without quantitative standards, Any programmer will say that he is the best (many people are used to this way of thinking and use everyone's methods to protect their weaknesses, but they do not know, this kind of protection is the biggest obstacle to your progress.) If such a program doesn't matter, but in fact, the program gap is very large. Other aspects should be built on this basis.

 

Ii. Role of editing in C Language

 

1. Concepts and content of Editing

 

We keep talking about the programming process: editing, compiling, and running. Editing is the first step in programming. What is editing? Which files are edited? Generally, editing mainly involves editing tools and content. Editing tools are software used to edit files. The editing content mainly creates various source programs, batch processing programs, and parameter files in the project. Remember: editing refers to 1. editing various source programs 2. batch processing programs 3. Parameter files. Remember this, we can have a conceptual understanding. With the knowledge of this concept, we will be able to consciously know what we want to grasp and prepare for it in the future.

 

1) source program

 

It refers to a statement that contains a program language. It can be compiled into executable files and intermediate files. For example, in c, *. c, *. h file.

 

2) Batch File Processing

 

A text file that can be executed row by the operating system. This is widely used in C language, such as batch compiling command files and shell commands.

 

3) parameter file

 

Data files created in a program to flexibly adapt to specific changes. Parameter files are not executable, but can be called by programs.

 

 

 

2. editing tools

 

Theoretically, any text editing software can be used as a program editing tool. What is the purpose of remembering this sentence? When you want to modify a source program, you don't have to install the development environment of this language. You just need to find an editing software to edit it. Because the source program is a text file, you only need to be able to open this file and edit it.

 

Recall to the novice the previous C language editing software: At the beginning, the editing software and programming language were separated. At that time, the dossystem used EDLINE and unix used vi. Later, DOS used EDIT, followed by notepad and word, and then directly used the text editor in the language.

 

 

 

3. editing functions

 

1. Generate source program

 

If we can't even edit the program, we won't be able to generate the source program, so we won't be able to become programmers. The first responsibility of a programmer may be to write a program. It can be seen how important editing tools are.

 

2. Edit the program

 

Programs are not always okay when they are generated. In most cases, programmers must constantly modify and add their own programs. Editing is a favorable tool for implementing such changes, if no editing tool is available and a program error is found, it may not be modified.

 

3. debug the program

 

When debugging the program, we will check how the source program is written. Dbx provides an e command in the C language debugging program. You can use vi to open the source program to help the debugger find the cause of the error.

 

4. Edit the parameter files and batch command files used by the program

 

In reality, there are very few applications with no parameters in C language. The C language needs to be input before processing and output. Therefore, programmers must not only compile the program, but also edit the parameter file so that the program can run smoothly.

 

In addition, the solution to a problem is rarely flat by a program. Therefore, running different programs logically makes the program combination more flexible even if the program is more modular, it also allows the program to keep moving forward like a solitaire. Therefore, editing a batch command file is also an important function of editing a program.

 

3. How to Learn vi

 

Now, the C language can have many editing programs: line editing, screen editing, unix editing, window editing, and powerful editing programs, there are also simple editing programs, editing programs in development languages, and independent editing programs. In my experience, if you are specialized in developing background programs in unix environments and using C language, vi must be the preferred editing program.

 

1. First, it is not difficult to learn vi. Never be afraid of it. Anyone can learn it. It takes a short time and some people can guide you. Generally, one day is enough to get started.

 

2. It is best to find a person who understands vi as a consulting object. Don't hold on to books, and then you can start.

 

3. Pay attention to the learning method. Do not ask why first. Do what you need first, learn to imitate, and then think about it yourself.

 

4. Before you practice, remember 2 points: First, vi is not a screen editor, but a line editor, edited in the unit of action. You can only press the mouse. Second, vi is case sensitive. Therefore, pay special attention to the case sensitivity of various commands in vi.

 

Next I will try to teach you how to learn vi in practical ways.

 

 

 

Step 1: Learn to enter vi and exit vi

 

If you learn vi for the first time, you can start by running vi.

 

Assume that the edited file name is. c, in the command line status of the operating system, type: vi. c (enter a carriage return when the command line ends. The prompt is not displayed here. in addition, note that the UNIX operating system is case-sensitive. Do not write vi as an upper-case VI)

 

Then you enter vi. at this time, you will see. c [New file]. This is what vi tells you. You are editing. c,. c is a new file.

 

After entering, do not rush to edit the text. Enter: q and press Enter. Then you exit from vi to the command line status of the operating system. Q should be short for quit.

 

After entering and exiting the operation several times, you have learned how to use the simplest vi. In this case, you need to restrain the impulse to edit and master the entry and exit. When learning, we often think too much forward, but it is not necessary. Generally, we only need to study step by step. Don't think about it. Step 1: Let's proceed.

 

Step 2: Learn to add strings.

 

This step is to learn how to add a string in vi or input a string. Suppose we want to input "hellow world!" in a. c !" This string. Enter the following characters in the order below. Do not enter the wrong characters.

 

1) vi a. c

 

2) I

 

3) hellow world!

 

4) Esc

 

5 ):

 

6) x

 

If you enter it successfully, a. c will have "hellow world !"

 

You can enter cat a. c In the command line to see: hellow world !, Congratulations, you have entered the string.

 

In this case, remember: I is the Add command, indicating that all subsequent characters will be entered, unless the increase ends when the Esc occurs. Therefore, when you want to input a string, the first one must think of the I command, which I remember as follows: I is insert.

 

You can try to edit a B. c with the content hello myself! Can you check it?

 

 

 

Step 3: Learn how to modify and delete data.

 

It is impossible for us to add a program at a time when editing the program. Always modify, delete, and Add. In the following example, we want to set hello world! In A.C! Change to hello myself.

 

Method 1:

 

1. vi a. c

 

2. dd

 

3. I

 

4. hello myself.

 

5. Esc

 

6 ,:

 

7. x

 

This method is similar to the dd command to delete the original line, and then input hello myself through the I command. . This method focuses on the dd command. Remember to delete a row using dd (delete, delete, I remember it like this ).

 

Method 2:

 

1. vi a. c

 

2. move the cursor to world

 

3. xxxxxx

 

4. I

 

5. myself.

 

6. Esc

 

7 ,:

 

8. x

 

This method focuses on the x command to delete the current character, which is also one of the most commonly used commands.

 

Method 3:

 

1. vi a. c

 

2. move the cursor to world

 

3. xxxxx

 

4. I

 

5. myself

 

6. Esc

 

7. move the cursor!

 

8. r

 

9 ,.

 

10 ,:

 

11. x

 

This method focuses on the r command (replace) to replace the current character, which is also one of the most commonly used commands.

 

Method 4:

 

1. vi a. c

 

2. move the cursor to world

 

3. cw

 

4. myself.

 

6. Esc

 

10 ,:

 

11. x

 

This method focuses on the cw command (change word) to replace the current word. Note that it also requires Esc to end the modification state, which is also one of the most common commands.

 

As you can see, all the above methods use: x to save the edited content. : X is also one of the most common commands.

 

If you find that this editing is wrong, you are in a mess and want to stop editing the file and do not want to modify the original program. Q! can be used! Command to discard modification and exit vi .! Can be remembered as forced, q! It can be understood as forced exit. Likewise, x! It is understood as force save.

 

Through the above three steps of learning and practice, we can basically access the basic operation process and operation commands of vi. These basic operations can be said to be capable of programming.

 

You can try to use the above method to edit a. c In my previous blogs. The file name can be changed to helloworld. c.

 

# Include "stdio. h"

 

Main ()

 

{

 

Fprintf (stdout, "hello world! \ N ");

 

Exit (0 );

 

}

 

Let's take a look at the vi operation to see how much we can master the above basic commands. Practice the basic things over and over again and remember them carefully so that you can better understand and cooperate with the subsequent operation commands. After learning the above operations, you can say that you are already getting started with vi.

 

Step 4: Learn to copy, query, and cancel

 

When editing a program, we generally add, delete, and modify operations. At this time, our field of view is limited to single character, word, and single line. In many cases, you need to perform operations. For example, if you want to add a new row, you need to copy the row before modifying it.

 

Let's take the previous example. Now the content of A.C is hello myself.

 

The final content is required as follows:

 

Hello myself

 

Hello world

 

Hello c

 

The first method is to copy and then modify it.

 

1. vi a. c

 

2. yy

 

3. p

 

4. p

 

5. Move a row up

 

6. Move to myself (second line)

 

7. cw

 

8. world

 

9. Esc

 

10. Move to the next row

 

11. Move to myself (Row 3)

 

12. cw

 

13. c

 

14. Esc

 

15. x

 

This method focuses on copying and pasting. Yy is to copy the current row, p is to paste the copied row. This is one of the most typical command designs in my opinion. It may be the originator of Ctrl-c and Ctrl-v.

 

Method 2: Direct insertion

 

1. vi a. c

 

2. o

 

3. hello word

 

4. hello c

 

5. Esc

 

6. x

 

This method focuses on inserting data under the current row. O is the INSERT command, indicating that the current row is inserted, and the Esc insertion ends. If you want to insert an uppercase O value for the row above the current row, exit the insert operation.

 

Method 3: Copy query modification method.

 

1. vi a. c

 

2. yy

 

3. p

 

4. p

 

5.: 1

 

6 ,/

 

7. myself

 

8. n

 

9. cw

 

10. world

 

11. Esc

 

12. n

 

13. cw

 

14. c

 

15. Esc

 

16. x

 

The focus of this method is to copy two lines of hello myself, then move the cursor to the file header Through the: 1 command (this command is very useful), and then use the/command to find myself, use the n (next) command to move to the second myself word, use cw to modify, and then use the n command to move to the third myself and use cw to modify. /The command is the SEARCH Command, and n is the next one. Currently, the same query command is also available in some development languages, and vi is also used for reference.

 

During operations, we often encounter operation errors. At this time, vi provides an undo command to cancel the previous operation.

 

For example, when you copy two rows and find that three rows are copied, if you type u

 

1. vi a. c

 

2. yy

 

3. p

 

4. p

 

5. p

 

6. u

 

At this time, your last paste will be canceled.

 

This example focuses on canceling the u operation. This command can be used for many operations, for example, i, I, O, o, A, x, r, dd, cw, s, etc. After these operations, type u and the previous operation will be canceled.

 

In addition, we will introduce a G command that allows the cursor to locate the last line of the file.

 

For example, the file content is:

 

Hello world

 

Hello myself

 

Hello c

 

The current cursor is in the first line

 

Type G, and the cursor is positioned on the last line of hello c. This command is particularly useful when creating a program. For example, if you compile a program today and then compile it tomorrow, you can run the G command to the last line. Then, run the o command to continue the insertion of your new program.

 

To be continued!


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.