Command Execution format
(command) (number) (text object)
(number) (command) (text object) 
Copy the code
Basic operation
ZZ or :wq Save and exit :e! Restore to the contents of the last stored file :w pathname/filename Write to other files :rf pathname/filename Delete other files :! Ls pathName To view the usage of other directories, run sh -> CTRL-dexitCreate the corresponding shell to execute its task and return to Ctrl-Z ->fgPause vi, execute the corresponding task, and returnCopy the code
Move the character cursor
H/L Moves one character left/right 4L Moves four characters rightCopy the code
Move the row cursor
Moved to the beginning of a line of $0 moved to the end of each line ^ moved to the current line the first non whitespace 10 | moved to the current line column 10 j/k line on the next line 2 j moving up at the end of the line 2 G mobile to file 1 G mobile to file the first line of the CTRK - G at the bottom of the screen display information, include a line number, The total number of lines in the file, the current location as a percentage of the full textCopy the code
Move by text block
W moves one word, including symbols and punctuation w moves one word, not including symbols and punctuation 2W moves two words, b moves one word backward, including symbols and punctuation B moves one word backward, that's bad, e moves the symbol and punctuation to the end of the word, E moves to the end of the word, 3) Move up 3 sentences {move up 3 sentences {move to the beginning of the current paragraph, A paragraph is the text under a blank line} moved to the beginning of the next paragraph [[moved to the beginning of the current section]] moved to the beginning of the next sectionCopy the code
Simple file editing
I insert a add C change D delete p paste d -> p can move the line y copy cw means change the whole word x delete a character r rewrite rewrite a characterCopy the code
To add text
Add a after the cursor add the end of line A add I before the cursor add the beginning of line I add o cursor position next line open new line add o cursor position above line open new line addCopy the code
To modify the text, you need to inform how much text to modify, can be used in conjunction with the cursor movement command
The c2b cursor moves two characters c$or c to the end of the line c0 to the beginning of the line cc changes the entire line of textCopy the code
Replace text
R replaces one character 2r replaces two characters at the same time r replaces the whole line, gradually overwriting s replaces one character, deletes the character first and goes into insert mode, so this can be used to replace one character with more than one s replaces the whole line, deletes the whole line first and goes into insert modeCopy the code
Changing case
~ Changes one character to uppercase or lowercase. 3~ Changes three characters to uppercase or lowercaseCopy the code
To delete text, you need to tell how much text to delete, which can be used with the cursor movement command
Dw deletes the cursor to the end of the word, contains the blank space at the end of the word de deletes the cursor to the end of the word itself, does not contain the blank space behind the tail de deletes the cursor to the end of the word, Include punctuation db forward delete a character d0 remove the cursor to the beginning of the line d$or d remove the cursor to the end of the line dd delete a whole line of characters 2dd remove two lines of characters x x-ing out printer erase meaning, Delete one character 2x delete two characters X delete one character before cursor 2x delete two characters before cursorCopy the code
Mistaken delete recovery
U Undo the previous command 3p puts the third buffer on the next line, reserving nine buffers by default Ctrl-r redo the undo operationCopy the code
Move text by deleting it before placing it
P puts the buffer text after the cursor, a single line on the same line, and a multi-line buffer on another lineCopy the code
To copy text, you need to tell how much text to copy. You can use this command with the cursor movement command
Yw copies the cursor to the character y0 at the end of the word to the character Y $at the beginning of the line yy or y at the end of the line copies the entire lineCopy the code
If the command is repeated, each command is stored in a temporary buffer until the next command is issued
. Repeat the previous commandCopy the code
Merge rows
J merges two rows and merges the current row with the next row. 3J merges three rowsCopy the code
Edit command
Text object To change the delete copy
A single character r x yl
Five characters 5s 5x y5l
A word cw dw yw
Two words, no punctuation c2W d2W y2W
Back up three words c2b d2b y2b
A whole line cc dd yy
To the end of the line Or c $c D $or d y$
To the beginning of a line c0 d0 y0
Cursor movement command
Move command ←↓↑→ HJKL First character of the next line + first character of the previous line - end of a word E or e W or W B or B to the end of a line $to the beginning of a line 0 Place the text P or P into the buffer to save the editing result and leave ZZ Do not save the edits and leave :q! Cursor position Insert text I at the beginning of a line Insert text I at the beginning of a line Append text A At the end of a line Append text A Next line of the cursor Open a new line O Previous line of the cursor Open a new line O Delete a line and replace text S New text overwrites existing text R Merge current line with next line J Repeat previous action. Undo the previous action uCopy the code

