Linux vi Editor Use tutorial detailed

Source: Internet
Author: User
Tags save file

Linux under the VI Editor is the standard common editor, basically can be qualified under the various conditions of Linux. Since Linux operates on a command-line basis, we need to know how to use the VI editor under Linux.

VI Editor's concept

VI Editor is divided into three kinds of States, namely:
1. Command mode

Controls the movement of the screen cursor, the deletion of characters, words, or rows, the movement of a section and into the insert mode, or the last line mode.

2. Insert mode

Only in insert mode, you can do text input, press the "esc" key to return to the command line mode.

3. End Line Mode

To save or exit VI, you can also set up an editing environment, such as finding a string, listing line numbers ... Wait

When you need to open a document using the VI editor, you only need to open the document using the $ VI test.txt command. After the document is opened, we enter the command mode by default, in which case we can move the cursor through the top and bottom of the keyboard. However, it is important to note that the document content cannot be modified in command mode. The following figure:

If we need to insert text or delete text inside the document, we need to go into insert mode. The way into insert mode is to press the I or a key of the keyboard in command mode. The –insert– identification is displayed in the view. You can then begin editing the text. The following figure:

The bottom line mode is generally the VI editor to operate and set up, more commonly used is the document save and exit. If you need to enter a bottom line mode, you only need to enter a ":" Window below the colon appears. If you enter q! after the colon Then enter, you can exit the VI editor. The following figure:

The above is a simple vi operation method Description, in order to better Operation VI Editor we need to learn more VI instructions and methods. Therefore, the following is attached to the VI operation manual for easy use and search for the required commands.

Order to enter VI

VI FileName: Open or create a new file and place the cursor at the beginning of the first line
VI +n FileName: Open the file and place the cursor at the beginning of nth
VI + FileName: Open the file and place the cursor at the end of the line
VI +/pattern FileName: Open the file and place the cursor in the first matching string
Vi-r filename: A system crash occurred during the last edit with VI, filename restored
VI filename....filename: Open multiple files, edit them sequentially

Move Cursor Class command

H: Move the cursor one character to the left
L: The cursor moves one character to the right
Space: The cursor moves one character to the right
Backspace: Move the cursor one character to the left
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 top of the word
B or B: Move the cursor left one word to the top of the word
E or E: Move the cursor right one word j to 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: The cursor moves to the beginning of nth
n+: Move the cursor down n line
N: Move n line up on cursor
n$: The cursor moves to the end of the 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
$: The cursor moves to the end of the current line

Screen Tumbling Class command

Ctrl+u: Turn to file first half screen
Ctrl+d: Turn to file tail half screen
CTRL+F: Flip a screen to the end of the file
Ctrl+b to the first screen of a file
NZ: Rolls the nth row to the top of the screen and rolls the current row to the top of the screen without specifying N.

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: Open a new row below the current line
O: Open a new line above the current line
R: Replace the current character
R: Replaces the current character and its subsequent 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 text entered
NCW or NCW: Modifying a specified number of words
NCC: Modify specified number of rows

Delete command

NDW or NDW: Deletes the beginning of the cursor and the n-1 word after it
Do: Delete to the beginning of the line
d$: Delete to end of line
NDD: Deletes the current row and its n-1 line
X or x: Deletes a character, x deletes the cursor, and x deletes the cursor before the
Ctrl+u: Deletes the text entered in the input mode

Search and Replace commands

/pattern: Searches the end of the file for pattern at the beginning of the cursor
Pattern: Search for pattern from the beginning of the cursor to the first file
N: Repeat the last search command in the same direction
N: Repeat 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 the N1 to N2 line 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 labels ($)
Number: Show line numbers
A: Shows the number of changes made by line-oriented commands
Terse: Displays a brief warning message
Warn: show no write information when you go to another file without saving the current file
Nomagic: Allow special characters in search mode with no "" in front
Nowrapscan: Prohibit VI when the search arrives at both ends of the file, and start at the other end
MESG: Allow VI to display other users write to their own terminal information

Last line command

: N1,N2 CO N3: Copies the contents of the N1 line to the N2 line to the first N3 line
: n1,n2 m N3: Moves the contents of the N1 line to the N2 line to the lower N3 line
: n1,n2 D: Remove content from N1 line to N2 line
: w: Save the current file
: E filename: Open file filename for editing
: x: Save the 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
Fixed n1,n2, which means that the entire file contents are entered as command
: R!command: Places the output of command commands on the current line.

The original text comes from: Taste life

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.