VI editor usage (3)

Source: Internet
Author: User
ArticleDirectory
    • 2.1 redirection of the optical mark in the program
    • 2.2 keyword completion during the program editing process

Add two blog posts: Use of the VI Editor (1)

VI Editor (2)

2. VI Editor Program Edit

This section describes several operations that can improve the efficiency of the VI editor in program design, including jump of the cursor, completion of keywords, andSource codeIndentation.

2.1 jump of the cursor in the program 1. jump between brackets

The command % can be used to jump between the brackets and jump to a bracket that matches the brackets under the current cursor. If the cursor is on, it will jump to the ")" that matches it. If it is currently on ")", it will automatically jump forward to the "(" that matches it. See the following example:

 
Int main () {int X; For (x = 1; x <= 10; X ++) printf ("% d \ n", x); Return 0 ;}

Assume that the cursor is at the beginning of the row 2nd. After Entering command %, the cursor will jump to the beginning of the row 7th.

If the current cursor does not stay on any of the available parentheses, the command % also finds one forward for it, but only searches in the current row, therefore, if the current cursor is located on x <= 10 of the first line in the preceding example, the command % will still find the first "(" first.

2. Jump of local variables and function names

The command GD can search for the local variable name or function name in the current file, and position the cursor at the first position, as shown in figure 1-13.CodeAfter the GD command is run on the sum variable of the second row, the cursor is positioned on the sum of the second row.

 

This function is useful for searching for static variables or functions.

3. Search for a global identifier

When editing a program, you often want to know whether a variable is declared as Int or unsigned. The quick way to solve this problem is to use the command [I, the command will list all the lines containing this identifier, not only in the current file, but also find the header files contained in the current file, and the files contained in the header file, and so on. Take the following example:

 
/* File1.c */# include <stdio. h> # include "yanyb. H "int main () {int X; X = A; printf (" % d \ n ", x); Return 0 ;}

File1.c contains the header file yanyb. h.

 
/* Yanyb. H */int A = 1;

Run the [I command on a in file1.c. The following information is displayed at the bottom of the window.

 
Yanyb. H1: 1 int A = 1; file1.c2: 6 x =;

Each row in the list listed above has a label. to jump to a certain item, you only need to enter the corresponding label first:

3 [<tab>

2.2 keyword completion during the program editing process

Many program editors provide the keyword complementing function, and the VI editor is no exception. The corresponding commands are Ctrl + P and CTRL + N, the difference between the two commands lies in the search order.

When the printf function is input in the source program, only one part is input, as shown in figure 1-14.

 

Enter Ctrl + N, and the corresponding function options will appear on the screen. You can use the up and down arrow keys for selection, such:

 

In addition to the keyword completion, you can also complete the variable or function name defined above.

2.3 source code indentation

Indentation not only enhances the readability of the code, but also helps to eliminate errors. The vi Editor provides two functions: Automatic indentation and manual indentation. See the following.

1. Automatic indent

By default, the VI editor can automatically enable indentation. You can use the following command to set the indentation in command mode.

: Set cindent shiftwidth = 2

The indent is set to two spaces, that is, the output file format is as follows.

 
Int main () {int X; For (x = 1; x <= 10; X ++) printf ("% d \ n", x); Return 0 ;}

If it is set to 4, the output result is as follows:

Int main () {int X; For (x = 1; x <= 10; X ++) printf ("% d \ n", x); Return 0 ;}
2. Manual indent

If you want to take over some code with a very bad indentation format, or you want to add, delete, and fix code in the existing code, you may need to re-organize the code indentation. In this case, you can use the "=" command to implement it.

See the following example:

 
Int main () {int X; For (x = 1; x <= 10; X ++) printf ("% d \ n", x); Return 0 ;}

For the above code, you can execute the = operation in the row where the cursor is located to indent the line. You can also use the command = g to indent all the lines from the current row to the bottom of the file. The output result is as follows:

 
Int main () {int X; For (x = 1; x <= 10; X ++) printf ("% d \ n", x); Return 0 ;}

As you can see, the entire code segment achieves good indentation.

 

VI editor finished learning!

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.