Vi/vim Basic Use

Source: Internet
Author: User

Summary:

In the minicom terminal to modify the files in the Development Board, you must use the VI, because the development Board is not like the development of the host, there are gedit and kscope such as the editor, and even in the development of the host, will often use VI, because VI will be more convenient to use. But vi for beginners, still feel the difficulty of getting started, because it is based on the command line, all the edits are based on the command line to operate, if not much will not remember, so the special will be reproduced in the order, when needed to be consulted.

Introduced:

Abstract: This article introduces the basic use of VI (VIM), but for ordinary users basically enough! Vi/vim the difference is simple, they are multi-mode editor, the difference is that Vim is the upgrade version of VI, it is not only compatible with all VI directives, but also some new features inside. For example, with syntax highlighting, visualization can be run not only at the terminal, but also in X window, Mac OS, Windows.

The VI Editor is the standard editor for all UNIX and Linux systems, and its power is not inferior to any of the latest text editors, here is simply a brief introduction to its usage and a small number of instructions. Because the VI editor is exactly the same for any version of UNIX and Linux systems, you can learn more about it in any other place where VI is introduced. VI is also the most basic text editor in Linux, after learning it, you will be in the world of Linux unimpeded.

[Simply, you can use the upper and lower left and right arrows and delete,backspace keys to move and delete positions, either in command mode or in insert mode]

1, VI of the basic concept
Basically, vi can be divided into three states, namely command mode, insert mode (and bottom line mode), and the function of each mode is 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

In general, however, we simplify the VI into two modes when we use it, that is, the bottom row mode (last line mode) is also counted as the command-line pattern.

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 and you want to switch to insert mode to enter text. First Use VI people 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, do not move, switch to "insert mode" and then!

b) switch to insert mode to edit the file
Press the letter "i" under Command mode to enter "Insert Mode", and you can start typing the text again at this point.

c) Toggle of Insert
You are currently in "Insert Mode", you can only enter text, if you find the wrong word! To move back with the cursor key, delete the word, then click the "esc" key to go to "command mode" and then delete the text.

d) Exit VI and save the file
Under Command mode, click the ":" colon key to enter "last line mode", for example:
: wFileName (enter "w filename" to save the article with the filename you specified)
: Wq(Enter "wq", save and Exit VI)
: q!(Enter q!, do not save the mandatory exit VI)

3. Command mode function key
1). Insert mode
ByI"Switch into insert mode"insert mode", press" I "Enter insert mode is the beginning of the cursor from the current position to enter the file;
ByaAfter entering insert mode, enter text from the next position where the cursor is currently located;
Byo"When you 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
ByESCKey

3). Move the cursor
VI can be directly on the keyboard with the cursor to move up and down, but the normal VI is the lowercase English letter "h", "j", "k", "l", respectively, control the cursor left, bottom, upper, and right one grid.
ByCtrl"+"b": The screen moves to" after "a page.
ByCtrl"+"F": The screen moves one page toward the front.
ByCtrl"+"u": The screen moves the half page toward" after ".
ByCtrl"+"D: The screen moves the half page toward the front.
by number "0": Moves to the beginning of the article.
ByG": Move to the end of the article.
By$": Moves to the end of the line where the cursor is located.
By^": Moves to the beginning of the line where the cursor is located
ByW": The cursor jumps to the beginning of the next word
Bye": Cursor jumps to the end of the next word
Byb": The cursor goes back to the beginning of the last word
By#l": The cursor moves to the first # position of the line, such as: 5l,56l.

4). Delete text
x": Each time you press, delete the" after "character at the location of the cursor.
#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": Copies the row of the cursor to the 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" must be mated 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. Multiple replies can be performed by pressing "U" multiple times.

8). Change
CW": Change the word at the end of the cursor
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 line 15th of the article.

4. Next line mode command brief
Before you use "last line mode", remember to press the "ESC"Key to make sure you are already under"command mode", then press":The colon can enter "last line mode".

A) List line numbers
Set Nu: After entering "set nu", 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 the line, such as entering the number 15, and then enter, you will jump to the 15th line of the article.

C) Find characters
/Keywords": Press the"/"key first, then enter the character you want to look for, if the first keyword is not what you want, you can always press"N"You will find the keyword you want."
? Keywords": Press"?"first Key, then enter the character you want to look for, and if the keyword you first looked for is not what you want, you can always press "N"Will look forward to 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, you can"q"followed by a"!" Forced to leave VI.
QW": generally recommended when leaving, 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:
hMove left cursor one character
LMove the cursor one character to the right
kMove the cursor up one line
JMove the cursor down one line
^Cursor moves to the beginning of the line
0Number "0", the cursor moves to the beginning of the article
GCursor moves to the end of the article
$Move cursor to end of line
Ctrl+FFlip the screen forward
Ctrl+bFlip the screen backwards
Ctrl+DTurn Half-screen forward
Ctrl+uTurn Back half screen
IInsert a character before the cursor position
aThe next character at the cursor position begins to increment
oInsert a new row, starting at the beginning of the line
ESCFallback from input state to command state
xDelete the character following the cursor
#x# characters after the cursor is deleted
X(uppercase x), delete the character in front of the cursor
#XDelete the # characters before the cursor
DDDelete the line where the cursor is located
#ddRemove the # line from the number of lines that the cursor contains
ywCopy a word where the cursor is located
#ywCopy the # Word where the cursor is located
yyCopy a row where the cursor is located
#yyCopy the # line from the number of lines where the cursor is located
PPaste
uCancel operation
Ctrl+RYou can revert to operations that use the U command
CWChange a word in the position of your cursor
#cwChange the # Word where the cursor is located

2) The following table lists some of the instructions in the line command mode
WFileName stores the file being edited as filename
WqFileName stores the file being edited as filename and exits VI
q!Discard all changes, exit VI
Set NuShow line Numbers
/Or?Find, enter what you are looking for in/after
NAnd/Or?Used together, if the found content is not the keyword you are looking for, press N or backward (with/to) or forward (with) to continue looking until you find it.

Annex has a master summary of the diagram, Vim command, you can print it out, put to the computer, forget to look at, very convenient, hehe.

Vi/vim Basic Use

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.