Linux under the VI command Daquan

Source: Internet
Author: User
Tags first string save file

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 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 are entered as command and executed, and if N1,N2 is not specified, the entire file content is entered as the command
: R!command: The output of command commands is placed in 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.

Use of VI
--------------------------------------------------------------------------------

First, insert text
┌──┬────────────┐
│ Command │ Description │
├──┼────────────┤
│i│ inserting text match either the current word │
├──┼────────────┤
│i│ inserting text at the beginning of the line │
├──┼────────────┤
│a│ add text after the current character │
├──┼────────────┤
│a│ add text at the end of a row │
├──┼────────────┤
│o│ insert a blank line after the current line │
├──┼────────────┤
│o│ insert a blank line before the current line │
├──┼────────────┤
│r│ entering text in rewrite mode │
└──┴────────────┘
Second, move the cursor
┌─────┬───────────┐
│ Command │ Description │
├─────┼───────────┤
│j or DOWN ARROW │ Move down one line │
├─────┼───────────┤
│k or up ARROW │ move up one line │
├─────┼───────────┤
│h or LEFT arrow │ move left one character │
├─────┼───────────┤
│l or RIGHT arrow │ move right one character │
├─────┼───────────┤
│w│ Right One word │
├─────┼───────────┤
│w│ Right moves a space-delimited word │
├─────┼───────────┤
│b│ left one word │
├─────┼───────────┤
│b│ left a space-delimited word │
├─────┼───────────┤
│0│ move to the beginning of the line │
│ctrl-f│ forward Page │
├─────┼───────────┤
│ctrl-b│ back Page │
├─────┼───────────┤
│ng│ to Nth line │
├─────┼───────────┤
│g│ to the last line │
└─────┴───────────┘
Third, replace the text
┌─────┬──────┐
│ Command │ Description │
├─────┼──────┤
│$│ to the end of the line │
├─────┼──────┤
│ (│ to the beginning of the sentence │
├─────┼──────┤
│) │ to the end of the sentence │
├─────┼──────┤
│{│ to the beginning of the paragraph │
├─────┼──────┤
│}│ to the end of the paragraph │
└─────┴──────┘

Iv. deletion of text
┌───┬───────────┐
│ Command │ Description │
├───┼───────────┤
│r│ replacing a character │
├───┼───────────┤
│c│ Modify text until ESC is pressed
├───┼───────────┤
│cw│ Modify Next Word │
├───┼───────────┤
│cnw│ Modify the next n words │
└───┴───────────┘
Five, text editing
┌──┬──────────────────────┐
│ Life Temple │ Description │
├──┼──────────────────────┤
│yy│ move a line of text into the default buffer │
├──┼──────────────────────┤
│yn│ move the next word into the default buffer │
├──┼──────────────────────┤
│ynw│ move the following n words into the default buffer. │
├──┼──────────────────────┤
│p│ If the default buffer contains a line of text, the current │
Insert a blank line after the ││ line wells the sound in the default buffer │
││ paste into this line, if the default buffer contains │
││ multiple words, paste them to the right of the cursor. │
├──┼──────────────────────┤
│p│ If the default buffer contains a line of text, it is current │
││ insert a blank line in front of the line wells the default buffer in the inner │
││ paste into this line, if the default buffer contains │
││ multiple words, paste these words to the left of the cursor


└──┴──────────────────────┘
Vi. Saving and exiting
┌───────────┬───────────────┐
│ Command │ Description │
├───────────┼───────────────┤
│zz│ Save and Exit │
├───────────┼───────────────┤
│:w filename│ Write File │
├───────────┼───────────────┤
│:w│ Writing Files │
├───────────┼───────────────┤
│:x│ Save (if the current file has been modified) and exit │
├───────────┼───────────────┤
│:q! │ do not save files, direct exit │
├───────────┼───────────────┤
│:q│ Exit vi│

VI Common Skills

