Linux under the VI command detailed

Source: Internet
Author: User
Tags save file

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 in sequence


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 to the end of the word
): 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: To enter and execute the contents of N1 rows to N2 rows in a file As command and, if you do not specify N1,N2, to input the entire file contents as command
: R!command: Places the output of command commands on the current line


Register operation
"? Nyy: Save the contents of the current row and its lower n rows to registers? , where? is a letter, n is a number
"? NYW: Save the current line and its next n words to a register? , where? is a letter, n is a number
"? Nyl: Save the current line and its next n characters to a register? , where? is a letter, n is a number
"? P: Take out registers? and places 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 item in the 1th Delete register.


VI's use
--------------------------------------------------------------------------------


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


Four, delete text
┌───┬───────────┐
│ Command │ Description │
├───┼───────────┤
│r│ Replace a character │
├───┼── ─────────┤
│c│ Modify text until you press ESC health │
├───┼───────────┤
│cw│ Modify the next word │
├───┼───────────┤
│cnw│ Modify The next n words │
└───┴───────────┘
Five, text editing
┌──┬──────────────────────┐
│ Life Temple │ Description │
├──┼          ──────────────────────┤
│yy│ moves one line of text to the default buffer │
├──┼──────────────────────┤
│yn│ moves the next word to the default buffer │
├──┼──────────────────────┤
│ynw│ moves the following n words to the default buffer │
├──┼──────────────────────┤
│p│ if the default slow     If the flush area contains one line of text, insert a blank line after the current │
││ line Maita The sound │ in the default buffer
││ to this line; If the default buffer contains │
││ multiple words, paste the words to the right of the cursor. │
├──┼──────────────────────┤
│p│ If the default buffer contains one line of text, the current │
││ line inserts a blank line before the inside │ Maita default buffer
││ paste into this line If the default buffer contains │
││ multiple words, paste the words to the left of the cursor



└──┴──────────────────────┘
Six, save exit
┌───────────┬───────────────┐
│ Order │ Description │
├───────────┼───────────────┤
│zz│ Save and Exit │
├───────────┼───────────────┤
│:w filename│ write to file │
├───────────┼───────────────┤
│:w│ Write to File │
├───────────┼───────────────┤
│: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 editing document in the Unix/linux world, however, because of its many commands, many people are not accustomed to 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 aims to introduce some of the most common commands and advanced application techniques of VI.


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 to the right of the keyboard in Linux, it is useful to remember these 4 commands. These 4 keys are the basic position where the right hand is placed on the keyboard.
ng--jump command. n is the number of rows, which immediately jumps the cursor to the specified line.
ctrl+g--the number of rows and columns of the cursor at the location of the report.
W, b--the cursor to skip a word forward or backward.
----2. Edit command
I, a, r--inserts character commands (I=insert, A=append, R=replace) before, after, and at the cursor.
CW, dw--Change (permutation)/delete the command (C=change, d=delete) of the word at the cursor.
X, d$, dd--deletes a character, deletes all characters at the end of the line at the cursor, and deletes the entire line's commands.
----3. Find command
----/string,? string--the command to look backward or forward from the cursor at the corresponding string.
----4. Copy Copy command
----yy, p--the command to copy a row to the Clipboard or remove the contents of the Clipboard.


Second, common problems and application skills


----1. Read the contents of the/etc/passwd in a new file and remove the user name section.
----VI File
----: R/etc/passwd read/etc/passwd at cursor location in open file files
----:%s/:.*//g deletes all parts of the/etc/passwd after the user name from the colon until the end of the line.
----You can also read the contents of a file after a specified line number, such as using the command ": 3R/ETC/PASSWD" to begin 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 the blank lines in the file and the comment lines starting with #.
----#cat Squid.conf.default | Grep-v ^$ | Grep-v ^#


----2. When you open a file editor, you know that the logged-on user does not have permission to write to the file, and that 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 one part of the modification to a temporary file, such as simply saving the contents of line 20th to 59th to a file/tmp/1, we can type the following command.
----VI File
----: 20,59W/TMP/1


----3. Use VI to edit a file, but you need to delete the contents of a large paragraph.
----First open the file with the edit command "VI file", then move the cursor to the line where you want to delete, press Ctrl+g to display the line number, and then press ctrl+g at the end to display the line number at the end of the file.
----: 23,1045d assumes that the line number 23 and 1045 is obtained 2 times, the contents of this period are removed, or the MA, MB command mark is used in the start and end rows to be deleted, and then the ": A,BD" command is used to remove it.


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


----5. Edit 2 files at the same time, copy the text from one file and paste it into another file.
----VI file1 file2
----yy is a good copy of the cursor at file 1
----: n Switch to File 2 (n=next)
----p Pastes the copied row at the cursor of file 2
----: n Switch back to file 1


----6. Replaces the path in the file.
----Use the command ":%s#/usr/bin#/bin#g" to/usr/bin all paths in the file to/bin. You can also use the command ":%s//usr/bin//bin/g" Implementation, where "" is an escape character, indicating that the subsequent "/" character is a meaningful character, not a separator.

Related Article

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.