Introduction to the use of the VI command under Linux

Source: Internet
Author: User
Tags first string save file

One of the most powerful editors--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, the UNIX system slightly know people, 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, comfortable, because several other text processors are not UNIX standard equipped. 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 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 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 air of their own half-dead, so enter the VI, do not move, the conversion into insert after.
? switch to insert mode to edit the file
Press ' I ', ' a ' or ' o ' three keys under 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, want to use the cursor key to move back, the word deleted, you will press the ESC key to return 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 instructions are appended with the function keys of the word "commonly used", which indicates the more commonly used VI directives, so readers must 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 prompt symbol ":", and then press ENTER to jump to the 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: "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 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, you can use this command # to represent the line number, for example, 30,50 W Nice, to 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

VI Command Highlights:

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: Move the cursor right one word j to the end of the word

): Move the cursor to the end of the sentence

(: The cursor moves to the beginning of the sentence

}: Move the cursor to the beginning of the paragraph

{: Cursor moves to end of paragraph

NG: Cursor moves to the beginning of nth

n+: The cursor moves down n rows

N: Move the cursor up n rows

n$: Cursor moves to end of Nth line

H: Move the cursor to the top row of the screen

M: Move the cursor to the middle line of the screen

L: The cursor moves to the last line of the screen

0: (note is the number 0) cursor moves to the beginning of the current line

$: Cursor moves to the end of the current line



Screen Tumbling Class command

Ctrl+u: First half screen to file

Ctrl+d: Half-screen to the end of the file

CTRL+F: Flip a screen to the end of a file

ctrl+b; Turn one screen to the top of the file

NZ: Rolls line N to the top of the screen and scrolls the current line to the top of the screen when n is not specified.



Insert Text Class command

I: Before the cursor

I: At the beginning of the current

A: After the cursor

A: At the end of the current line

O: A new line below the current line

O: New row above the current line

R: Replace the current character

R: Replaces the current character and its characters until the ESC key is pressed

S: replaces the specified number of characters with the input text starting at the current cursor position

S: Deletes the specified number of rows and replaces them with the input text

NCW or NCW: Modifies a specified number of words

NCC: Modifying a specified number of rows



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



Search and Replace commands:

/pattern: Searches for pattern at the end of the file from the beginning of the cursor

? pattern: Searches for pattern from the beginning of the cursor to the top of the file

N: Repeat the last search command in the same direction

N: Repeats the last search command in the opposite direction

: s/p1/p2/g: Replaces all P1 in the current row with P2

: n1,n2s/p1/p2/g: All P1 in line N1 to N2 are replaced with P2

: g/p1/s//p2/g: Replace all P1 in the file with P2



Option settings

All: List all option settings

Term: Set terminal type

Ignorance: ignoring case in search

List: Display tab stops (CTRL+I) and end-of-line flags ($)

Number: Show line numbers

Report: Displays the number modified by the line-oriented command

Terse: Displays a short warning message

Warn: Displays no write message if the current file is not saved when you go to another file

Nomagic: Allows the use of special characters that are not preceded by "\" In search mode

Nowrapscan: Prohibit VI from the other end when the search reaches the end of the file

MESG: Allow VI to display information that other users write to their terminal using write



Last line mode command

: N1,N2 CO N3: Copy the contents of the N1 line to the N2 row below the N3 line

: n1,n2 m N3: Move the contents of the N1 line to the N2 line below the N3 line

: n1,n2 d: Delete the contents of N1 rows to N2 rows

: w: Save current file

: E filename: Open file filename for editing

: x: Save current file and exit

: Q: Exit VI

: q!: Do not save file and Exit VI

:!command: Execute shell command

: 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 referred to

N1,N2, the input of the entire file content as the command

: R!command: Puts the command's output to the current line.

Introduction to the use of the VI command under Linux

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.