About the Linux VI Command VI command list

Source: Internet
Author: User
Tags first string save file

VI is a screen editor provided by all Unix systems, and it provides a Windows device through which you can edit files. Of course, the UNIX system slightly know people, more or less think VI super difficult to use, but VI is the most basic Editor, learn the VI, in the UNIX world will be unimpeded, comfortable, because several other text processors are not UNIX standard equipped. If you don't have Joe or Pico installed on other Linux machines, you might not be able to use VI.

The basic concept of VI
Basically, vi can be divided into three operation states, namely command mode, insert mode and bottom command mode (last line mode), the function of each mode is as follows:
1. Comand Mode: Controls the movement of the screen cursor, the deletion of the 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 searching for strings, listing line numbers, 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
1. Enter VI
After the system prompt symbol to enter VI and file name, you can enter the vi full screen editing screen, such as: $ 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 air of their own half-dead, so enter the VI, do not move, the conversion into insert after.
2. Switch to the Insert mode edit file
Press ' I ', ' a ' or ' o ' three keys under command mode to enter insert mode. This is the time to start typing text.
 i : Insert to insert the input text from where the cursor is currently located.
         a : Increment, the next word where the cursor is at the beginning of the input text.
         o : Insert a new line and enter text from the beginning of the beginning.
3. Insert mode switches to command mode
    press esc key. If you are currently in insert mode, you can only keep typing. If you find a typo, want to use the cursor key to move back, the word deleted, you will press ESC to convert to command mode, and then delete the text.
