How to use the VIM editor

Source: Internet
Author: User

For the first use VI, there are a few notes to remind:

1. When you open a file with VI, you are in command mode, and you want to switch to insert mode to enter text. Toggle method: Click on the letter "i" "Command mode" to enter "Insert Mode", then you can start to enter the text.

2, after editing, you need to switch from insert mode to command line mode in order to save the file, switch method: Press the "esc" key.

3. Save and exit the file: Enter in command mode: WQ! (Don't forget to wq the front:)

1, VI of the basic concept

Basically VI can be divided into three states, namely command mode, insert mode (insert
mode) and the bottom row mode (last line mode), the functions of the modes are differentiated as follows:

1) command-line mode

Controls the movement of the screen cursor, the deletion of characters, words, or lines, moving and copying a section and entering insert mode,
Or to last line mode.

2) Insert mode

Only in the Insert mode, you can do text input, press "esc" to return to the command line mode.

3) Bottom Line mode

Save or Exit VI, or you can set the editing environment, such as looking for a string, listing line numbers ... such as
However, in general, we use the VI to simplify the two mode, that is, the bottom row mode (last lines) is also counted in
command-line mode.

2, VI of the basic operation

A) Enter VI

After the system prompt symbol to enter VI and file name, go to the VI Full screen editing screen:
$ VI myfile
However, it is important to note that after you enter VI, you are in command mode.
", you will be able to enter text by switching to" insert mode ". Anyone who uses VI for the first time will want to
Use the next key to move the cursor, the result of the computer has been a beep, the gas to death, so enter the VI, not first
Move, switch to insert mode.

b) switch to insert mode to edit the file
Press the letter "i" under Command mode to enter the Insert mode (Ins
ert mode) ", this time you can start typing text.

c) Toggle of Insert
You are currently in insert mode, you can only enter text all the time, if you find the wrong
The word! To use the cursor keys to move back, delete the word, you must first click the "esc" key to go to the command line mode (c
Ommand mode) "Delete the text again

d) Exit VI and save the file

Under Command mode, click the ":" colon key to enter "last.
", for example:

: w filename (enter "w filename" to save the article with the filename you specified)

: Wq (input "wq", save and Exit VI)

: q! (Enter q!, do not save the mandatory exit VI)

3. Command mode function key

1). Insert mode
Press "i" to switch into insert mode "insert mode", press "I" after entering insert mode is from the cursor current bit
Start input file;

After pressing "a" into insert mode, the text is entered from the next position at the current cursor position;

When you press "o" to enter insert mode, you insert a new line and enter text from the beginning of the beginning.

2). Switch from insert mode to command line mode

Press the "esc" key.

3). Move the cursor
VI can be directly with the cursor on the keyboard to move up and down, but the normal VI is a lowercase English letter "h", "
j","k","l", respectively, control the cursor left, bottom, up, and right one grid.

Press "ctrl"+"b": The screen moves one page to the back.

Press "ctrl"+"f": The screen moves one page to the front.

Press "ctrl"+"u": The screen moves the half page toward the back.

Press "ctrl"+"d": The screen moves the half page toward the front.

"0" by number: Moves to the beginning of the article.

Press "g": Move to the end of the article.

Press "$": Moves to the end of the line where the cursor is located.

Press "^": Moves to the beginning of the line where the cursor is located

Press "w": Cursor jumps to the beginning of the next word

Press "e": Cursor jumps to the end of the next word

Press "b": Cursor back to the beginning of the last word

Press #l: The cursor moves to the first # position of the line, such as: 5l,56l.

4). Delete text
"x": Each time you press the cursor, delete the "after" character at the position where the pointer is located.

#x: For example, "6x" represents the "after" 6 characters of the location where the cursor was deleted.

"x": Uppercase X, each time it is pressed, deletes the "front" character at the location of the cursor.

"#X": for example, "20x" represents the "front" 20 characters of the location where the cursor was deleted

