Linux Learning Note VI (VIM) editor

Source: Internet
Author: User
Tags first string line editor

Baidu's information about VI

The introduction of VI and VIM can be seen on the URL above.

1 Entering and exiting VI

Enter: In the terminal command line input VI file name will go to VI, and set up an empty name for just entered the file, if enter the vi without the file name, then enter VI after the system will automatically create an empty file, you can in the command mode to rename this file.

Exit: Press ESC to switch to command mode, enter

: Q This is not saved on the exit, the general system will prompt you to do not save

: q! Force quit, do not save

: Wq Save and exit

If you have not named the file name before, you can follow the file name you want after W

Three Modes of 2 VI

Command mode (default mode when entering):

Any input will be used as an edit command, the content will not appear on the screen, if the input error, the system will issue an error sound. Any input will react immediately;

Input mode (edit mode):

Enter the input mode from the command mode, you can enter the I-Jian, then you can enter the characters, in the input mode, press ESC will return to the command mode;

Special mode (last line mode):

Instructions that are preceded by ":" or "/" appear on the last line of the screen, and any input will be treated as a special instruction.

(So the last time we wrote the C program is: Terminal input VI hello.c into the VI editor, the default is the command mode, press I to enter the editing mode, then you can enter the program, after completion, press ESC, that is, exit the edit mode, enter the command mode, and then enter ":", That is, enter the last line mode, then we enter save Exit command Wq. )

VI in the editing of the command very much, here is not introduced, there are many online, command key is to use, only the use of proficiency, to realize the efficiency of the VI editor. Here is an excerpt from the "Linux C Programming" (Tsinghua Press, Li Yupo) in a section on vim, Vim command Basic and VI, we can refer to.

Vim (vi improve) can be said to be the most powerful editor in Linux, which is developed by the traditional text Editor vi under the UNIX system. The following is the first introduction of VI.

VI is a visual editor (vi means visualization of--visual). So, what is a visual editor? Visual editors can see them when editing text. Examples of non-visual editors can be cited, such as Ed, SED, and Edlin (which is the last editor that comes with Dos). VI became a part of the BSD Unix, and later at T also began to use VI, so standard UNIX also began with VI. Vim under Linux is an enhanced version of VI with features such as color and highlighting, which is very helpful for programming.

1. start and exit vim

Due to the many functions of vim, first look at how to start and exit vim.

(1) Type VIM at the Linux prompt (or use Vim myfile to edit an existing file) to launch it.

(2) to exit Vim, first press the ESC key back to the command line mode, and then type ":", the cursor will stay at the bottom line, and then type "Q", and finally press ENTER key, see Figure 2-1.


Under X-window, you can also run vim under X-window by finding the "programming" ︱vi I mproved in the Start menu.

2. command-line mode operation

Command-line mode provides a considerable number of keystrokes and combinations of keys to execute commands to help users edit files. Since these commands are quite numerous, this is only a brief introduction.

(1) Move cursor

In command-line mode and insert mode, you can use the up, down, left, and right 4 arrow keys to move the position of the cursor. However, in some cases, such as when using Telnet remote login, the arrow keys can not be used, you must use the command line mode of the cursor to move the command. These commands and their effects are shown in table 2-1.

Table 2-1 Common commands for moving the cursor


Operating instructions


Move the cursor one cell to the left


Move the cursor one cell to the right


Move the cursor up one cell


Move the cursor down one cell


Move the cursor to the front of the line


Move light to the last face of the line


Move the cursor to the beginning of the last line

W or W

Move the cursor to the next word


Moves the cursor to the last character of the word. If the cursor is located at the last character of the word, it bounces to the last character of the next one. Punctuation characters such as ".", "," or "/" are treated as a single word.


Moves the cursor to the last character of the word, or jumps to the first character of the previous word if the cursor is positioned as the first character of the word


Move the cursor to the previous "{" position. In C language programming, if you press two times to find the function at the beginning of "{", if you press two consecutive times can also find the beginning of the previous function


With the use of "{", move the cursor to the back "}"


You can use page down and page up if you want to look around the article, but you can use CTRL+B to scroll forward one page with the cursor when the two keys are not available, which is the equivalent of page up


Scrolls the cursor backward by one page, equivalent to page down


Move the cursor forward half a page


Move the cursor back half a page


Scrolls the cursor down one line


Scrolls the cursor back one line


Moves the cursor to the nth row (n is a number)

(2) Copy text

Copying the text saves you the time of the re-entry, and Vim also provides the following command, as shown in table 2-2.

Table 2-2 Common commands for copying text


Operating instructions


Copy the current position of the cursor to the entire row (press two times y continuously)


Copy the position of the cursor to the location of the entire word (w, which can be understood as word, that is, a word)


If you enter 3yw, you will copy the cursor position to the end of the word and the next two words (a total of 3 words)


If you press 3YY, it will be copied along with the next line in the position of the cursor.


Pastes the copied content to the location where the cursor is located. If the entire line of text is copied, the entire line is pasted to the location of the cursor

(3) Delete text

Delete a text command to delete one character at a time, or to delete several characters at a time or a whole line of text, as shown in table 2-3.

Table 2-3 Common commands for deleting text


Operating instructions


Press the D and left ARROW keys continuously to delete the previous character at the cursor position


Delete the position character of the cursor

D+ Up ARROW key

Delete the position of the cursor at the same time as the row above it


Delete the position of the cursor at the same time as the next row


