Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the vi Editor

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use the vi editor", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the vi editor.

Vi can be divided into command mode and insert mode. Vi is in command mode after it starts. In command mode, you can move the cursor, copy and paste text, and so on. You can switch from command mode to insert mode using commands such as "a", "I", "c", "C", "O", and "o". In insert mode, you can enter text and press Esc to switch back to command mode. In order to distinguish the commands in different modes, this article will indicate the operation in which mode.

In addition, all of the operations in this article are done in vim that comes with 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

Enter the ": X" command in command mode, then enter the password, confirm to enter the password, and finally enter the ": wq" command to save and exit. The file has been encrypted.

The file encrypted by the above method becomes a ciphertext file. To read the original plaintext, the user will be prompted for a password after opening the file. If the password is entered correctly, you can see the original plaintext. You can also see that on the final status line, there is a prompt that this file has been encrypted.

Cancel encryption

The way to unencrypt is simple. When you enter the ": X" command in command mode, you will first be prompted for a password, and then directly hit the enter key. Next, when prompted to type the password repeatedly, enter the enter key directly, and finally enter the ": wq" command to save and exit. In this way, the encryption of the file is cancelled.

Note that the above encryption method is running in vim on Red Hat Linux 9.0. There may be slight differences on other platforms, please refer to the actual platform or version of the help.

Replace operation

Some simple replacement commands that are often used are:

◆ s replaces the current character with one or more characters, for example, 5 s means to replace 5 characters starting with the current character with one or more characters

◆ S replaces the current line with one or more characters

◆ r single character substitution.

Text format conversion

To convert DOS format text to Unix format text, the command is as follows:

: 1 recordings / ^ M / g

Where ^ M is pressed by Ctrl+V+M at the same time, which means enter.

Add a string to the beginning and end of a line

Use the following command to add the string "NewString" at the beginning of each line of text, noticing the presence of spaces.

: G / ^ / s//NewString/g:% s / ^ / NewString/g

Add the string "NewString" at the end of each line with the following command:

: g/$/s//NewString/g:% s/$/NewString/g

Insert the string "NewString" from line 3 to the beginning of the last line of the text:

: 3. NewString/ / ^ /

Replace in the entire file

Replace a specific string throughout the file with the following command:

:% s/old_string/new_string/g

If you only replace "old_string" with "new_string" in lines 3 through 7 of the file, use the following command:

: 3,7s/old_string/new_string/

Note that "s" represents the replacement action, "%" represents all lines, "g" represents the full text, "^" indicates the beginning of the line, and "$" represents the end of the line.

Assisted programming

Syntax highlighting

To write a program in vi, if you want to turn on syntax highlighting conveniently, simply type ": syntax on" in command mode. Similarly, if you want to turn off syntax highlighting, type ": syntax off" in command mode.

Quick View function help

When reading and writing programs with vi (mainly refers to C or C++ programs), if you need to know the specific functions of a function, you can move the cursor over the function, and then type "K" in command mode to transfer the man help for this function in "Linux Programmer's Manual".

Mark positioning

When reading and writing large program files, it is very helpful to use the tag (bookmark) function to locate. You are advised to use it more often. Here is a simple example to illustrate.

First open a source code file and move the cursor to the location where you want to mark it. If you make a tag called "debug1", then the user can type the marked command "mdebug1" in command mode, and then type enter, and a tag called "debug1" is done.

Then the user can move the cursor to another location at will, and when you type "`cursor" in command mode, you can quickly return to the beginning of the line where the mark of "debug1" is located.

"ctags" command

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

$ctags * .c

The above command generates a tags file for the .c file in the current directory, then uses the ta function name command to quickly open the file where the function is located and position the cursor at the beginning of the function.

Automatic alignment

Use the ": set ai" command in command mode to automatically align the lines in the program.

Show match

In programming, {}, [], () often appear in pairs, and often have multiple layers of nesting, it is easy to miss and make mistakes. So the following technique is very useful. Using the ": set sm" command in command mode displays matching {, [, (to) when the user taps),],}.

Abbreviated text

In programming, you often have to enter repetitive and long information, such as long nouns and company information, and you can use the abbreviation function of vi. It can replace a long word with a short acronym. For a longer URL, for example, you can give it an abbreviation, use ": ab myurl http://www.mykms.org/kms/myproject/index.php?id=0023"" in command mode, and then type "myurl" in insert mode, and "myurl" will automatically become the long URL you just defined. In addition, typing ": una myurl" in insert mode cancels the abbreviation "myurl".

Set option

Use ": set" to set the environment variable for vi. There are many environment variables for vi. Use ": set all" to display all environment variables. Use ": set variable name and parameters" to set the corresponding environment variable.

The following command displays the line number:

: set number

The following command makes the file read-only:

: set readonly

The following command makes vi in ": n" and ":!" Automatically save the file before the command:

: set autowrite

The following command shows what mode the user is in:

: set showmode

By the same token, ": set noshowmode" turns off this display.

There are many more vi environment variables. Understanding them will make it more convenient for users to use vi. Users can find out the detailed role of the corresponding environment variables through the online help of vi.

Use macros

Vi macros provide more powerful features, users can customize their own macros according to their needs, and then use macros to complete a set of pre-defined operations. Define a macro with "map" and delete it with "unmap". Once you are familiar with the basic commands of vi, you can combine these commands to accomplish complex tasks. If you define the combination of these commands into macros, it will be convenient for users to call them frequently. This is a very powerful function, it is recommended that you use it a lot.

At this point, I believe you have a deeper understanding of "how to use the vi editor". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report