(Worth collecting!) ) Vi Text editor explaining

Source: Internet
Author: User

Brief introduction

VI Editor is usually referred to as VI, VI Editor is the most basic text editor on Linux and Unix, working in character mode. It can do the output, delete, find, replace, block operations and many other text operations, and users can customize according to their own needs, because no need for a graphical interface, VI is a highly efficient text editor. Although there are many graphical interface editors available on Linux, the functions of VI in System and server management are unmatched by the graphical editors.

Role

Create or modify a text file
Maintain various configuration files in Linux systems

Vi. relationship with Vim

Vi: Default text Editor in Unix-like systems
The enhanced version of the Vim:ⅵ editor, also known as the VI

I. Working mode of the Ⅵ editor

In the Ⅵ editing interface, you can use three different modes of operation, namely command mode, input mode and last line mode, which can be performed differently in different modes.

Command mode: The command mode is entered by default when the Ⅵ editor is started. In this mode, the main completion such as cursor movement, string lookup, as well as delete, copy, paste the contents of the file and other related actions
Input mode: The main operation of this mode is to enter the contents of the file, you can modify the text file body or add new content. When in input mode, the last line of the Ⅵ editor will appear with the "-insert-" status prompt.
Last-line mode: This mode can set the Ⅵ editing environment, save the file, exit the editor, as well as the contents of the file to find, replace and other operations. When in last row mode, the last line of the Ⅵ editor appears with the colon ":" Prompt.

Command mode, input mode and last line mode are the three states of the Ⅵ editing environment, which can be switched between different modes by different key operation. For example, you can enter the last line mode by pressing the colon "" key from the command mode, and if you press A, Ⅰ, O and other keys to enter the input mode, you can press ESC to return to command mode in the input mode and the last line mode.

VI Working mode diagram:

II. basic operations in the command mode
温馨提示:在学习ⅵ编辑器的基本操作时,建议复制一个内容较多的系统配置文件进行练习,而不要直接去修改系统文件,以免发生失误造成系统故障。例如,以下操作把系统配置文件/ etc/inittab复制为当前目录下的 vitest.file文件,然后用ⅵ编辑器打开vitest.file文件进行编辑。

[Root (@host ~]# cp/etc/inittab/vitestfile #做备份
[Email protected]~] # vi vitest file

Precautions:

In the Ⅵ Editor's command mode, you can enter specific keystrokes (called Ⅵ operation commands, which are distinguished from Linuⅸ system commands). Mainly including mode switch, cursor movement, copy, delete, paste, file content search and save and exit, and so on, here only the most basic, most commonly used key commands.

1. Mode switch

In command mode, use a, I, O and other keys to quickly switch to the input mode, while determining the way and location of the insertion point, in order to input the contents of the file. When you need to return to command mode, press the ESC key.
Several common mode toggle keys and their functions are as follows:

A: Inserts the content after the current cursor position.
A: Inserts the content at the end of the line where the cursor is located (end of line).
I: Insert content before the current cursor position.
L: Inserts content at the beginning of the line where the cursor is located (the beginning of the row).
o: Insert a new line after the cursor line.
O: Insert a new line in front of the line where the cursor is located.

2. Move the cursor

Move cursor Direction: Directly using the four arrow keys in the keyboard ↑, ↓, ←, → complete the corresponding cursor movement

(1) Page shift

Use the PageDown key or cmh+f key combination to turn down a full page of content
Use the PageUp key or the Cth+b key to flip up a full page of content.
Where the PageDown key and PGE keys are equally applicable to Ⅵi input mode

(2) Quick jump in line

Press the home key or the ^ key, the number 0 key to quickly jump to the beginning of the line.
Press the end key or the key to quickly jump the cursor to the end of the line.
The PageDown, PageUp, Home, end, and arrow keys can also be used in the Ⅵ input mode in the above key operation

(3) Quick jump between rows

Use the key command 1G or G to jump to the 1th line of the file's contents.
Use the key command G to jump to the last line of the file.
Use the key command #g to jump to the # # line in the file (where the "#" number is replaced with a specific number).

To make it easier to see the inline jump effect, here's how to display the line number in the Ⅵ editor first. As soon as you switch to the last row mode and execute the "Setm" command to display the line number, the "Set Nonu" command can suppress the line number.

: Set Nu

The display format of the Ⅵ editor interface after displaying the line number is as follows (the number at the beginning of each line)

1 Inittab is no longer used when using SYSTEMD
2 #
3 # ADDING CONFIGURATION here would have NO EFFECT on YOUR SYSTEM
...//Omit part of the content

Quick Query Graph:

3. Copy, paste and delete (1) Delete operation

Use the X key or the DEL key to delete a single character at the cursor.
Use the key command DD to delete the current cursor line, using the form of #d can also delete the # line from the beginning of the cursor (where "#" is replaced with a specific number)
Use the key command d^ to delete all characters before the current cursor to the beginning of the line.
Use the key command to d$s delete all characters at the end of the line at the current cursor.