Double-press Two D, you can delete the line where the cursor is located, if you delete continuously, you can hold down D


Delete the word at the cursor location, or delete a character after the cursor if the cursor is between two words


Delete the row containing the cursor and the N-line down (n is a number)

N+d+ Up ARROW key

Delete the row containing the cursor and the n rows up


With n+d+d command


Delete all words after the cursor is in line


Delete the character at the cursor position


Remove the previous character from the cursor position


Delete the position of the cursor and the n characters after it


Delete the position of the cursor and the n characters before it

(4) Find the number of rows and other keys

When we write a program, we often need to jump to a line to modify, so the line number of each line is very important. The commands that VIM provides for this are shown in table 2-4.

Table 2-4 commonly used commands to find the number of rows


Operating instructions


Shows the number of lines in the last row and the total number of articles


Moves the cursor to n rows (n is a number)


Modify the character of the cursor


Modify the character at the cursor position, and you can always replace the character until the ESC key is pressed


Represents the resiliency feature


Cancel all changes made to the line


Repeat the previous command


Input Z for two consecutive times, indicating saving the file and exiting VI


Symbol matching function, when editing, if you enter "% (", the system will automatically match the corresponding ")"

3. command line mode switch to input mode

When you enter VIM, the default mode is command-line mode, and the following keys are used when entering data into input mode:

Press "a" key to start typing from the next character at the current cursor position.

Press the "I" key to insert the newly entered character starting at the cursor position.

Press the "O" key to add a new line and move the cursor to the beginning of the next line.

4. operation of the last-line mode

Vim's last-line mode is an input command that can be displayed at the bottom of the interface, typically to perform tasks such as finding specific strings, saving and exiting. Enter the colon ":" in the command-line mode, you can enter the last line mode, you can also use the "? "and"/"keys enter the last line mode. The last-line mode has fewer operations commands than command-line mode, as shown in table 2-5.

Table 2-5 final row mode main Operations Command


Operating instructions


When editing in VI, you can use E to create a new file


Loading new files


Write the file, that is, save the edited content to the file system. Vim when editing the file, the edits are saved in the temporary file, if you do not write a direct exit, the changes are not saved to the file


If you want to write a read-only file, you can use w! Force Write file


means to exit vim, but the file content is modified, the system will prompt to save, if you do not save the exit, you need to use the command q! Force exit

Set Nu

Set can set some of the features of Vim, here is the number of prompt lines at the beginning of each line. To cancel the setting, use the command set none


Find the match string feature. When you edit, you want to find a string that contains one, you can use the "/string" to find it automatically, and the system highlights all the found strings and goes to the first string found. If you want to continue looking down, you can press the N key;

You can also use the? String "Looks up a specific string, it uses the same as"/", but it is a forward lookup string

5. Vim considerations

Since the Vim editor of the Linux system evolved from the VI under UNIX, the VI Editor under UNIX was developed from the line editor ed. As a result, vim is not as easy to use and intuitive as the popular Microsoft-launched editor, but its powerful features are unmatched by Microsoft's peers. So some people may feel some inconvenience and confusion when they study. For this type of issue, here are a few things to keep in mind when using VIM. Of course, to skillfully use vim, but also need to continue to improve and accumulate in operation.

Confusion when inserting edit mode and command mode toggle

This is usually caused by the fact that the insertion command has not been entered to start the text input, so that the text cannot be entered in the correct location, and when the information is inserted, the ESC key is not pressed to end the insertion, and the command cannot be executed by entering additional command information.

When this happens, first determine how you are doing, and then decide what to do next. If you are not sure what state you are in, you can also use the ESC key to return to the command mode to re-enter.

The VIM editor creates confusion when editing a document

This state is often caused by the screen refresh error, you can use the Ctrl+l key to refresh the screen, if it is in the terminal, you can use Ctrl+r for screen refresh.

The system does not react when the information displayed on the screen is manipulated.

This can happen because multiple processes on the screen are suspended (such as accidentally using the Ctrl+s key, etc.), and you can use CTRL+Q to release it and then re-enter it.

When the edits are complete, the VIM does not exit correctly

This can happen because of an unexpected system occurrence. Such as: The file property is read-only, the user has no write permission to the edited file. If you forcibly execute the exit command ": w! "Still unable to exit, you can use the": w newfile "command to re-save the file and then exit, to reduce the loss of work, the new file newfile should be the user has write permissions to the file.

If you do not have a file available for the time being, you can use the/tmp directory to build a new file. Because the/TMP in a Linux system is a temporary directory, the system always refreshes the directory when it starts, so the operating system generally does not protect it under this directory. However, when the processing is complete, remember to dump the new files, otherwise it will still cause information loss.

In the case of vim, what happens if the system is out of power or when the machine is suddenly in use?

When the power is off and the machine is in operation, the work being done is undoubtedly a loss, but the VIM program can minimize the loss. Because, the operation of Vim is actually the data operation to the edit buffer, and the system often saves the contents of the buffer automatically. Therefore, after the user can use the-r option to enter VI after the next login system, the last saved content in the system is restored. For example, when you edit a CD file, a sudden power outage or a system crash Recovery command is:

[[Email protected] david]$ VI CD-R

Vim learning should focus on the actual application, in understanding the use of VIM rules should be more machine operation, and constantly accumulate experience, and gradually make themselves a VI editor expert.

Linux Learning Note VI (VIM) editor

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: 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.