Abstract:

Enter the vi command vi filename: open or create a 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 the NTH line vi + filename: Vi +/pattern filename: Open the file and place the cursor at the beginning of the pattern piece…

Enter the vi command

Vi filename: Opens or creates a file and places the cursor at the beginning of the first line

Vi +n filename: Open the file and place the cursor at the beginning of the NTH line

Vi + filename: Open the file and place 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 pattern

Vi -r filename: Restores filename after a system crash occurred during the last vi editing

vi filename…. Filename: Open multiple files and edit them one by one



Move cursor class command

H: Move the cursor left one character

L: Move the cursor one character to the right

Space: Move the cursor one character to the right

Backspace: Move the cursor one character to the left

K or Ctrl+ P: Move the cursor up a line

J or Ctrl+ N: Move the cursor down a line

Enter: Moves the cursor down a line

W or W: Move the cursor right one word to the beginning of the word

B or B: Move the cursor left one word to the beginning 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

(: Cursor moves to the beginning of the sentence

} : Cursor moves to the beginning of the paragraph

{: cursor moves to end of paragraph

NG: Moves the cursor to the beginning of the NTH line

N + : Moves the cursor down N lines

N – : Moves the cursor up n lines

N $: Moves the cursor to the end of line n

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

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

L: Move the cursor to the last line of the screen

0 :(note that the number is zero) moves the cursor to the first position

$: Move the cursor to the end



Screen tumble command

Ctrl+ U: Scroll to the first half screen of the file

Ctrl+ D: Scroll half screen to the end of the file

Ctrl+ F: Scroll to the end of the file

Ctrl + b; Turn to the first screen of the document

Nz: Rolls the NTH line to the top of the screen. If n is not specified, the current line is rolled to the top of the screen.



Insert text-like commands

I: Before the cursor

I: at the beginning of the current line

A: After the cursor

A: At the end of the current line

O: Open a new line below the current line

O: Create a new line above the current line

R: replaces the current character

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

S: Replaces the specified number of characters with the entered text, starting at the current cursor position

S: Deletes a specified number of lines and replaces them with the entered text

NCW or NCW: Modifies a specified number of words

NCC: Modifies a specified number of rows



The delete command

NDW or NDW: Deletes the n-1 word at and after the cursor

Do: Delete to the beginning of the line

D $: Deletes the file to the end of the line

NDD: Deletes the current line and the n-1 line after it

X or x: Deletes a character, x deletes after the cursor and x deletes before the cursor

Ctrl+ U: Delete the text entered in the input mode



Search and replace commands

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

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

N: Repeat the previous search command in the same direction

N: Repeat the previous search command in the opposite direction

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

: n1,n2s/ P1 /p2/g: replace all p1 in rows N1 to n2 with P2

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



Option is set

All: lists all options

Term: Set the terminal type

Ignorance: Ignore case in search

List: Displays TAB stops (Ctrl+I) and end-of-line flags ($)

Number: displays the line number

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 moving to another file

Nomagic: Allows search mode to use special characters not preceded by “\”

Nowrapscan: forbids vi to search for files at both ends and start from the other end

Mesg: allows VI to display information written by other users to their terminal



Last line mode command

: n1,n2 co n3: copies the contents between lines n1 and n2 to line n3

: n1,n2 m n3: moves the contents between lines n1 and n2 under line n3

: n1,n2 d: Deletes the contents between lines n1 and n2

: w: saves the current file

: e filename: opens a file filename for editing

: x: Saves the current file and exits

: q: Exits the vi

: q! : Do not save the file and exit the vi

:! Command: Runs the shell command

: n1, n2 w! Command: Lines N1 to n2 in the file are used as the command input and executed. If n1 and n2 are not specified, the entire file is used as the command input

: r! Command: Puts the output of the command to the current line



Register operation

“? Nyy: Save the current line and the contents of the next n lines to the register? Middle, middle? Is a letter and n is a number

“? Nyw: Save the current line and its next n words to register? Middle, middle? Is a letter and n is a number

“? Nyl: Saves the current line and its next n characters to a register. Middle, middle? Is a letter and n is a number

“? P: Take out the register? And place it at the cursor position. Here? It can be a letter or a number

NDD: delete the current line and the following n lines of text, and put the deleted content in delete register 1.



The use of the VI

——————————————————————————–



Insert text

┌ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐

│ command │ description │

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ I │ Insert text │ before the current character

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│I │ Insert text │ at the beginning of the line

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│a │ Add text │ after the current character

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│A │ Add text │ at the end of the line

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ │o │ insert empty line │ after the current line

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ │O │ insert the empty line │ before the current line

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│R │ Type text │ in overwrite mode

└ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘

Move the cursor

┌ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐

│ command │ description │

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│j │ down arrow move down one line │

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ │k or │ up arrow move up one line │

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│h or left arrow │ left one character │

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│l │ right arrow move right one character │

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│w │ move the word │ right

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ │W │ right move a word │ delimited by space

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│b │ move the word │ left

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ │B │ left move a word │ delimited by space

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ │0 │ move to the beginning of the line

│Ctrl-F│ Forward page │

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ Ctrl-b │ Turn the page backwards

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ │nG│ to line N │

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ │G │ to the last line │

└ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘

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 │

├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ┤

│ │ at the end of the paragraph

└ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ┘



Delete the text

┌ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐

│ command │ description │

├ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ r│ Replace one character │

├ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│c │ Modify the text until you press Esc

├ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ CW │ Next word │

├ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ CNW │ Modify the next n words │

└ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘

5. Text editing

┌ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐

│ life │ description │

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ YY │ Move a line of text to the default buffer │

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│yn│ Move the next word to the default buffer │

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ynw │ Move the next n words to the default buffer │

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│p │ in the current │ if the default buffer contains a line of text

Insert a blank well after the ││ │ line to default the sound │ in the buffer

││ let’s paste it in this line. If the default buffer contains │

││ ├ ─ paste the words to the right of the cursor. │

├ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│P │ current │ if the default buffer contains a line of text

│ │ ├ ─ insert a blank well in front of the default buffer

││ let’s paste it in this line. If the default buffer contains │

│ │ ├ ─ paste the words to the left of the cursor





└ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘

6. Save and exit

┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐

│ command │ description │

├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│zz│ Save and exit │

├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ │:w filename │ Write file │

├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│:W│ Write file │

├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ │:x│ save (if the current file has been modified) and exit │

├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

│ : q! │ quit │ without saving the file

├ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤

Exit vi │ │ │ : q



VI Common Skills



The VI command is the most commonly used file editing command in the Unix/Linux world, but many people are not used to using it because of its large command set. In fact, you only need to master the basic commands and use them flexibly, and you will find its advantages and gradually like using this method. This article aims to introduce some of VI’s most commonly used commands and advanced application techniques.



1. Basic commands



– 1. The cursor command



K, j, H, L — up, down, left, right cursor movement commands. Although you can use the four cursor keys on the right of the keyboard in Linux, it’s useful to remember these four commands. These four keys are the basic position of the right hand on the keyboard.

NG — Jump command. N is the number of lines. This command immediately causes the cursor to jump to the specified line.

Ctrl+G — Report the number of rows and columns at the cursor position.

W, b – causes the cursor to skip a word forward or backward.

– 2. Edit command

I, a, r — Insert character commands (I = INSERT, a=append, r=replace) before, after, and at the cursor.

Cw, dW — Commands to change (replace)/ delete the word at the cursor (c=change, d=delete).

X, d$, dd — deletes a character, deletes all characters from the cursor to the end of the line, and deletes an entire line.

– 3. To find the command

– / string,? String — the command to find the corresponding string backwards or forwards from where the cursor was.

– 4. Copy copy command

—- yy, p — commands to copy a line to the clipboard or to retrieve the contents of the clipboard.



Two, common problems and application skills



– 1. Read the contents of /etc/passwd in a new file and extract the username part.

—- vi file

—- :r /etc/passwd Read /etc/passwd at the cursor position in the open file file

—- :%s/:.*//g Delete all parts of the /etc/passwd user name from the colon to the end of the line.

—- You can also read the contents of the file after the specified line number. For example, run the :3r /etc/passwd command to read all contents of /etc/passwd from the third line of the new file.

—- We can also delete all empty lines and comment lines starting with # from the file using the following methods.

—- #cat squid.conf.default | grep -v ^$ | grep -v ^#



– 2. After you edit a file, you know that the login user has no write permission on the file and cannot save the file. You need to save the changes to a temporary file.

—- vi file

—- :w/TMP /1 saves all changes, or you can save some changes to a temporary file. For example, just save the contents between lines 20 and 59 as/TMP /1.

—- vi file

—- :20,59w /tmp/1



– 3. Edit a file with VI, but remove large chunks of content.

—- First open the file using the editing command “vi file”, then move the cursor to the line to be deleted and press Ctrl+G to display the line number at the end of the file, and then press Ctrl+G to display the line number at the end of the file.

—- :23,1045d If the line numbers obtained twice are 23 and 1045, delete all the contents in this period. Alternatively, you can mark the beginning and end lines with ma and MB, and then run the :a,bd command to delete them.



– 4. Add strings at the beginning or end of entire lines or lines of a file.

—- vi file

—- :3,$s/^/some string/Insert “some string” at the beginning of the first to last lines of the file.

—- :%s/$/some string/g Add “some string” at the end of each line of the entire file.

—- :%s/string1/string2/g Replace “string1” with “string2” throughout the file.

—- :3,7s/string1/string2/ only replace “string1” to “string2” in lines 3 through 7 in the file.

—- Note: s is substitute, % represents all lines, and g represents global.



– 5. Edit 2 files at the same time, copy text from one file and paste it into the other file.

—- vi file1 file2

—- YY copies the line at the cursor of file 1

—- :n Switch to file 2 (n=next)

—- p Paste the copied line at the cursor of file 2

—- :n Switch to file 1



– 6. Replace the path in the file.

– use the command “: % s # / usr/bin# / bin# g” can all path in the file/usr/bin/bin. You can also use the :%s /usr/bin/bin/g command, where “” is an escape character, indicating that the following”/” character is a meaningful character, not a delimiter.



This article from MoShuiQian flow blog garden, the original link: http://www.cnblogs.com/zhoug2020/p/4022148.html, if you want to reprint, please contact our author


Copyright Notice: The content of this article is contributed by Internet users, copyright belongs to the author, the community does not have the ownership, also do not assume the relevant legal responsibility. If you find any content suspected of plagiarism in our community, you are welcome to send an email to [email protected] to report and provide relevant evidence. Once verified, our community will immediately delete the content suspected of infringement.

The original link