(2) Copy operation

Use the key command y to copy the contents of the entire row of the current line to the Clipboard, and use the #yy form to copy the #行内容 from the cursor (where the "#" number is replaced with a specific number). The copied content needs to be pasted before it can be used. In the Ⅵ editor, the contents of the previous deletion or copy are saved to the clipboard buffer, and press the P key

(3) Paste operation

You can paste the contents of the buffer after the cursor position, press the P key will be pasted at the cursor position in the command mode, press the/key after the specified string, starting from the current cursor to find the backward

Quick Query Graph:

4. Find the contents of a file

(If you press "?" The key is searched forward). After you have completed the search, you can press N, n to select from different search results. For example, enter "/Initdefault", press ENTER to find out the "F Initdefault" string in the file and highlight it, the cursor will automatically move to the first find results, press the N key to move to the next find results.

Quick Query Graph:

5. Undo Edit and Save and exit

When editing the contents of the file, sometimes you will need to undo some of the errors of editing operations, you can use the key command u, u key. where the U-key command cancels the most recent operation and restores the result of the operation, you can repeatedly press the U key repeatedly to recover a multi-step operation: the U-key command cancels all edits made to the current line.
You can press the ZZ command when you need to save the current file contents and exit the Ⅵ editor.

Quick Query Graph:

Basic operation in the last line mode

When you press the colon ":" Key in the command mode to switch to the last line mode, the ":" Prompt will be displayed in the final line of the Ⅵ editor, where the user can enter a specific last-line command, complete with rich functionality such as saving a file, exiting the editor, opening a new file, reading other file contents, and string substitution.

1. Save the file and exit the Ⅵ editor

Save the file. After modifying and confirming the contents of the file, the ": w" command needs to be saved

: W

If you need to save as a different file, you will need to specify a new file name and, if necessary, specify a path to the file.
For example, performing a "w/root/newfile" operation will save the currently edited file to the/roo directory with the file name NewFile

: W/root/newfile

Exits the editor. You can execute the "q" command when you need to exit the Ⅵ editor. If the contents of the file are
Modifications are not saved, only using the ": Q" command will not successfully exit, you need to use! "
Command forced exit (exit without saving)

: q!

Save and exit. Both save the file and exit the Ⅵ editor can use an end-line command "two"
or ": X" Implementation with the same effect as the ZZ command in command mode

: Wq

Or

: X

Quick Query Graph:

2. Open a new file or read into other file contents

Open a new file for editing. In the current Ⅵ editor, you can edit a new file by executing the last line command in the form ": E new File". For example, executing the ": e~/installlog" operation will open the install log file directly in the current user's host directory for editing

: e~/install.log

Reads the contents of other files into the current file. The last-line command in the form of "x other files" can read the contents of (read) other files and copy them to the current cursor location. For example, performing a "r/etc/filesystems" operation will copy the contents of the system file/Etc/filesystems into the current file

: R/etc/filesystems

Quick Query Graph:

3. replace file contents

In the last-line mode of the Ⅵ editor, the ability to replace specific strings in a file with new content will greatly improve editing efficiency when you need to modify the same content in large batches. The last-line command format when using the Replace feature is as follows:

: [Replace range]sub/old content/new content [/g]

In the above substitution format, the primary key is sub (substitute, replace), or it can be abbreviated as S. The replacement range is optional and, by default, only replaces the contents of the current row, which can generally be represented in the following two forms.

%: Find and replace throughout the file content.
N,m: Find and Replace in file contents within the specified number of lines.
The "G" section at the very end is also optional, which means that all matching results for each row in the replacement range are replaced, and the first matching result in each row is replaced when the "G" is omitted.

For example, to replace the "Initdefault" string in the 5th to 15th line of the document with "DEFAULT", you can do the following:

: 5,15 sub/initdefault/default/g

The "%" symbol is required to represent all when the entire document scope needs to be searched for a replacement operation. For example, the following action replaces all "Initdefault" strings in the current file with "Bootdefault".

:% sub/initdefault/bootdefault/g

If you want to prompt the user for each substitution action, you can include the "C" command at the end of the replacement command, as follows:

:%o sub/initdefault/bootdefault/c

Quick Query Graph:

Summarize

Ⅵ, Vim is a full-screen file editor, including command mode, input mode, last line mode, three kinds of usage states
! The VIM command can be returned directly to the last edit
Cut and paste functions can be achieved using the DD+P key
Use DW to delete entire words
When editing the system configuration file, remember to back up first!
We recommend using the Vim editor, which is color-coded, and general editing errors will show red
To be proficient in using VI text Editor, usually multi-use, many operations to remember these commands

(Worth collecting!) ) Vi Text editor explaining

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.