Vi and vim Editor (12): advanced editing method (3)

Source: Internet
Author: User

Vi and vim Editor (12): advanced editing method (3)

@ Function:

The naming buffer provides another method to create a macro, that is, you can save a complex command sequence to a naming buffer, and then run the content in the buffer using the @ command. For example, enter the following line in the insert mode:

Delete the row and save it to the "a" Naming buffer. Then, you can move the cursor to the header of a word and execute "@ ", you can execute the content in the buffer zone as a command. In this example, the word is changed to function and is still in command mode. Because the @ command is interpreted as a vi command, you can use the dot (.) to repeat the command no matter what command is stored in the buffer zone. "@" Repeats the previous @ operation.

Use the ex script:

You can store ex commands in the ". exrc" file so that these ex commands are automatically executed when you open the vi editor. Alternatively, you can store the ex command in other files, and then read the script file through ": so scriptname" when opening the vi editor to execute the script. For example, the "substitute. ex" script contains the following content:

After opening a file in the vi Editor, run the following command to execute the ex script:

You can also directly apply the script to a file in the UNIX command line. Here, you need to use the UNIX redirection function: "ex-filename <scriptname", so that you can apply the scriptname script to the filename file. For example:

Of course, we can also write a shell script that contains the call to the ex script, which allows us to make more flexible modifications to the file. For example, call the "substitute. ex" script in a shell script:

Here document:

In shell script, the operator <indicates that the following line ends with a string as the command input, which is also called here document. As shown in, using this syntax, you can directly write the ex command in a shell script:

The "end_of_script" string here can be any string, as long as it does not appear elsewhere in the input text, so as to identify the end position of the here document. It should be noted that, compared with the here document, it is more flexible to write the editing command in a file other than the shell script.

Note in the ex script:In the ex script, the text after any double quotation marks is ignored during execution. Therefore, the double quotation marks can start with the comment.

In addition to the ex Editor, UNIX provides more powerful editors than ex: sed stream editor and awk data manipulation language, which provide more powerful editing functions. Of course, this is the future direction of learning. At present, I have learned the vi editor in a down-to-earth manner.

Edit the program source code:

The vi Editor provides many powerful functions for programmers, including contraction control, the start and end of the search process, and the use of ctags.

Contraction control:

If you want to use automatic contraction in vi, you need to set the following options: ": set autoindent ". In this way, when the space or tab key is used for contraction, the subsequent lines will be automatically scaled at the same distance. When you enter the code in the case of automatic contraction, pressing "CTRL + T" at the beginning of a row will make the current row enter the next level of contraction level, press "CTRL + D" to return to the upper layer of the contraction level. Note that "CTRL + T" and "CTRL + D" are used in the insert mode.

The "<" and ">" command can also be used to scale down the code. ">>" By default, the Code moves eight spaces to the right, and "<" moves eight spaces to the left by default. The number of spaces to be moved by default can be changed by using the shiftwidth option. For example, you can use ": set shitwidth = 4" to set the number of spaces to 4. You can also move multiple rows at the same time. You only need to add a value before "<" or ">.

When we scale down the code, because of the tab key, the Space key is displayed blank on the screen, and it is not easy to see whether there is a space at the end of the trip, in this case, you can use the list option to change the display status. This option has been discussed in the previous article. You can also use the ": l" command to achieve the same effect.

A special search command:

"(, [, {, <" Can be called parentheses. When the cursor is placed on any brackets, press the "%" key, you can move the cursor to a pair of closed brackets. Similarly, when the cursor is placed on a closed bracket, press the "%" key to return to the corresponding opening bracket. When the cursor is not placed on the bracket character, after pressing the "%" Key, vi will find the first open or closed bracket in the current line, move the cursor to the brackets corresponding to the brackets.

Use tags ):

The ctags command on UNIX can generate an information file. The vi editor can use this file to determine which functions are defined in each source file. By default, the file name is tags. Combine the "ctags" command in UNIX with the ": tag" command in vi to quickly move the cursor to a tag. First, use the "ctags" command on the UNIX Command Line to create the corresponding tags file for the program source file, and then use the ": tag name" command in the vi editor to move the cursor to the row where the name function is located.

In command mode, you can move the cursor to the identifier to be searched, and then enter "CTRL + [" to move the cursor to the row where the identifier is located. Note that vi looks for words starting from the current cursor position, rather than the complete word containing the cursor. If the tag is located in a new file but the current file is not saved, vi will not allow you to read the new file. You must use the ": w" command to save the current file, or use the ": tag! Name to force vi to discard the editing result.

We will continue to learn about the ctags command in subsequent articles. Here we will only learn the simple usage of ctags.

Summary of the vi commands in this chapter:

"@", "@", "CTRL + T", "CTRL + D", "<", ">", ": l", "% ", ": tag name", "CTRL + [", ": tag! Name ".

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.