Ubuntu Tools: VI Editor

Source: Internet
Author: User

about VI

VI is the abbreviation of "Visual interface", its status on Linux as if the edit program on DOS. It can perform many text operations, such as output, delete, find, replace, block operations, and so on, and users can customize them according to their own needs, which is not available in other editing programs.

VI is not a typesetting program, it is not like word or WPS can be the font, format, paragraph and other properties of the orchestration, it is just a text editing program.

VI There are no menus, only commands, and commands are numerous. VI has three basic modes of operation: command line mode, text input mode, and last line mode.

1. Command-line mode

At any time, no matter what mode the user is in, just press the key to make VI into the command line mode; We enter the Launch VI command under the shell environment (prompt $), and it is in that mode when you enter the editor.

In this mode, users can enter a variety of legal VI commands for managing their own documents. Any character entered from the keyboard is interpreted as an edit command, and if the input character is a valid VI command, VI completes the corresponding action after accepting the user command. Note, however, that the commands you enter are not displayed on the screen. If the input character is not a legal command of VI, VI will ring the alarm.

Cursor movement;

When we press ESC to enter command mode, we can use some of the following keys to move the cursor;

j 向下移动一行; k 向上移动一行; h 向左移动一个字符; l 向右移动一个字符; ctrl+b 向上移动一屏; ctrl+f 向下移动一屏; 向上箭头 向上移动; 向下箭头 向下移动; 向左箭头 向左移动; 向右箭头 向右移动;

When we edit a file, for the J, K, L, and H keys, we can also add a number to the front of these action commands, such as 3j, which means moving down 3 rows.


Text Input mode

Enter the input command in command mode i, attach command A, open command O, modify command C, replace command R or Replace command s can enter the text input mode. In this mode, any characters entered by the user are saved by VI as the contents of the file and displayed on the screen. In the process of text input, if you want to return to the command mode, press the button. i 在光标之前插入; a 在光标之后插入; I 在光标所在行的行首插入; A 在光标所在行的行末插入; o 在光标所在的行的上面插入一行; O 在光标所在的行的下面插入一行; s 删除光标后的一个字符,然后进入插入模式; S 删除光标所在的行,然后进入插入模式; 文本内容的删除操作; x 一个字符; #x 删除几个字符,#表示数字,比如3x; dw 删除一个单词; #dw 删除几个单词,#用数字表示,比如3dw表示删除三个单词; dd 删除一行; #dd 删除多个行,#代表数字,比如3dd 表示删除光标行及光标的下两行; d$ 删除光标到行尾的内容; J 清除光标所处的行与上一行之间的空格,把光标行和上一行接在一起; Restore the modification and restore the delete operation; u 撤消修改或删除操作;

Press ESC to return to command mode, and then press the U key to undelete the previous delete or modify, or press multiple times if you want to undo multiple previous modifications or deletions. This is not much different from the undo operation of Word;



last-line mode

The last line mode is also known as ex escape mode.

The functions of the VI and ex editors are the same, the main difference being the user interface. In VI, commands are usually a single key, such as I, A, O, etc., whereas in ex, the command is the body line that ends with a hit return. VI has a dedicated "escape" command to access many of the line-oriented ex commands. In command mode, the user presses the ":" Key to enter the last row mode, at which time VI displays a ":" As a prompt in the final row of the display window (usually the last line of the screen), waiting for the user to enter a command. Most file management commands are executed in this mode (such as writing the contents of the edit buffer to a file medium). After the last command is executed, VI automatically returns to the command mode. For example:

: 1, $s/A/a/g

Convert uppercase A to lowercase a from the first line of the file to the end of the file.

If you change your mind when you enter a command in the last line mode, you can press the key, or use the BACKSPACE key to delete all the commands you have entered, and then click Backspace to make VI return to command mode.



VI's entry and exit

The entry of Vi

After the user logs on to the system, the system gives the prompt "$". Type VI after the prompt and the name of the file you want to edit (or create) to enter VI.

[Example 1] Type the command:

$ VI example.c

If you type only VI, without the file name, you can enter VI. After you type the contents of the file at the cursor and exit VI, simply enter the file name after exiting the command.

After entering VI, the first entry is the command mode, which means waiting for command input instead of text input. The letters entered will be interpreted as commands. The cursor stops at the top of the first line of the screen (denoted by), and the remaining lines start with a "~" symbol, which indicates that the behavior is a blank line. The last line, also called the status line, shows the file name currently being edited and its status. If this example is [new file], example.c is a newly created file. If the example.c file already exists in the system, after entering the above command, the contents of the file are displayed on the screen, and the cursor stops at the top of the first line, showing the file's filename, number of lines, and number of characters in the status line.

[Example 2] Type the command:

$ VI example.c

Note that in order to save space, the screen only shows 15 rows. In fact, the initial display line is related to the user's terminal, the general CRT terminal can display 25 lines. In the window system, the number of rows displayed is related to the window that runs VI. However, we can set the number of rows displayed. For example, on a CRT terminal that can display 25 rows, let VI display only 15 rows. The methods you set are described later in this section.

When using VI to create a new file, in the command to enter VI can also not give the file name, when editing the file needs to save data, and then by the user to specify the file name.

When entering VI, the user can not only specify a file name to be edited, but also have many additional operations.

If you want the cursor to be on a specific line in the file after you enter VI, you can add any option +n after VI, where n is the specified number of rows.

[Example 3] Type the command:

