Three modes of VIM in Linux and basic commands

Source: Internet
Author: User
Tags switches

The three modes of VIM in Linux are: command mode , last line mode , and edit mode . here is a diagram of the three:

Switch between the three modes:

The command mode is the default mode in Vim.

Command mode switch to last line mode : Use colon (:) in English.

last-line mode switches to command mode : Press ESC or double ESC or delete all commands from the last line.

Command mode switch to edit mode : Key I or a.

edit mode tangent back to command mode : Press ESC.

among them, between the last line mode and the editing mode, cannot switch directly , must switch to the command mode, then switches.

Next, refine the various command operations in each mode:

First, last line mode

the basic operations in the last-line mode are : saving, save , exit, find, replace .

1. Save

Syntax:: w (write)

Description: Saves the changes you have made, mainly after the edit mode operation is complete.

2. Save  

Syntax:: W file path

Note: Saving and save commands are basically similar, except that the save path to the specified file is saved.

    

3. Exit

Syntax:: Q (Quit)

Description: If you do not want the throne to exit directly you can use (: q!), where! indicates mandatory

    

4. Find

Syntax:/String to find

Note: You can change the cursor position by using n (toggle down), n (toggle up). The location you are looking for is highlighted

    

5. Replace

Syntax:: S/substituted characters/substituted characters

Description: Replaces the first occurrence of a character in the current row

    

Syntax:: S/substituted characters/substituted characters/g

Description: Replaces all matching characters for the current line , and G represents the global

    

Syntax:%s/substituted character/substituted character

     Description: A matching substitution for the first matching string for each line of the entire document

    

Syntax:%s/substituted string/substituted string/g

Description: Replace the characters on the entire document match

    

Second, the command mode

the basic operations in the command mode are : Move, delete, and copy the cursor .

1, the movement of the cursor

Syntax: Navigate to Last Row (G)

Position to first row (two GG)

Cursor is positioned to the specified line (number +g)

Cursor top or Move down (digital +↑ or digital +↓)

2. Delete

Syntax: Delete current line (DD) [line number reset]

Description: Deleting the next line in the current row does not move up , and the document line number is re-indexed.

Syntax: Delete current line (D) [Current line blank, keep current line number]

Description: Delete the next line in the current row does not move up, leaving the current line blank after deletion

Syntax: delete multiple lines (digital +DD)

Description: Deletes the number of n rows in the next row of the current cursor, and n represents the number filled.

Note: In vim, the delete and cut commands are the same and can be displayed with the Paste command.

3. Copy

Syntax: Copy current line (YY)

Syntax: Copy multirow (digital +yy)

Description: Indicates the number of rows specified to copy the current line

Tip: After the copy is complete, you can paste it with P (parse) and paste it on the next line of your row

Third, the editing mode

the basic operations in the command mode are : Enter Edit and exit .

1. Enter the editor

Syntax: A (AfterInsert) means that editing is inserted after the cursor

I (insert) indicates an edit insertion at the current cursor

2. Exit

Syntax: Exit edit mode with ESC

Iv. expansion

1, the line number display problem

In general, Vim is displayed by the line number by adding the Set NU command in the last line mode, but this is not

Globally, the line number will automatically disappear the next time the file is opened, and for ease of operation we will usually

Modify the Vim configuration file so that the line number display is in effect for the global.

The configuration file for Vim is located in the current user's home directory. If you do not create it yourself. The file name is called. VIMRC

    

Enter "Set Nu" in the current file to enable the global line number display to be valid.

2, abnormal shutdown processing

If the file does not shut down properly, it is closed directly through the remote window if it is not saved.

    

This problem is resolved by deleting the file's swap file

The prompt information is already very clear. Swap files. VIMRC.SWP is created automatically when the file is not closed properly.

So use the command: Rm-f. VIMRC can

Note: the usual format for a generic interchange file is: file name. SWP

     3. Redo/Revoke

If you want to undo the operation, enter in the last line mode: U

Cancel Undo to: Ctrl + R

Multi-Step Undo is: Number + U

4,: Wq and: What is the difference between the way to exit X?

In actual development or recommended use: Exit X.
The difference is in the way Wq changes the file's last modified time regardless of whether or not the file is modified.
And: X modifies the last modification time of the file only if the file content really changes.

Three modes of VIM in Linux and basic commands

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