Use the VI Editor

Source: Internet
Author: User

VI is one of the most widely used text editors in Unix-like operating systems. This tool is provided by default for both UNIX, Solaris, and Linux. As long as you are familiar with Vi, you can freely edit text on any UNIX operating system platform. There are many materials on the basic usage of vi. The purpose of this article is not to repeat these basic usage, but to provide some practical skills for people who have a preliminary understanding of the basic usage of VI, this allows you to use VI to edit files more quickly and efficiently.

VI can be divided into command mode and insert mode. VI is in command mode after it is started. In command mode, you can move the cursor, copy and paste text at will. Commands such as "a", "I", "C", "C", "O", and "O" can be used to switch from the command mode to the insert mode. In insert mode, you can enter text and Press ESC to switch back to command mode. In order to differentiate commands in different modes, this article will introduce the modes of operations.

In addition, all operations in this article are performed in VIM in Red Hat 9.0. Most of the techniques described in this article are also feasible in various VI versions.

Encryption and decryption

Encrypt and decrypt files

In command mode, enter the ": X" command, enter the password, confirm the password, and enter the ": WQ" command to save and exit. The file is encrypted.

The encrypted file is converted into a ciphertext file. To read the original plain text, you are prompted to enter the password after opening the file.

If the password is entered correctly, the original plaintext is displayed. In addition, you can see that the file is encrypted on the last status line.

Cancel Encryption

It is easy to cancel encryption. In command mode, enter the ": X" command. First, the system prompts you to enter the password. Then, press Enter. Next, when you are prompted to repeat the password, press the Enter key and then enter the ": WQ" command to save and exit. In this way, the file encryption is canceled.

Note that the preceding encryption method runs in VIM on Red Hat Linux 9.0. It may be slightly different from other platforms. Refer to the help of the actual platform or version.

Replacement Operation

Some commonly used simple replacement Commands include:

◆ S: Replace the current character with one or more characters. For example, "5" means to replace the five characters starting from the current character with one or more characters.

Character;

◆ S: Replace the current row with one or more characters;

◆ Replace a single character in R.

Text Format Conversion

Convert the DOS text to UNIX text. The command is as follows:

: 1, $ S/^ m/g

^ M is simultaneously pressed by Ctrl + V + M, indicating the carriage return.

Add a string at the beginning and end of a row

Use the following command to add the string "newstring" to the beginning of each line of text. Note the existence of spaces.

: G/^/S // newstring/G: % S/^/newstring/g

Use the following command to add the string "newstring" at the end of each line ":

: G/$/S // newstring/G: % S/$/newstring/g

Insert the string "newstring" from the first row of the text to the first row of the last line ":

: 3, $ S/^/newstring/

Replace

Replace a specific string in the entire file. The command is as follows:

: % S/old_string/new_string/g

If you only Replace "old_string" in rows 3rd to 7th of the file with "new_string", use the following command:

: 3, 7 S/old_string/new_string/

Note: "S" indicates the replacement action, "%" indicates all rows, "G" indicates the full text, "^" indicates the first row, and "$" indicates the end of the row.

.

Assisted Programming

Highlight the syntax

Write programs in VI. If you want to enable the syntax brightening function for convenience, simply enter ": syntax on" in command mode. Similarly, if you want to disable the syntax brightening function, enter ": syntax off" in command mode ".

Quick View of function help

When you use VI to read and write a program (mainly a C or C ++ Program), if you need to know the specific functions of a function, you can move the cursor to the function, then input "K" in command mode to mobilize man help for this function in "Linux programmer's Manual.

Mark and locate

When reading and writing large program files, it is very helpful to use the tag (bookmarks) function to locate them. We recommend that you use it more frequently. The following is a simple example.

First open a source code file and move the cursor to the position you want to mark. If you make a tag named "debug1", you can enter the tag command "mdebug1" in command mode, and then press the Enter key, A tag named "debug1" is ready.

Next, you can move the cursor to another position at will. When you enter "'debug1" in command mode, you can quickly return to the first line of the line marked by "debug1.

"Ctags" command

When reading and writing C or C ++ programs, run the "ctags" command to scan the C source program in the current directory. The command is as follows:

$ Ctags *. c

The above command will be in the current directory. the C file generates the tags file, and then uses the "ta function name" command to quickly open the file where the corresponding function is located, and positions the cursor at the beginning of the corresponding function.

Automatic Alignment

In command mode, the ": Set ai" command can be used to automatically align all rows in the program.

Show matching

In programming, {}, [], and () are usually paired, and there are often multiple layers of nesting, which is easy to miss and make mistakes. So the following tips are very useful. In command mode, the ": Set SM" command will display the matched {, [, () when you press),], and.

Abbreviated text

In programming, you often need to enter repeated and long information, such as long nouns and company information. In this case, you can use the VI abbreviation function. It can replace a long word with a short abbreviation. For example, a long URL can give it an abbreviation, in command mode using ": AB myurl http://www.mykms.org/kms/myproject/index.php? Id = 0023 ", and then input" myurl "in insert mode," myurl "will automatically become the long URL you just defined. In insert mode, enter ": una myurl" to cancel the abbreviation of "myurl.

Set options

You can use ": Set" to set environment variables of VI. There are many VI environment variables. Use ": Set all" to display all environment variables.

You can use ": Set variable name and parameter" to set the corresponding environment variable.

The following command displays the row number:

: Set number

The following command can be used to set the file to read-only:

: Set readonly

The following command causes VI to be in ": N" and ":!" The file is automatically saved before the command:

: Set autowrite

The following command shows the mode in which the user is located:

: Set showmode

Similarly, ": Set noshowmode" is to disable this display.

There are many other VI environment variables. Understanding them will make it easier for users to use VI. You can use the online help of VI to find the detailed functions of corresponding environment variables.

Use macros

VI macros provide more powerful functions. Users can customize their own macros based on their own needs, and then complete a set of operations defined in advance through macros. Use "map" to define a macro and use "unmap" to delete it. After you are familiar with the basic commands of Vi, you can combine these commands to complete complicated work. If you define the combination of these commands as a macro, you can call them easily. This is a very powerful function, and we recommend that you use it more.

Summary

VI is a common default editor for Unix-like operating systems. After learning about Vi, you can use it to efficiently edit files. However, the process of learning VI is very long because it has many functions. However, it is indeed a tool worth the time to learn. We hope everyone can learn VI well and make full use of this powerful tool for text editing.

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.