Common VI shortcut keys
Cursor control command
Command cursor Movement
H or ^ h move one character to the left
J or ^ n move down a row
K or ^ P move up a row
L or move one character to the right by Space
G move to the last row of the file
Ng moves to the nth line of the file
W moves to the beginning of the next word
W moves to the beginning of the next word, ignoring punctuation
B is moved to the beginning of the previous word
B moves to the beginning of the previous word, ignoring punctuation marks
L move to the last line of the screen
M moves to the middle line of the screen
H move to the first line of the screen
E move to the end of the next word
E. Move to the end of the next word and ignore punctuation marks.
(Move to the beginning of a sentence
) Move to the end of the sentence
{Move to the beginning of a paragraph
} Move to the beginning of the next paragraph
0 or | move to the first column of the current row
N | column N to the current row
^ Move the first non-null character to the current row
$ Move to the last character of the current row
+ Or return refers to the first character in the next line.
-Move to the first non-null character in the previous line
Add text in VI
Command insert action
A insert text behind the cursor
A inserts text into the current row
I insert text before the cursor
I insert text before the current row
O Insert a new row under the current row
O Insert a new row above the current row
: R file reads the file content and inserts it into the current row.
: NR file reads the file content and inserts it after line N
Return to command mode
^ V char indicates that the specified meaning of char is ignored during insertion to insert special characters.
Delete text in VI
Command to delete
X deletes the character at the cursor. You can add the number of characters to be deleted before X.
NX deletes n characters from the current cursor
X deletes the character before the cursor. You can add the number of characters to be deleted before X.
NX deletes n characters from the current cursor
Delete DW to the beginning of the next word
Ndw deletes n words from the current cursor
DG deletes the row until the end of the file
Dd deletes the entire row
NDD is deleted from the current row.
DB deletes the word before the cursor
NDB deletes n words from the current row.
: N, MD deletes n rows from Row M.
D Or d $ Delete from cursor to end of line
Delete dcursor_command to the cursor command. For example, DG will delete the dcursor_command from the abnormal line to the end of the file.
^ When inserting h or backspace, delete the previous character
^ W Delete the previous word when inserting
Modify VI text
The number before each command indicates the number of times the command is repeated.
Command Replacement Operation
Rchar replaces the current character with Char
R text escape replaces the current character with text until the ESC key is changed
Stext escape uses text to replace the current character
S or cctext escape Replace the entire line with text
Cwtext escape: change the current word to text
Ctext escape: Change the remaining text of the current row to text
Change CG escape to the end of the file
Ccursor_cmd text escape changes from the current position to the cursor command position to text
Search and replace in VI
Command search and replacement operations
/Text Search forward text in the file
? Text Search for text in the file
N repeated searches in the Same Direction
N repeated searches in the opposite direction
Ftext looks forward to the current row for text
Ftext searches for text in the current row.
Ttext searches for text in the current row and positions the cursor in the first character of text.
Ttext searches for text in the current row and positions the cursor in the first character of text.
: Case Insensitive for set IC search
: Set noic search is case sensitive
: S/oldtext/newtext replace oldtext with newtext
: M, NS/oldtext/newtext replace oldtext with newtext through N in line m
& Repeat the final: s command
: G/text1/S/text2/text3 search for rows containing text1, replace text2 with text3
: G/text/command: run the commands represented by command in all rows that contain text.
: V/text/command: run the commands represented by command in all rows that do not contain text.
Copy text in VI
Command copy operation
YY puts the content of the current row into the temporary buffer
Nyy puts n rows of content into the temporary buffer
P put the text in the temporary buffer behind the cursor
P put the text in the temporary buffer before the cursor
"(A-z) nyy copies n rows into a nameable buffer named in parentheses. Omitting n indicates the current row.
"(A-z) NDD Delete n rows into the nameable buffer named in parentheses. Omitting n indicates the current row
"(A-z) P put the content of the nameable buffer named parentheses after the current row
"(A-z) P put the content of the nameable buffer named parentheses before the current row
Undo and repeat in VI
Command Undo operation
U undo the last modification
U undo all modifications to the current row
. Repeat the last modification
Repeat the preceding F, F, T, or t search command in the opposite direction.
; Repeat the preceding F, F, T, or t SEARCH Command
"NP retrieves the last n deletions (buffer contains a certain number of deletions, usually 9)
N repeats the previous/or? SEARCH Command
N repeats the previous/or? Command
Save text and exit vi
Command to save and/or exit
: W save the file but do not exit vi
: W file saves the modification in file but does not exit vi
: WQ or ZZ or: X save the file and exit vi
: Q! Exit VI if you do not save the file
: E! Discard all modifications and start editing the last saved file.
Options in VI
Option Function
: Set all print all options
: Set nooption disable Option
: Set nu: print the row number before each row
: Set showmode: whether to display the input mode or the replacement mode.
: Case Insensitive for set noic search
: Set list: displays the tab (^ I) and end-of-line characters.
: Set Ts = 8. Set Tab stops for text input.
: Set window = N set text window to show N rows
VI Status
Option Function
:. = Print the row number of the current row
: = Print the number of rows in the file
^ G display the file name, current row number, total number of lines of the file, and the percentage of file locations
: L use the letter "L" to display many special characters, such as tabs and line breaks
Locate paragraphs and placement tags in the text
Option Function
{Insert in the first column {to define a paragraph
[Return to the beginning of a paragraph
] Move Forward to the beginning of the next paragraph
M (a-z) uses a letter to mark the current position. For example, use MZ to Mark Z.
'(A-z) move the cursor to the specified tag. For example, use 'z to move the cursor to Z.
Connect rows in VI
Option Function
J. Connect the next row to the end of the current row
N rows after NJ connection
Cursor placement and screen Adjustment
Option Function
H move the cursor to the top line of the screen
NH move the cursor to line N at the top of the screen
M move the cursor to the center of the screen
L move the cursor to the bottom line of the screen
NL moves the cursor to line N at the bottom of the screen
^ E (CTRL + E) rolling a row on the screen
^ Y: Roll one row down the screen
^ U roll up half page
^ D roll down half of the screen
^ B rolling a page on the screen
^ F roll down a page
^ L screen repainting
Z-return: set the current row to the top line of the screen.
NZ-return: set row N under the current row to the top line of the screen.
Z. Set the current row to the center of the screen
NZ. Set row N on the current row to the center of the screen
Z-set the current row to the bottom line of the screen
NZ-set row N on the current row to the bottom line of the screen
Shell escape commands in VI
Option Function
:! Command :! Ls
:!! Execute the previous shell command
: R! The command reads the input of the command and inserts it, for example, R! Ls will first execute LS and then read the content
: W! Command uses the edited file as the standard input of the command and executes the command, for example, W! Grep all
: CD Directory: change the current working directory to the directory indicated by directory
: Sh starts a sub-shell and returns VI using ^ d (CTRL + D)
: So file: read and execute commands in shell program file
Macros and abbreviations in VI
(Do not use control keys and symbols. Do not use character keys K, V, G, Q, V, *, =, and function keys)
Option Function
: Map key command_seq defines a key to run command_seq. For example, map e EA can always move e to the end of a word to append text.
: Map displays all defined Macros in the status line.
: Umap key: Macro used to delete the key
: AB string1 string2 defines an abbreviation, so that when string1 is inserted, string1 is replaced with string2. When you want to insert text, type string1 and Press ESC. Then, string2 is inserted.
: AB show all abbreviations
: Una string cancels the abbreviation of string
Indent text in VI
Option Function
^ When I (CTRL + I) or tab inserts text, insert the moving width. The moving width is defined in advance.
: Set AI enable auto indent
: Set Sw = N set the moving width to n characters
N <make n rows move a width to the left
N> make n rows move one width to the right. For example, 3> move one width to the right of the next three rows.