Scroll to the screen
Z-enter Moves the current line to the top of the screen and scrolls the screen Z. Move the current line to the center of the screen and scrolls the screen Z - Move the current CTRL-fForward, scroll up a screen with Ctrl-B Backward, scroll down a screen with Ctrl-U Up, scroll up half a screen with CTRL-dScroll down half screen 100z scroll line 100 to the top of the screenCopy the code
The cursor moves across the screen
H high goes to the top of the screen, M middle, L low goes to the middle of the screen, L low goes to the bottom of the screen, 10H goes to the top of the screen, 10L goes to the bottom of the screen, 10L goes to the bottom of the screen, 10L goes to the bottom of the screen, 10L goes to the bottom of the screenCopy the code
Move according to the search pattern
/ Downmatch search? Match up search n repeat search in the same direction n repeat search in the opposite directionCopy the code
Searches the current row
Fx moves to the place where the next X character appears on the line 2FX searches the place where the next X character appears on the line fx moves to the place where the last X character appears on the line tx moves to the place where the last X character appears on the line The place of the character after the character; Repeat the previous search command in the same direction, and repeat the next search command in the opposite direction' 'Return to the beginning of the previous line containing the token return to the previous token or contextCopy the code
Open the file with a thousand flowers blooming
Vi +n file Open the file in the NTH line vi + file Open the file in the last line vi +/pattern file Open the file in the first place where pattern appears vi -r file Read-only mode Display the file vi -r file Restore edit bufferCopy the code
Buffer usage
# vi stores the last nine deletions in numbered buffers 1-9, with 1 indicating the last deletion
# vi will place the copied text in alphabetical buffers, a-z buffers

