Linux VI Operations Command collation

Source: Internet
Author: User

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

Vi/vim Basic Use Method
This article describes the basic use of VI (VIM), but for the average user is basically enough! I/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:
: 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: I, a, o switch into insert mode. [Super Common]
Press "i" switch into insert mode "insert mode", press "I" into the insertion mode is the beginning of the cursor from the current position to enter the 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 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.
Press "ctrl"+"b": The screen moves one page to the back. Common
Press "ctrl"+"f": The screen moves one page to the front. Common
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. Common

Press "g": Move to the end of the article. Common
Press "$": Moves to the end of the line where the cursor is located. Common
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. Common

4). Delete text
"x": Each time you press the cursor, delete the "after" character at the position where the pointer is located. [Super Common]
#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. [Super Common]
"#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. [Super Common]
"#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. Common
"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. [Super Common]

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

4. Next line Mode command brief
Before using "last line mode", remember to press "esc" to make sure you are under "command mode" and then 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. [Super Common]

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
"/Keyword: First press the"/"key, and then enter the character you want to look for, if the first keyword is not what you want, you can always press"n"will look back to the keyword you want.
"? keyword": Press "?" first Key, and then enter the character you want to find, if the first keyword 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, 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 the cursor one character to the left
L Move cursor one character to the right
K Move the cursor up one line
J Cursor moves 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
$ cursor moves to end of line
Ctrl+f Front Flip Screen
Ctrl+b Turn back the screen
Ctrl+d forward Half screen
Ctrl+u 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 line and enter it from the beginning
ESC fallback from input State to command state
X Delete the character following the cursor
#x删除光标后的 # Characters
X (uppercase x), delete the character in front of the cursor
#X删除光标前面的 # Characters
DD Delete the line where the cursor is located
#dd删除从光标所在行数的 # Line
YW a word at the location where the cursor is copied
#yw复制光标所在位置的 # words
YY the line where the cursor is copied
#yy复制从光标所在行数的 # Line
P Paste
U Cancel operation
CW change a word at the cursor location
#cw更改光标所在位置的 # words


2) The following table lists some of the instructions in the 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 changes, exit VI
Set NU Display line number
/or? Find, enter what you are looking for in/after
N and/or, if the search is not the keyword you are looking for, press N or backward (with/to) or forward (with) to continue looking until you find it.

1. About exiting
: wq! ----Force Save exit
: Wq----Save exit
: X-----Function and: Wq
ZZ----function as: WQ, (note that z is uppercase and not in command mode)
: Q----Exit
: q! ---force quit

==============================================

2. About Mobile
H: Move one character to the left of the current line
J: Move to the next line
K: Move to the previous line
L: Move one character to the right of the current line
Ctrl +f: Scroll forward one page
Ctrl +b: Scroll Backward one page
: N Positions the cursor to the nth row
: $ to position the cursor to the last row
0 position the cursor at the beginning of the bank
$ to position the cursor at the end of our line
G positions the cursor to the last line of this article, with the same functionality as: $.
H position the cursor to the top of the screen
M position the cursor in the middle of the screen
L position the cursor to the bottom of the screen


============================================
3. About Search
/: Follow up with what you're looking for, search forward in the file
?: Back with what you're looking for, search backwards in the file
N: Repeat search forward
N: Repeat Search backwards


=============================================
4. About replication

YY: Copy cursor when moving forward
Nyy: Copy the current line of the cursor to the n-1 line below the current line
: 1,100 Co 200 copies the contents of the 1~100 to line No. 200.
: 100,102 Co $ copies the contents of the 100~102 line to the last row.


==============================================
5. About pasting
P: Paste to the next line in the current row
P (Large): Paste to the previous row of the current line


==============================================
6. About Delete. Cut
DD Delete When moving forward
NDD is similar to Nyy
DW Delete a Word
NDW similar to NDD
X Delete one character
NX Delete N Characters
DG deletes all the contents of the current cursor to the end of the file.
D0 Delete all contents of the current cursor to the beginning of our line
d$ Delete all contents of the current cursor to the end of our line
: 1,100d Delete 1~100
: 100d Delete Line 100th
: 1,100 mo $ moves the contents of the 1~100 line to the last row.

=============================================
7. About inserting
I: Enter insert mode before the character in the current position
I: Insert at the beginning of the current line
A: Enter insert mode after the character in the current position
A: Insert at the end of the current line
O: Open a new row below the current line for insertion
O: Open a new row above the current line for insertion


=============================================
8. With regard to revocation

U: Undo Last Change


=============================================
9. About the replacement
RegExp: It's a pattern to match.
Replacement: Is the string to replace
: S/regexp/replacement-------------------------Replace the first style that appears on the current line
: s/regexp/replacement/g-------------------------Replace all matches of current line
:%s/regexp/replacement/g-----------------------Replace all matching styles in the file


=============================================

PS: Another important command is the "." command, which is used to repeat the previous command.

VI How to: Undo the last operation? , repeating a set of edits multiple times? .......

A) undo the last edit operation. ====> u
b) Repeat the last edit operation. =====>.
c) Restore the edited operation that was undone. ======> Ctrl + R
d) Repeat a group of edits multiple times. ====> the "." Command to repeat the last edit action.

Chsry Source: www.cnblogs.com/chsry Date: 2015-09-05-16:40:01 This blog post, most of the personal finishing development, reproduced please indicate the source

Linux VI Operations Command collation

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.