Linux VI Command

Source: Internet
Author: User
Tags save file

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.

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

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 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
with/or, if the search is not the keyword you are looking for, press N or backwards (with/to) or forward (with?) to continue looking until you find it.


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


The most powerful editor in the--vi
VI is a screen editor provided by all Unix systems, and it provides a Windows device through which you can edit files. Of course, people who know a little bit about UNIX systems,
More or less think VI super difficult to use, but VI is the most basic Editor, so hope readers can learn it well, later in the Unix world will be unimpeded,
Because several other text processors are not equipped with UNIX standards. Maybe someone else's Linux machine doesn't have Joe or Pico installed, and if you don't have VI, you probably won't.
The basic concept of VI
Basically, vi can be divided into three operation states, namely command mode, insert mode and command mode (last line mode),
The functions of each mode are differentiated as follows:
1. Comand mode: Controls the movement of the screen cursor, the deletion of a character or cursor, moves and copies a section and enters insert mode, or to last line mode.
2. Insert mode: Only in the insert mode, you can do text data input, press ESC and so on to return to Comand mode.
3. Last line mode: will save the file or leave the editor, you can also set the editing environment, such as looking for a string, listing line number, etc.
However, vi can be simplified into two modes, that is, the last line mode is also counted into command mode, the VI is divided into command and insert mode.
Basic operation of VI
? Enter VI
After the system prompt symbol to enter VI and file name, you can enter the vi full Screen editing screen:
$ VI testfile
It is important to note that you are in "Command mode" after you enter VI and you need to switch to insert mode to enter text.
First Use VI users will want to first use the next key to move the cursor, the result of the computer has been called, the gas of his half-dead, so enter the VI, do not Disturb,
After converting to insert.? switch to insert mode to edit the file by pressing ' I ', ' a ' or ' o ' three keys in command mode to enter insert mode.
You are now ready to enter text.
I: Insert to insert the input text from where the cursor is currently located.
A: increase, the current cursor is located in the next word start to enter text.
o: Insert a new line and enter text from the beginning of the beginning.
? Insert Switch →command mode, press the ESC key
You are currently in insert mode and you can only keep typing. If you find a typo, you want to use the cursor keys to move back, delete the word,
Press ESC to convert back to command mode, and then delete the text.
Leave VI and save file
Under command mode, you can type in the last line mode by the colon ":", for example:
: w filename (enter "W filename" to save the article to the file name specified by filename)
: Wq (input "Wq", because the file name is specified at the time of entry testfile, so write testfile and leave VI)
: q! (enter "q!" to force the left and discard the edited file)
Command Mode function Key List
When the Command mode command is introduced, the command is followed by a function key of the word "common", which indicates a more commonly used VI instruction.
Please be sure to learn and remember.
(1) I, a, o switch into insert mode. [Super Common]
(2) Move cursor
VI can be directly with the keyboard cursor keys 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 to move one grid.
Press CTRL+B: The screen moves backward one page. Common
Press CTRL+F: The screen moves forward one page. Common
Press Ctrl+u: The screen moves back half a page.
Press Ctrl+d: The screen moves forward half a page.
Press 0 (number 0): Moves the beginning of the article. Common
Press G: Move to the end of the article. Common
Press W: The cursor jumps to the beginning of the next word. Common
Press e: The cursor jumps to the end of the next word.
Press B: The cursor returns to the beginning of the previous word.
Press $: To move to the end of the line where the cursor is located. Common
Press ^: Moves to the first non-whitespace character of the line.
Press 0: Move to the beginning of the line. Common
Press #: Move to the first # position of the line, for example: 51, 121. Common
(3) Delete text
X: Deletes the next character at the location of the cursor every time it is pressed. [Exceptional]
#x: For example, the 6x table deletes the next 6 characters in the position of the cursor. Common
X: An x in large print, each time the first character in the position of the cursor is deleted.
#X: For example, the 20X table deletes the first 20 characters in the position of the cursor.
DD: Deletes the line where the cursor is located. [Exceptional]
#dd: For example, the 6DD table deletes text that is 6 lines down from the line where the cursor is located. Common
(4) Copy
YW: Copies the character of the cursor at the end of the word into the buffer.
(Want to be in and #x, #X的功能相反)
P: Paste the characters in the buffer to the cursor location (directive ' yw ' and ' p must be used with).
YY: The line where the cursor is copied. [Exceptional]
P: Copy the line to the place where you want to paste it. (instruction ' yy ' and ' p ' must be used in combination)
#yy: For example, 6yy means copying text from 6 lines down the line where the cursor is located. Common
P: Copy multiple lines to where you want to paste them. (instruction ' #yy ' must be used with ' P ')
"Ayy: Put the copied row in buffer A, VI provides the buffer function, can be used to present the data buffer
"AP: Paste the data placed in buffer a.
"B3yy: Deposit three rows of data into buffer B.
"b3p: Paste the data that exists in buffer B
(5) Replace
R: Replace the character at the cursor: [Frequently used]
R: Replace character until ESC is pressed.
(6) Restore (undo) Previous Instruction
U: If you mistakenly operate an instruction, you can press u immediately to revert to the previous operation. [Exceptional]
.:. You can repeat the last instruction.
(7) Change
CW: Changes the word-to-tail $ at the cursor location.
C#w: For example, the c3w represents a change of 3 words.
(8) Jump to the specified line
Ctrl+g: Lists the line number of the line where the cursor is located.
#G: For example, 15G, moves the cursor to the beginning of the 15th line of the article. Common
Last line mode instruction brief
Before you use last line mode, remember to press ESC to confirm that you are already in command mode, and then press the colon ":" or "/" or "? ”
One of the three keys goes to last line mode.
1. Column Travel number
Set Nu: After entering "set Nu", the line number is listed before each line of the article.
2. Jump to a line in an article
#: The pound sign represents a number, enter a number before the last line mode hint symbol ":", and then press ENTER to jump to that row.
such as: 15[enter] will jump to the 15th line of the article. Common
3. Searching for strings
/keywords: first press/, and then enter the word you want to look for, if the first keyword is not as you as possible, you can always press N will look down to the keyword you want.
? Keyword: press first? , then enter the word you want to look for, if the first keyword is not what you want, you can press N to look forward to the keyword you want.
4. Replace string
1, $s/string/replae/g: enter "1, $s/string/replace/g" in last line mode to replace the string string of the full text with the replace string.
1, $s means the search interval is the meaning of the article from beginning to the beginning, G is to say that all replace do not need to confirm.
%S/STRING/REPLACE/C: The string string of the full-text will also be replaced with the replace string, and the above instruction is different,%s and 1, $s is the same function,
c indicates that the substitution must be confirmed again before being substituted.
1,20s/string/replace/g: Replaces a string between 1 and 20 rows with the Relpace string.
5. Save File
W: Press W before the last line mode hint symbol ":" To save the file. [Exceptional]
#,# w FileName: If you want to extract a section of an article and save it as another file, use this command # to represent the line number, for example, 30,50 W Nice,
Save the 30th to 50th line of the article you are editing into a nice file.
6. Leave
Q: Press Q to leave, sometimes if you cannot leave VI, can be paired with "! : Strong leave VI, such as "q! ”
QW: Generally recommended to leave, with the use of W, so when leaving the file can be saved. Common

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.