"1P restore last deleted content, insert after cursor"1pu. After placing the buffer contents one by one after the current cursor, u deletes the text. Repeat and rotate until you find the text you want"Dyy copies the current line into buffer D,"Plus the buffer name can be used to name the buffer"DP places the contents of buffer D before the cursorCopy the code
Mark a place
Mx marks the current location as x'x single quote, move the cursor to the first line of the X mark ''Two single quotes, back to the beginning of the line of the previous mark or context' x backquotes, move the cursor to the character marked with X ' 'two backquotes, back to the exact position of the previous mark or contextCopy the code
Vi buffer and mark command
The command meaning
+n file Open file at line NTH
+ file Open file on the last line
+ /pattern file Open file at the first place where pattern appears
-c command file Execute the command after opening the file, usually in line number or search mode, eg: vi-c /gitlab practice.txt
-R Open the file in read-only mode, same as the view command
-r Restore files after a crash
1-9 The last nine deletions, the last one. – The first one
a-z You can use the named buffer, capital letters to indicate the existing contents of the buffer after append
“b command Execute the command with buffer B
mx Mark the current location as X
‘x Move the cursor to the first character on the line marking X
~x Move the cursor to the character marked with x
‘ ‘ Returns to the beginning of the line of the previous tag or context
` ` Returns to the exact location of the previous tag or context
The ex command
: 1P Print line 1 :1 Print line 1 :1,3 Print line 1-3 :1s/GitLab/ GitLab Replace GitLab in the first line with GitLab :s/GitLab/ GitLab Replace GitLab in the current line with GitLab :vi ex mode to vi mode Q Vi mode to EX mode :1,3d delete row 1 to 3 :1m2 move: m Move line 1 after line 2 :1co2 copy can be abbreviated as co, agree with t, copy line 1 after line 2:setNu number can be abbreviated as nu. Set the line number to be displayed:setNonu no number can be abbreviated as nobu. Disable line number 1,10Temporarily display the line numbers of lines 1-10:= Lists the total number of lines in the file :.= Lists the line number of the current line :/pattern/= Lists the line number of the first occurrence of patter :. Represents the current line :$represents the last line of the file :% Represents each line of the file :.,+20Display the current line and the line numbers directly down 20 lines
Copy the code
Ex search mode
:/pattern/d Deletes the next line containing pattern :.,/pattern/d deletes the current line and the first line containing pattern :100, +5 p prints the contents of the current line between +5 and 100 :100; +5 p Print lines between 100 and 105. When using a semicolon, the address of the first line will be used as the current cursor position :/pattern; +10 p Displays the next ten lines containing pattern :g/pattern Looks for the last occurrence of pattern in the file, g stands for global, You can make the search mode display and include all lines of the pattern :g/pattern/p Find and display all lines of the pattern :g! / pattern/nu search and displays the line does not contain the pattern, and show the corresponding line number: 1, 10 g/pattern for line 1-10 lines contains the pattern: 1 d | s/This/This delete the first row, And replace This with This in the current line (original second line)Copy the code
Ex saved in leave file
:w writes the contents of the buffer to the file, but does not leave :q leaves the editor :wq writes and leaves, even if the file has not been modified :x writes to the file and leaves, and only writes to :! W newfile writes the contents of the buffer to a newfile. W newfile writes the contents of the buffer to a newfile The original file will not change :1,10w newfile writes 1-10 lines of the buffer to the newfile :1,10w >> newfile appends 1-10 lines of the buffer to the newfile :r newflereadShort r, read out the contents of the newfile and insert it on the next line where the cursor is :2r newfile insert the contents of the newfile after the second line :0r newfile insert the contents of the newfile at the beginning of the fileCopy the code
Vi Multi-file editing
:n To go to the next file :first to go to the first file :last to go to the last file :e filename Starts a file. If it exists, it opens it. If it does not exist, it creates a new fileReturn to previous file, need to write buffer contents to file:w %. New Writes to the current file, % indicates the name of the current file CTRL -^ has the same effect as :eSwitch to the previous file