"dd": Delete the line where the cursor is located.

"#dd": Deletes the # line starting at the line where the cursor is located

5). Copy

"yw": Copies the characters from the cursor to the end of the word into the buffer.

"#yw": Copy # words to Buffer

"yy": Copy cursor line to buffer.

"#yy": for example, "6yy" represents a copy of 6 lines of text from the line where the cursor is located.

"p": Paste the characters in the buffer to the cursor location. Note: All copy commands related to "Y" are

You must mate with "P" to complete the copy and paste function.

6). Replace

"r": Replaces the character at which the cursor is located.

"r": Replaces the character where the cursor is located until the "esc" key is pressed.

7). Reply to Last action

"u": If you mistakenly execute a command, you can press "u" immediately to go back to the previous action. Press multiple "U"
You can perform multiple replies.

8). Change

"cw": Change the word-to-tail at the cursor location

"c#w": For example, "c3w" represents a change of 3 words

9). Jump to the specified line

"ctrl"+"g" lists the line number of the line where the cursor is located.

"#G": for example, "15g", which means moving the cursor to the beginning of the 15th line of the article.

4. Next line Mode command brief

Before using "last line mode", remember to press "esc" to determine that you are already in "command m
After ode", press":"colon to enter"last line mode".

A) List line numbers

"set nu": After you enter "set nu", the line numbers are listed before each line in the file.

B) jump to a line in the file

The "#":"#" number represents a number, enter a number after the colon, and then press ENTER to jump to that line.

If you enter the number 15, and then enter, you will jump to the 15th line of the article.

C) Find characters

"/keyword": First press the "/" key, and then enter the character you want to look for, if the first keyword is not you want to
Yes, you can always press "n" to find the keyword you want.
"? keyword": Press "?" first Key, and then enter the character you want to look for if the first keyword is not what you want to
Yes, you can always press "n" to find the keyword you want.

D) Save the file

"w": Enter the letter "w" the colon to save the file.

E) Leave VI

"q": Press "q" is to exit, if you cannot leave VI, can be "q" followed by a "!" Forced to leave VI.

"qw": Generally recommended to leave, use with "w", so you can also save the file when exiting.

5. VI Command List

1. The following table lists the functions of some keys in the command mode:

H
Move left cursor one character

L
Move the cursor one character to the right

K
Move the cursor up one line

J
Move the cursor down one line

^
Cursor moves to the beginning of the line

0
Number "0", the cursor moves to the beginning of the article

G
Cursor moves to the end of the article

$
Move cursor to end of line

Ctrl+f
Flip the screen forward

Ctrl+b
Flip the screen backwards

Ctrl+d
Turn Half-screen forward

Ctrl+u
Turn Back half screen

I
Insert a character before the cursor position

A
The next character at the cursor position begins to increment

O
Insert a new row, starting at the beginning of the line

Esc
Fallback from input state to command state

X
Delete the character following the cursor

#x
# characters after the cursor is deleted

X
(uppercase x), delete the character in front of the cursor

#X
Delete the # characters before the cursor

Dd
Delete the line where the cursor is located

#dd
Remove the # line from the number of lines that the cursor contains

yw
Copy a word where the cursor is located

#yw
Copy the # Word where the cursor is located

Yy
Copy a row where the cursor is located

#yy
Copy the # line from the number of lines where the cursor is located

P
Paste

U
Cancel operation

cw
Change a word in the position of your cursor

#cw
Change the # Word where the cursor is located


2, the following table lists some instructions in the line command mode

W filename
Save the file you are editing as filename

WQ filename
Save the file being edited as filename and exit VI

Q!
Discard all changes, exit VI

Set Nu
Show line Numbers

Or?
Find, enter what you are looking for in/after

N
Used with/or, if the found content is not the keyword you want to find, press N or backwards (with/to) or forward (
Continue searching until you find it.

How to use the 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: 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.