4. Leave VI and save file
    under command mode, you can press the colon ": ", go to last line mode , for example:
       : w filename (enter "W filename" to save the article to the specified file name filename)
        : Wq (enter "Wq" because the file name testfile is specified at the time of entry, so it will be written to 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.
1. I, a, o switch into insert mode. [Super Popular]
2. Move the cursor
VI can be directly on the keyboard with the cursor keys 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 shift 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 o: To 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. [Super Popular]
NX: For example, the 6x table deletes the next 6 characters in the position of the cursor. [Common]
x: Uppercase X, each time the first character of the cursor position is deleted.
NX: 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. [Super Popular]
NDD: For example, the 6DD table deletes 6 lines of text from the line where the cursor is located. [Common]
4. Copying
YW: Copies the character of the cursor at the end of the word into the buffer.
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. [Super Popular]
P: Copy the line to the place where you want to paste it. (instruction ' yy ' and ' p ' must be used in combination)
Nyy: 6yy means a copy of the text that is 6 lines down from 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:: Replaces the character at which the cursor is located: [Common]
R: Replace character until ESC is pressed.
6. Revert (undo) Previous Instruction
U: If you mistakenly operate an instruction, you can press u immediately to revert to the previous operation. [Super Popular]
.:. You can repeat the last instruction.
7. Change
CW: Changes the word-to-tail $ at the cursor location.
CNW: For example: c3w represents a change of 3 words.
8. Skip to the specified line
Ctrl+g: Lists the line number of the line where the cursor is located.
NG: For example: 15G, moves the cursor to the beginning of the 15th line of the article. [Common]

Last line mode instruction Brief
Before using last line mode, remember to press the ESC key 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. List line Numbers
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 N: # represents a number, enter a number before the last line mode hint symbol ":", and then press ENTER to jump to that row, for example, 15[enter] to jump to the 15th line of the article. [Common]
3. Searching for strings
/keywords: first press/, then enter the word you want to look for, if the first keyword is not expected to find, you can always press N to search down until the desired keyword.
Keywords: First press, then enter the word you want to look for, if the first keyword is not expected to find, you can always press N to look down until the desired keyword.
4. Replacing strings
1, $s/string/replae/g: "1, $s/string/replace/g" will replace the full-text string string with the replace string, where 1, $s means the search interval is the meaning of the article from the beginning to the beginning. , G is an indication that all substitutions do not have to be confirmed.
%S/STRING/REPLACE/C: Similarly, the full-text string string is replaced with the replace string, and the above instruction is different,%s and 1, $s is the same function, and C is to indicate 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 the file
W: Press W before the last line mode hint symbol ":" To save the file. [Super Popular]
N,n w FileName: If you want to extract a section of an article and save it as another file, use this instruction N to represent the line number, for example: 30,50 w Nice, save the 30th to 50th line of the article you are editing as nice.
6. Exit
Q: Press Q to exit, sometimes if you cannot exit VI, can be paired with "!" : Strong exit VI, for example: "q!"
QW: Generally recommended when exiting, use with W, so you can also save files when exiting. [Common]


VI Command List

A text editor is the most commonly used tool in all computer systems. The editor under Unix has ex,sed and VI, among them, the most widely used is VI, and the VI command is numerous, the following:

command to enter VI
VI FileName: Opens or creates a new file and places the cursor at the beginning of the first
VI +n FileName: Opens the file and places the cursor at the beginning of nth
VI + FileName: Opens the file and places the cursor at the beginning of the last line
VI +/pattern FileName: Opens the file and places the cursor at the first string that matches the pattern
Vi-r FileName: The last time you were editing with VI, a system crash occurred, restoring filename
VI filename....filename: Open multiple files, edit them sequentially

move Cursor Class command
H: Move the cursor one character to the left
L: Move the cursor right one character
Space: Move the cursor right one character
Backspace: Cursor moves left one character
K or ctrl+p: Move the cursor up one line
J or CTRL + N: Move the cursor down one line
Enter: Move the cursor down one line
W or W: Move the cursor right one word to the beginning of the word
B or B: The cursor moves left one word to the beginning of the word 
E or E: The cursor moves right one word to the tail  
) : The cursor moves to the end of the sentence  
(: The cursor moves to the beginning of the sentence  
} : The cursor moves to the beginning of the paragraph  
{: The cursor moves to the end of the paragraph  
NG: Cursor moves to nth line  
n+: Cursor moves down n rows N: Move the cursor up n rows  
n$: Move the cursor to the end of Nth line  
H: The cursor moves to the top line of the screen  
M : The cursor moves to the middle line of the screen  
L: The cursor moves to the last line of the screen  
0: (note is the number 0) the cursor moves to the current beginning  
$: Cursor moves to the end of the current line  
~: Changes the case of the character that the cursor is in
.: Repeat the previous action
<<: Move the current line to the left
>>: Move the current line to the right

strong> screen Tumbling class command &NBSP;
Ctrl+u: Flip Half screen to file &NBSP;
Ctrl+d: Half-screen to the end of the file &NBSP;
Ctrl +F: Flip a screen to the end of the file &NBSP;
Ctrl+b; turn one screen at the top of the file NZ: Rolls line N to the top of the screen, and rolls the current line to the top of the screen when n is not specified.

Insert text class command &NBSP;
I :   before cursor;
I:   at the beginning of the current line;
A: After the cursor Span class= "Apple-converted-space" >&NBSP;
A: At the end of the current line &NBSP;
O: A new line under the current line Span class= "Apple-converted-space" >&NBSP;
O: New line above the current line &NBSP;
R: Replace the current character &NBSP;
R: Replaces the current character and its subsequent characters until the ESC key is pressed  
S: Starts at the current cursor position and replaces the specified number of characters with the input text &NBSP;
S: Deletes the specified number of lines and replaces them with the input text &NBSP;
NCW or NCW: Modifies a specified number of words &NBSP;
NCC: Modifies a specified number of rows

Modify and replace text
C Replace all characters starting at the cursor position to the end of the line
R replaces one or more characters at the beginning of the cursor position
CC replaces all characters of the line where the cursor is located

Delete command
NDW or NDW: Delete the n-1 characters at the beginning and after the cursor
Do: Delete to the beginning of the line
d$: Delete to end of line
NDD: Deletes the current line and its subsequent n-1 rows
X or x: Deletes a character, x deletes the cursor, and x deletes the cursor before the
Ctrl+u: Delete text entered under input mode
X: Delete the character of the cursor
X: Delete a character before the cursor
DW: Delete A word from the beginning of the cursor
DD: Delete cursor in the row
Do: Delete to the beginning of the line
d$: Delete to end of line

search and replace command &NBSP;
/ Pattern: Search from the beginning of the cursor to the end of the file Pattern&NBSP;
? Pattern: Searches to the top of the file from the beginning of the cursor Pattern&NBSP;
N: Repeats the last search command in the same direction &NBSP;
N: Repeats the last search command in the opposite direction &NBSP;
: s/p1/p2/g: Replaces all P1 in the current row with P2 instead of &NBSP;
: n1,n2s/p1/p2/g: All n2 in line N1 to P1 are replaced with P2  
: g/p1/s//p2/g: Replace all P1 in the file with P2 &NBSP;
fa: Searches for a position where a appears next to the current line
FA : Searches for a where a appears on the current line
TA: Search for a character before the next occurrence
Ta: The character after the last occurrence of a search
;: Repeat the most recent search command

option set &NBSP;
All: List all option settings &NBSP;
Term: Set terminal type Ignorance: Ignores case   in Search;
List: Show tab stops (Ctrl+i) and end of line flag ($) &NBSP;
Number: Displays the line number &NBSP;
Report: Displays the number modified by the line-oriented command &NBSP;
Terse: Displays a brief warning message &NBSP;
Warn: Displays no write information if the current file is not saved when you go to another file &NBSP;
Nomagic: Allows the use of special characters that are not preceded by "\" In search mode &NBSP;
Nowrapscan: Prohibit VI from the other end when the search reaches both ends of the file &NBSP;
Mesg: Allow VI to display information that other users write to their terminal

last-line command &NBSP;
: N1, N2 CO N3: Copy the contents of the N1 line to the N2 line N3 &NBSP;
: n1,n2 D: Deletes the contents of N1 rows to N2 rows &NBSP;
: w: Save current file &NBSP;
: E filename: Open file filename for editing &NBSP;
: x: Save the current file and exit &NBSP;
: Q: Exit Vi&NBSP;
: q!: Do not save the file and exit Vi 
:!command: Execute shell command command&NBSP;
: N1,n2 w! Command: The contents of the N1 line to the N2 line in the file as input and execution of the command, if not specified n1,n2, the entire file content as the command input : R!command: Puts the command's output to the current line

Register Operation
"? Nyy: Saves the contents of the current row and its next n rows to the register?" , where? is a letter, n is a number
"? NYW: Saves the current line and its next n characters to the register?" , where? is a letter, n is a number
"? Nyl: Saves the current line and its next n characters to a register?" , where? is a letter, n is a number
"? P: Remove the Register?" and place it at the cursor position. Over here? Can be a letter, or it can be a number
NDD: Deletes the current line and its total n lines of text, and places the deleted content in the 1th delete register.

Plus

1. Display line number:
: Set Nu
2. Open another file:
: New file name (new if not present)
3. Jump to a line in a file: for example, 123 rows
123 Shift+g
4. When two (or more) files are open at the same time, switch the cursor between the two files:
CTRL + W W
5. Maximize one of the file windows when opening two (or more) files at a time
: Ctrl +w Shift+-
6. Go directly to the end of the file
SHIFT + G
7. Save the file and exit
: Wq
8. Save file does not exit
: w!
9. exit File Not saved
: q!
10. Quickly find a keyword in the file
1>/keyword Enter
2> move the cursor to the keyword you want to find and then press SHIFT + 8 (in fact the * number)

11. Set what encoding to open the file:
: Set ENCODING=EUC-JP (encoded)
12. Open the file for re-reading in:
: E
13. Set a user under the VI command to open the file with the encoding method:
For example:
[Email protected]/]$ VI ~/.VIMRC
Input: Set ENCODING=EUC-JP
After saving the exit, in the future under the user, the VI command to hit the file will default to EUC-JP encoding lattice
Expression.