$ VI +5 example1.c

The cursor will be located on line 5th in the file example1.c.


You can use VI to edit multiple files at once, as long as you write the file you want to manipulate in the command that goes to VI, and you have a wildcard character.

[Example 5] Type the command:

$ VI *.cat

You can edit all files with the suffix cat. Of course, you can also use the built-in pattern matching in the shell to load files, which is more tricky. Once you have included all the file names you want to edit on the command line, you can easily move around in VI.

Exit VI

When you are finished editing the file, ready to exit VI when you return to the shell, you can use one of the following methods.


In command mode, double-press two capital Letter Z, if the currently edited file has been modified, then VI save the file and exit, return to the shell; if the currently edited file has not been modified, VI exits directly and returns to the shell.


In the last line mode, enter the command

: W

VI saves the current edit file, but does not exit, but continues to wait for the user to enter the command. When you use the W command, you can then give the edit file a new file name.


[Example 6]

: w newfile

At this point VI will save the contents of the current file to the specified NewFile, while the original file remains unchanged. If NewFile is an existing file, VI gives a hint in the status line of the display window:

File exists (use!) To override)

At this point, if the user really wants to replace the contents of NewFile with the current contents of the file, you can use the command

: w! NewFile

Otherwise, you can select a different file name to save the current file.


In the last line mode, enter the command

: Q

System Exit VI returns to the shell. If the edit file is not saved when you exit VI with this command, VI displays the following information in the last line of the display window:

No write since last change (use!) To overrides)

Prompts the user that the file has been modified without saving, and then VI does not exit, continuing to wait for the user command. If the user does not want to save the modified file and want to forcibly exit VI, you can use the command

: q!

VI Discard the changes and return directly to the shell.


In the last line mode, enter the command

: Wq

VI will save the file first and then Exit VI to return to the shell.


In the last line mode, enter the command

: X

The function of this command is the same as the ZZ command in command mode.

Restore the modification and restore the delete operation;

u 撤消修改或删除操作;

Press ESC to return to command mode, and then press the U key to undelete the previous delete or modify, or press multiple times if you want to undo multiple previous modifications or deletions. This is not much different from the undo operation of Word;

Visual mode;

In the latest Linux distributions, VI provides visual mode because this function is vim. If you use the VI does not have this function, you can replace it with vim. Open the visual mode, press ESC, then press V to enter the visual mode;

The visual mode gives us a very friendly selection of text ranges to highlight, which is displayed at the bottom of the screen;

-- 可视 -- 或 --VISUAL--

Entering the visual mode, we can select the text range by using the cursor movement instruction in the preceding command line mode.

What is the use of selecting a text range?

We can delete the job for some part, press the D key to delete our selected content. When the content is selected, we press Y to copy, and D to delete;

It's worth mentioning. The deletion also represents replication. We return to the command mode, then move the cursor to a location, and then press the Shift+p key to paste the content just removed. Let us mention here first, in the following article, we have to say in detail.

Exit the visual mode, or use the ESC key;

3.9 Copy and paste the operation;

In fact, the deletion also has the meaning of shearing, when we delete the text, you can move the cursor to a place, and then press shift+p key to paste the content in place, and then move the cursor to somewhere, and then press P or shift+p and can be affixed;

p 在光标之后粘帖; shift+p 在光标之前粘帖

To give an example:

For example, if we want to copy the third line of a document and post it to line fifth, what should we do?

There are two ways;

The first method:

First delete the third line, move the cursor to the third line, then use the DD action, and then click the Shift+p key. This puts the third line that you just deleted.

Then we use the K key to move the cursor to line fifth, and then press the P key, so that the third row of the contents of the fifth line after the;

The second method;

Enter visual mode, press the ESC key, and then press the V key. Move the mouse pointer, select the contents of the third row, then press the Y key to copy, then move the pointer to line fifth, and finally press the P key;

Therefore, the copy and paste operation, is the command mode, insert mode and visual mode of the comprehensive use; we must learn to switch between the various modes, to use the ESC key, more important to learn to move the cursor in the command mode;

About line numbers;

Sometimes when we configure a program to run, there is an error in the configuration file X line. At this point we need to use the line number related operations;

Add line numbers for all content;

Press the ESC key, and then enter:

:set number

Where the cursor is located

In the lower right corner of the screen, there is a similar following;

         57,8 27%

In this, 57 represents line 57th, and 8 represents the 8th character;

Find and replace functions;

Find

First of all, we have to enter the ESC key, enter the command mode, we enter/or to enter the search mode;

/SEARCH 注:正向查找,按n键把光标移动到下一个符合条件的地方; ?SEARCH 注:反向查找,按shift+n 键,把光标移动到下一个符合条件的

For example : I want to find the swap word in a file, I should do the following;

First press the ESC key, enter command mode, and then enter;

/swap 或 ?swap

Replace

Press Esc key to enter command mode;

:s /SEARCH/REPLACE/g 注:把当前光标所处的行中的SEARCH单词,替换成REPLACE,并把所有SEARCH高亮显示; :%s /SEARCH/REPLACE 注:把文档中所有SEARCH替换成REPLACE; :#,# s /SEARCH/REPLACE/g 注:#号表示数字,表示从多少行到多少行,把SEARCH替换成REPLACE;

Note: In this, G is the global lookup; We notice that the search is highlighted in places where there is no replacement;

Ubuntu Tools: VI Editor

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.