The VI command can be said to be the most commonly used in the Unix/linux world to edit the command of the file, but because of its many command set, many people are not accustomed to use it, in fact, you only need to master the basic commands, and then flexible use, you will find its advantages, and will gradually like to use this method. This article is intended to introduce some of the most common commands and advanced application techniques for VI.

I. Introduction of BASIC commands

----1. Cursor command

K, J, H, l--up, down, left, right cursor move command. Although you can use the 4 cursor keys on the right side of the keyboard in Linux, it is useful to remember these 4 commands. These 4 keys are the basic positions that the right hand places on the keyboard.
ng--jump command. n is the number of rows, and the command immediately causes the cursor to jump to the specified line.
ctrl+g--the number of rows and columns in the cursor location report.
W, b--causes the cursor to skip forward or backward through a word.
----2. Edit command
I, a, r--insert character commands (I=insert, A=append, R=replace) before, after, and where the cursor is located.
CW, dw--Change (displace)/delete the command (C=change, d=delete) of the word where the cursor is located.
X, d$, dd--delete one character, delete all characters at the end of the line, and delete the entire line.
----3. Find command
----/string,? string--the command to find the appropriate string from the cursor location, backward or forward.
----4. Copy Copy command
----yy, p--copies a row to the Clipboard or takes out the contents of the Clipboard command.

Second, common problems and application skills

----1. In a new file, read the contents of the/etc/passwd and remove the user name section.
----VI File
----: R/etc/passwd reads the cursor in the file where it is opened/etc/passwd
----:%s/:.*//g Delete all parts of the/etc/passwd that follow the user name from the colon until the end of the line.
----You can also read the contents of the file after the specified line number, for example, using the command ": 3r/etc/passwd" to start reading all the contents of/etc/passwd from the 3rd line of the new file.
----We can also use the following method to delete all empty lines in the file and comment lines starting with #.
----#cat Squid.conf.default | Grep-v ^$ | Grep-v ^#

----2. When you open a file to edit it is not known that the user logged in to the file does not have permission to write, you need to save the changes to the temporary file.
----VI File
----: W/TMP/1 Save any changes you have made, or you can save some of these changes to a temporary file, such as simply saving the contents between lines 20th to 59th into a file/tmp/1, we can type the following command.
----VI File
----: 20,59W/TMP/1

----3. Edit a file with VI, but you need to delete the contents of the large segment.
----First Use the edit command "VI file" to open the file, and then move the cursor to the line to be deleted, press Ctrl+g to display the line number, and then at the end of the ctrl+g, display the end of the file line number.
----: 23,1045d assumes that 2 times the number of rows is 23 and 1045, the content of this period is deleted, you can also use the MA, MB command in the start and end lines to be deleted, and then delete with the ": A,BD" command.

----4. Add strings at the beginning or end of the line or lines of the entire file.
----VI File
----: 3, $s/^/some string/insert "some string" at the beginning of the first line of the file to the end of the last line.
----:%s/$/some string/g Add "some string" at the end of each line of the file.
----:%s/string1/string2/g Replace "string1" into "string2" throughout the file.
----: 3,7s/string1/string2/replaces only "string1" in the 3rd line of the file to the 7th line as "string2".
----Note: where S is substitute,% for all rows, G represents global.

----5. Edit 2 files Simultaneously, copy the text from one file and paste it into another file.
----VI file1 file2
----YY in the cursor at the file 1 copy the row
----: n Switch to File 2 (n=next)
----p Pastes the copied row at the cursor where the file 2 is located
----: n Switch back to file 1

----6. Replace the path in the file.
----Use the command ":%s#/usr/bin#/bin#g" to change all the paths in the file/usr/bin to/bin. You can also use the command ":%s//usr/bin//bin/g" Implementation, where "" is an escape character, indicating that subsequent "/" characters are characters that have practical meaning, not delimiters.

Linux under the VI command Daquan

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.