vi instruction

Enter the vi command 
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 the nth line 
vi + filename: open the file and place the cursor at the beginning The first line of the last line 
vi +/pattern filename: open the file and place the cursor at the first string matching pattern 
vi -r filename: the system crashed while editing with vi last time, restore filename 
vi filename... .filename : Open multiple files and edit them in sequence 

Move cursor commands
h : Move the cursor one character to the left 
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: The cursor Move 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 prefix 
b or B: move the cursor left one word to the prefix 
e or E: move the cursor right One word to the end of the word 
): move the cursor to the end of the sentence 
( : move the cursor to the beginning of the sentence 
}: move the cursor to the beginning of the paragraph 
{: move the cursor to the end of the paragraph 
nG: move the cursor to the beginning of the nth line 
n+: move the cursor down n lines 
n -: move the cursor up n lines 
n$: move the cursor to the end of the nth line 
H: move the cursor to the top line of the screen 
M: move the cursor to the middle line of the screen 
L: move the cursor to the last line of the screen 
0: (note the number zero) move the cursor to the beginning of the current line 
$: Move the cursor to the end 

of the current line Screen scrolling commands 
Ctrl+u: turn half a screen to the 
beginning of the file Ctrl+d: turn half a screen to the end of the file 
Ctrl+f: turn one screen to the end of the file 
Ctrl+b; turn one screen to the  beginning of the file
nz: Scroll the nth line to the top of the screen, or scroll the current line to the top of the screen when n is not specified. 

Insert text command 
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: open a new line above the current line 
r: replace the current character 
R: Replace the current character and the following characters until the ESC key is pressed 
s: Starting from the current cursor position, replace the specified number of characters 
with the entered 
text nCW: modify the specified number of words 
nCC: modify the specified number of lines 

delete command 
ndw or ndW: delete n-1 words starting at the cursor and after it 
do: delete to the beginning of the line 
d$: delete to the end of the line 
ndd: delete the current line and its next n-1 lines 
x or X: delete a character, x deletes the one after the cursor, and X deletes the text before the cursor 
Ctrl+u: deletes the text entered in the input mode 

Search and replace command 
/pattern : search for pattern from the beginning of the cursor to the end of the file 
?pattern: search for the pattern from the beginning of the cursor to the beginning of the 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 : Replace all p1 in the current line with p2 
: n1,n2s/p1/p2/g: Replace all p1 in lines n1 to n2 with p2 
: g/p1/s//p2/g: Replace the file in all p1 replace 

option settings  with p2
all: list all options settings 
term: set terminal type 
ignoreance: ignore case in search 
list: show tab stops (Ctrl+I) and end-of-line signs ($) 
number: show line number 
report: display the number of modifications made by line-oriented commands 
terse: display a brief warning message 
warn: display NO write message if the current file is not saved when moving to another file 
nomagic: allow in search mode, use the previous Special character nowrapscan without "\" 
: Prevent vi from searching from the other end of the file when it reaches both ends of the file 
mesg: Allow vi to display the information written by other users to their terminal with write 

Last line command 
: n1,n2 co n3: Copy the content between lines n1 to n2 to the n3th line 
: n1,n2 m n3: Move the content between lines n1 to n2 to the next line n3 
: n1,n2 d: delete the content between lines n1 and n2 
: w: save the current file 
: e filename: open the file filename for editing 
: x: save the current file and exit 
: q: exit vi 
: q!: do not save the file and exit vi 
: !command: execute the shell command command 
: n1, n2 w!command: use the contents of lines n1 to n2 in the file as command If you do not specify n1, n2, it means that the entire file content is used as the input of the command 
: r!command: put the output result of the command command into the current line 

register operation 
"?nyy: put the current line and its lower The contents of the n lines are saved to the register ?, where ? is a letter and n is a number 
"?nyw: save the current line and its next n words to the register? , where ? is a letter and n is a number 
"?nyl: save the current line and its next n characters to the register?, where ? 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 its next n lines of text, and put the deleted content in the delete register No. 1.

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

1. Insert text 
┌──┬────────── ───┐  │Command│Description│ ├──┼──────────────┤ │i │Insert text before 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 a blank line after the current line │  ├──┼──── ────────┤  │O │Insert a blank line before the current line │  ├──┼────────────┤  │R │Enter text in rewriting style │  └──┴ ────────────┘  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 │ left one character │ 
├──────┼────────────┤ 
│l or right arrow │ right one character │ 
├──────┼────────────┤ 
│w │ move one word right │ 
├─────┼────────────┤ 
│W │right Shift a word separated by a space │ 
├─────┼────────────┤ 
│b │ Shift a word to the left │ 
├──────┼──────── ───┤ 
│B │ left a space-separated word │ 
├─────┼────────────┤ 
│0 │ move to the beginning of the line │ 
│Ctrl-F │forward Page turn │ 
├──────┼────────────┤ 
│Ctrl-B │Page backward │ 
├──────┼──────────── ┤ 
│nG │ to the nth line │ 
├─────┼──────────────┤ 
│G │ to the last line │ 
└──────┴────────────┘ 
3. Replacement text  ┌──────┬──────┐
│Command│Description│ 
├──────┼─ 
─────┤ 
│$ │ to end of line │ 
├──────┼──────┤ 
│( │ to start of sentence │ 
├─────┼──────┤ 
│ ) │ to the end of the sentence │ 
├─────┼──────┤ 
│{ │ to the beginning of the paragraph │ 
├─────┼──────┤ 
│} │ to the end of the paragraph │ 
└──────┴──────┘ 

Fourth, delete text 
┌───┬────────────┐ 
│Command│Description│ 
├───┼──── ────────┤ │r 
│Replace a character │ 
├───┼────────────┤ 
│c │Modify text until Esc is pressed │ 
├───┼── ──────────┤ 
│cw │Modify the next word │ 
├───┼────────────┤ 
│cnw │Modify the next n words │ 
└─── ┴──────────────┘ 
5. Text  Editing 
┌──┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────  ──────────────┤  │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 │ if the default buffer If it contains a line of text,  insert a blank line after the current │ │ │ line and  paste the audio │ │ │ content in the default buffer into this line; if the default buffer contains  multiple words │ │ │, put These words are pasted to the right of the cursor. │  ├──┼────────────────────────┤ │P  │If the default buffer contains a line of text, it will be  inserted before the current │ │ │ line An empty line will paste the content │ │ │ in the default buffer  into this line; if the default buffer contains │  │ │ multiple words, paste these words to the left of the cursor       │ 



















└──┴────────────────────────┘ 
Six, save and exit 
┌────────────┬────── ──────────┐ 
│Command│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 the file, exit directly │ 
├────────────┼──────────────────┤ 
│:q │Exit vi │ 

Common VI skills

The VI command can be said to be the most commonly used command for editing files in the Unix/Linux world, but because of its large set of commands, many people are not used to using it. In fact, you only need to master the basic commands, and then use them flexibly. Discover its advantages and will gradually enjoy using this method. The purpose of this article is to introduce some of the most common commands and advanced application techniques for VIs. 

1. Introduction of basic commands 

---- 1. Cursor commands 

k, j, h, l - up, down, left, and right cursor movement commands. Although you can use the 4 cursor keys on the right side of the keyboard in Linux, it is very useful to remember these 4 commands. These 4 keys are exactly where the right hand is placed on the keyboard. 
nG - Jump command. n is the number of lines, this command immediately jumps the cursor to the specified line. 
Ctrl+G - Reports the number of rows and columns at the cursor position. 
w, b - makes the cursor skip forward or backward a word. 
---- 2. Edit Commands 
i, a, r - Insert character commands before, after and at the cursor (i=insert, a=append, r=replace). 
cw, dw - command to change (replace)/delete the word where the cursor is (c=change, d=delete). 
x, d$, dd - commands to delete a character, delete all characters from the cursor to the end of the line, and delete the entire line. 
---- 3. Search command 
---- /string, ?string - command to find the corresponding string backward or forward from the cursor position. 
---- 4. Copy Copy command 
---- yy, p - command to copy a line to the clipboard or remove the contents of the clipboard. 

2. Frequently Asked Questions 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 opened file 
---- :%s/:.*//g Delete /etc/passwd All parts of the user name starting with a colon and ending at the end of the line. 
---- You can also read the contents of the file after the specified line number, for example, use the command ":3r /etc/passwd" to read all the contents of /etc/passwd starting from the 3rd line of the new file. 
---- We can also use the following method to delete all blank lines and comment lines starting with # in the file. 
---- #cat squid.conf.default | grep -v ^$ | grep -v ^# 

---- 2. After opening a file for editing, it is known that the logged-in user does not have permission to write to the file and cannot save the file. The modifications need to be stored in a temporary file. 
---- vi file 
---- :w /tmp/1 Save all the modifications made, or you can save a part of the modifications to a temporary file, for example, only save the content between lines 20 to 59 as file /tmp/1, we can type the following command. 
---- vi file 
---- :20,59w /tmp/1 

---- 3.Edit a file with VI, but need to delete large sections of content. 
---- First use the editing command "vi file" to open the file, then move the cursor to the line to be deleted and 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 Assuming that the line numbers obtained twice are 23 and 1045, delete all the content during this period, or you can mark the start line and end line to be deleted with the ma and mb commands, and then use ":a,bd" command to delete. 

---- 4. Add some strings to the beginning or end of each line or lines of the entire file. 
---- vi file 
---- :3,$s/^/some string / Insert "some string" at the beginning of the first line to the last line of the file. 
---- :%s/$/some string/g Add "some string" to the end of each line in the entire file. 
---- :%s/string1/string2/g Replace "string1" with "string2" throughout the file. 
---- :3,7s/string1/string2/ Only replace "string1" with "string2" in lines 3 to 7 in the file. 
---- Note: where s is a substitute, % means all lines, and g means global. 

---- 5. Edit 2 files at the same time, copy the text from one file and paste it into the other. 
---- vi file1 file2 
---- yy Copy the line at the cursor of file 1 
---- :n Switch to file 2 (n=next) 
---- p Paste the line at the cursor of file 2 Copied line 
---- :n Switch back to file 1 

---- 6. Replace the path in the file. 
---- Use the command ":%s#/usr/bin#/bin#g" to replace all paths /usr/bin in the file with /bin. It can also be implemented using the command ":%s//usr/bin//bin/g", where "" is an escape character, indicating that the following "/" character is a character with actual meaning, not a separator.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325765636&siteId=291194637