Vi/vim Basic Use command

Source: Internet
Author: User
Tags linux text editor

Turn from: http://www.lupaworld.com/?uid-296380-action-viewspace-itemid-118973

Vi/vim Basic Use Method
This article introduced the VI (VIM) basic use method, but for the ordinary user is basically enough. I/vim the difference is simple, they are multimodal editor, the difference is that Vim is VI upgrade version, it is not only compatible with VI of all instructions, but also some new features in it. For example, the syntax is highlighted, and visualization can be run not only on the terminal, but also on X window, Mac OS, and Windows.

The VI Editor is the standard editor for all UNIX and Linux systems, and it is not as powerful as any of the newest text editors, but simply introduces its usage and a small number of instructions. Because of any version of UNIX and Linux systems, the VI editor is identical, so you can learn more about it in any other place that introduces VI. VI is also the most basic Linux text editor, learn it, you will be in the Linux world unimpeded.

[Simply, you can use the up and DOWN ARROW and delete,backspace keys to move and delete positions, whether in command mode or insert mode]

1, VI of the basic concept
Basically VI can be divided into three states, which are command mode, insert mode (insert modes), and bottom line mode (at last), the functions of each pattern are distinguished as follows:
(1) Command mode.
Controls the movement of the screen cursor, the deletion of characters, words, or rows, the movement of a section and into the insert mode, or the last line mode.
2 Insert Mode
Only in insert mode, you can do text input, press the "esc" key to return to the command line mode.
3 End Line Mode
To save or exit VI, you can also set up an editing environment, such as finding a string, listing line numbers ... Wait

In general, however, we simplified VI to two modes in use, which means that the bottom row mode (last line mode) is also included in the command line pattern.

2, VI of the basic operation
a) into VI
In the system prompts the symbol input VI and the file name, enters the vi full screen editing Picture: $ VI myfile. But one thing to be particularly aware of is that after you enter VI, you are in command mode, and you want to switch to insert mode to enter text. The first use VI people will want to first use the next key to move the cursor, the result of the computer has been beep beep, to their own gas, so into the VI, do not move, the conversion to the "Insert mode" "Say it."

b switch to insert mode edit file
By clicking the letter "i" in command mode, you can enter insert mode, where you can begin entering text in it.

c) the toggle of Insert
You are currently in insert mode, and you can only enter text all the time if you find that you have lost the wrong word. To use the cursor key to move back, the word deleted, you need to first click the "esc" key to go to command mode, and then delete the text.

d) Exit VI and save documents
Under Command mode, click the ":" colon key to enter "last line mode", for example:
: w filename (enter "w filename" to save the article with the specified filename filename)
: Wq (input "wq", save and Exit VI)
: q! (Input q!, do not save forced exit VI)

3, command-line mode (Command mode) function key
1). Insert mode
Press "i" to switch into insert mode "insert mode", press "I" into insert mode to enter the file from the current position of the cursor;
When you press "a" into insert mode, enter the text from the next position in the current cursor position.
When you press "o" into insert mode, you insert a new row and enter text from the beginning of the line.

2). Switch from insert mode to command line mode
Press the "esc" key.

3). Move cursor
VI can be directly on the keyboard to move around the cursor up and down, but the normal VI is in lowercase English letter "h", "j", "k", "l", respectively, control the cursor left, down, up and right one lattice.
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 half a page to the back.
Press "ctrl"+"d": The screen moves half a page to 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 "^": Move to the beginning of the line where the cursor is positioned
Press "w": The cursor jumps to the beginning of the next word
Press "e": The 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 position of the line, such as: 5l,56l.

4). Delete text
"x": Every time you click, delete the "back" character at the cursor position.
"#x": for example, "6x" represents the "back" 6 characters at the location where the cursor is deleted.
"x": Uppercase X, each time, deletes the "before" character in the position of the cursor.
"#X": for example, "20x" represents the "front" 20 characters where the cursor is deleted.
"dd": Deletes the line where the cursor is located.
' #dd ': Delete # line starting at the line where the cursor is located

5). Copy
"yw": Copies the character of the cursor at the end of the word to the buffer.
' #yw ': Copy # Word to Buffer
"yy": The row to the buffer where the cursor is copied.
#yy: For example, "6yy" indicates that the copy is "down" 6 lines of text from the line where the cursor is located.
"p": Pastes the characters in the buffer into the position of the cursor. Note: All copy commands related to "Y" must be matched 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, until the "esc" key is pressed.

7). Reply Last Action
"u": If you execute a command incorrectly, you can press "u" immediately and go back to the previous operation. Press "U" multiple times to perform multiple replies.

8). Change
"cw": Change the word at the end of the cursor
"c#w": For example, "c3w" represents a change of 3 words

9). Skip to the specified line
"ctrl"+"g" lists the line number of the row where the cursor is located.
"#G": "15g", for example, represents moving the cursor to the beginning of the 15th line of the article.

4, last line mode under the command introduction
Before using "last line mode", remember to press the "esc" key to determine that you are already under "command mode", and then press the ":" colon to enter "last line mode".

A) List line numbers
"set nu": After entering "set nu", the line number is listed before each line in the file.

B jump to a row in the file
The "#":"#" number represents a number, enter a number after the colon, and then press ENTER to jump to the line, such as entering the number 15, and then enter, will jump to the 15th line of the article.

C) Find characters
"/keyword: First press"/"key, and then enter the character you want to find, if the first time you find the keyword is not what you want, you can always press"n"will look back to the keyword you want so far.
"? keywords": Press "?" first Key, and then enter the character you want to find, if the first search keyword is not what you want, you can always press "n" will look forward to the keyword you want.

D) Save the file
"w": You can save the file by typing the letter "w" the colon.

E) Leaving VI
"q": Press "q" is exit, if unable to leave VI, can be in "q" heel a "!" Forced to leave VI.
"qw": Generally recommended to leave, with "w" to use together, so that when the exit can also save the file.

5, vi command list
1 The following table lists the functions of some keys in command mode:

H move the cursor one character to the left
L MOVE the cursor one character to the right
K cursor Move up one line
The J cursor moves down one line
^ cursor move to beginning of line
0 Number "0", the cursor moves to the beginning of the article
The G cursor moves to the end of the article
$ cursor move to end of line
Ctrl+f Turn the screen forward
Ctrl+b Turn the screen back
Ctrl+d, turn the half screen forward.
Ctrl+u Turn back half screen
I inserts a character before the cursor position
A the next character at the cursor position begins to increase
o Inserts a new row, starting at the beginning of the line
ESC retreat from input status to command state
X Delete characters after the cursor
#x删除光标后的 # Characters
X (capital X), remove the character before the cursor
#X删除光标前面的 # Characters
DD deletes the line where the cursor is located
#dd删除从光标所在行数的 # Line
YW copy a word in the position of the cursor
#yw复制光标所在位置的 # a Word
YY copy row of cursor position
#yy复制从光标所在行数的 # Line
P Paste
U Cancel operation
CW changes a word in the position of the cursor
#cw更改光标所在位置的 # a Word


2) The following table lists some instructions in line command mode
W filename stores the file being edited as filename
WQ filename stores the file being edited as filename and exits VI
q! Discard all modifications and exit VI
Set NU Display Line numbers
/or? Find, enter what you want to find in/after
n is used with/or, if the lookup is not a keyword you are looking for, press N or backward (with/in conjunction) or forward (with?) until you find it.

Master Summary of the figure:


Finish.

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.