14. Execute the shell command line in VI (to be done in the last line mode)
":! Command

16. To the file locking (last line mode)
": X": Enter the password after the ENTER key prompt appears. The next time you enter the file, the format
For "Vi-x filename", you will be prompted to enter the password.
17. Simplified input (last line mode)
": AB String Strings": use string instead of strings when editing
Eg: ": AB Rd Redstone Info": When entering "RD" in the text later and enter
, "Redstone Info" will appear
instead of Rd
18. Custom keyboard commands (last line mode, less dangerous, not recommended)
": Map Keys New_seq"
19. Display line numbers (in the last row mode, coding works well)
": Set number" displays line numbers
20. Looking for a function when coding with VI
First, in the shell environment with the ctags command to the current directory of C original program scanning,
Generate tags file
eg:$ Ctags *.c
Then, in VI can be the command ": Ta fun_name", you can directly open the function
The file where the Fun_name is located and places the cursor at the beginning of the function
21. Find out the differences between the two files
After opening a file with VI, press CTRL and drag it into a file.
Compare the two horizontal windows with the command crtl+w shift+l
Press F12 after you switch to that window with crtl+w and press F12 to find the difference of two files
DP: Copy one side of the text to the other side
Diffu: is to re-find the two different folders
}+ (: Find the next different
F9: is the encoding format for switching text

About the Linux VI Command VI command list

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.