"f6yy -> :w -> :e new.txt -> "Fp ===> Copy the cursor and the next 6 lines to buffer F -> write buffer to a file -> switch to new. TXT -> copy the contents of buffer F behind the cursor :1,6ya a -> :pu a ===> Drag and drop lines 1-6 to buffer A -> Place the contents of buffer A after the current cursor, ya is the drag command,pu is the place commandCopy the code
Global replacement
: S /old/new substitute can be abbreviated as S. Replace the first string with old in the current line with new, / is used to separate each command, and the last/can be omitted :s/old/new/g Replace all old in the current line with new. 1,10s/old/new/g Replace all old in lines 1-10 with new :%s/old/new/g pass :1,$s/old/new/g, replace old with new :1,10s/old/new/gc G /pattern/s/old/new/g Replace old with new when encountering a line containing pattern :g/pattern/s//new/g Replace pattern with new when encountering a line containing pattern. %s/pattern/new/gCopy the code
The metacharacter of the search pattern
Matches any single character, including Spaces, excluding newline * matches any single character before this symbol, which can be 0 to more than one. ^ means that the following regular expression must be at the beginning of the line. $means that the regular expression must be at the end of the line. [^ A-z] is used to match characters that are not lowercase letters. ^ in [] is used to buffer the contents of the pattern matching between \(\) and \(\). Generally, 9 patterns in a line can be reserved. For example \(That\) or \(this\) saves That to buffer 1. This through 2 \<\> matches words beginning with certain characters. For example \<ac will only match words beginning with ac. Ac \> matches only the word ending in ac ~ matches any regular expression used in the previous search, which can only be used for regular searches. That is, / [:alnum:] alphanumeric characters [:alpha:] alphanumeric characters [:blank:] Spaces and indices [: CNCTRL :] Control characters [:digit:] digit characters [:graph:] Printable and visible characters (excluding Spaces), Excluding whitespace [:print:] printable characters, including blank [:punct:] punctuation [:space:] blank character [:upper:] uppercase character [:xdigit:] Hexadecimal digit \n substitutes for the character in the NTH mode stored by \(\), n for numbers 1-9, The previous stored pattern is evaluated from left to right \ see the following character as a normal character & when used in a replacement string, replace it with the full pattern matched by the search pattern, e.g. %s/AAA/&, BBB replaces the string with AAA, BBB \u or \l changes the next character in the substitution string to uppercase or lowercase; \u or \l or \e or \e replaces all subsequent strings or until the occurrence of \e or \eCopy the code
: the set command
:setOption Opens options:setNooption Disables options:setIC specifies that the search mode ignores case:setNoic specifies that the search mode is case-sensitive:setWindow =20 Sets the number of lines displayed in the screen window:setWrapmargin sets the right margin distance for wrapping:setAll Checks the options vi is using:setoption? Query the current value of the specified option noignorecase Case-sensitive search wrapScan Go back to the beginning of the file and continue the search, which indicates that text matching can be found at any point in the file magic pattern matching wildcard autoindent setting autoindent Ctrl-t indent the next layer, CTRL-dUpper layer indent ShiftWith =4 Set default left or right for 4 Spaces tabStop set TAB for 4 Spaces List display control characters showmatch display matching brackets, insert mode will have effect Mourse =a Multi-window under mouse followCopy the code
Exrc file, no: ok, because exrc file is read by EX
Exrc file in the home directorysetExrc: ex can read the. Exrc file in the current directory: so.otherexrcsourceCan be shortened to so, load other EXRC filesCopy the code
Executing Unix Commands
:!command! Ex is told to create a shell, and the subsequent text is treated as uninx :! Date Displays the system time :sh Creates a shell, executes multiple uninx commands, and presses CTRL at the end-dBack to vi :r!command           readThis can be abbreviated to r, and can be combined with the uninx command to read the result of the uninx command into a file :1r! Date reads the system time to the first line of the file :1,10! Sort sorts rows 1-10!)commandPass the next sentence tocommand
Copy the code
Word abbreviation, insert pattern definition macro
Ab MB Mobiuspace The abbreviation of Mobiuspace is SET to MB. When you type Mobiuspace in vi, it is automatically expanded. The abbreviated word cannot appear at the end of the phraseCopy the code
Command abbreviation, command pattern definition macro
:map x sequence Defines the character x to be mapped to the editing command sequence :unmap x Undefines the editing command sequence :map Lists all the characters to be mapped :map v dwelp dw Deletes a word, E moves to the end of the next word, L moves a space to the right, and P places the deleted word in the new position :map! - Forcible override in dw mode - Indicates the DW commandCopy the code
With tags, uninx has the ctags command that can be used with vi’s :tag command
:ctags *.c Creates a tags file that describes all c source files: Tag Name Look for the definition of name in the tags fileCopy the code
Vim starts
vim -dFile1 file2 diff Is short for D. Files are started in diff mode. Vim -o file1 file2 All files open their own WindowsCopy the code
Vim multi-window editing
Ctrl-w -> s Split current window horizontally :new split current window horizontally, Some configurations are automatically adaptive :vsplit Vertical split current window :[n]split [++opt] [CMD] [file] n Specifies the number of lines to be displayed in the new window for VIm Ctrl-W -> Ctrl-j Move to the next window Ctrl-W -> Ctrl-K Move to the previous window Ctrl-W -> Ctrl-H Move to the left of the current window Ctrl-W -> CTRL-lMove to the right of the current window Ctrl-W -> Ctrl-P Move to the last accessed window Ctrl-W -> Ctrl-T Move to the upper left corner of the window Ctrl-W -> Ctrl-B Move to the lower right corner of the window Ctrl-W -> + Add a line to the current window :resize +2 Adds two lines to the current windowCopy the code
Vim buffer command
:ls lists buffer filesCopy the code