Common commands and shortcut keys of Linux vi editor

1: Search keywords
For example, if you are looking for lookup, you only need to enter /lookup, <enter> to find it, and you can use n and shift n to find the previous and next
or use: g/lookup/ all matching lines

cursor control commands

Command Cursor move
h or ^h move left one character
j or ^j or ^n move down one line
k or ^p move up one line
l or space move right one character
G move to the last line of the
file nG move to the end of the file Line n
w Move to the beginning of the
next word W Move to the beginning of the next word, ignore punctuation
b Move to the beginning of the previous word
B Move to the beginning of the previous word, ignore punctuation
L Move to the last line of the screen
M move 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, ignoring punctuation
(move to the beginning of the
sentence) move to the end of the sentence
{ move to start of paragraph
} move to the beginning of the next paragraph
0 or | move to the first column of the
current line n| move to the nth column of the
current line ^ move to the first non-blank character of the
current line $ move to the first non-blank character of the current line last character
+ or return move to the first character of the next line
- move to the first non-blank character of the previous line


add text in vi

Command Insert Action
a Insert text after cursor
A Insert text at current line
i Insert text before cursor
I Insert text before current line
o Insert new line below
current line O Insert new line above current line
: r file read Enter the file content and insert it after the current line
: nr file Read the file content and insert it after the nth line
escape Return to command mode
^v char The specified meaning of char is ignored when inserting, this is to insert special characters


delete text in vi

Command Delete operation
x Delete the character at the cursor, you can add the number of characters to be deleted before x
nx Delete n characters from the current cursor
X Delete the character before the cursor, you can add the number of characters to be deleted before X
nX deletes n characters forward from the current cursor
dw deletes to the beginning of the next word
ndw deletes n characters backward from the current cursor
dG deletes lines until the end of the file
dd deletes the entire line
ndd deletes
db from the current line Delete the word in front of the cursor
ndb Delete n words from the current line forward
: n,md Delete n lines from the mth line forward
d or d$ Delete from the cursor to the end of the line
dcursor_command Delete to the cursor command, such as dG will From when the production line is deleted to the end of the file
^h or backspace insert, delete the preceding character
^w When inserting, delete the preceding character


Modify vi text

The number in front of each command indicates the number of times the command is repeated
Command Substitute operation
rchar Replace the current character with char
R text escape Replace the current character with text until the Esc key is pressed
stext escape Replace the current character with text
S or cctext escape Replace the entire line with text
cwtext escape Change the current word to text
Ctext escape Change the rest of the current line to text
cG escape Modify to the end of the file
ccursor_cmd text escape Change to text from the current position to the cursor command position


find and replace in vi

Command Find and Replace
/text Find text forward in file
?text Find text backward in file
n Repeat search in same direction
N Repeat search in opposite direction
ftext Find text forward
on current line Ftext Find backward on current line text
ttext Search text forward in the current line and position the cursor at the first character of text
Ttext Search backward in the current line for text and position the cursor at the first character of text
: set ic ignore case when searching
: set noic Find case sensitive
: s/oldtext/newtext replace oldtext with newtext
:m,ns/oldtext/newtext On m line pass n, replace oldtext with newtext
& repeat the last :s command
:g/text1/s/ text2/text3 Find the line containing text1, replace text2 with text3
:g/text/command run the command represented by command
on all lines containing text:v/text/command run the command represented by command on all lines not containing text


copy text in vi

command copy operation
yy put the content of the current line into the temporary buffer
nyy put the content of n lines into the temporary buffer
p put the text in the temporary buffer after the cursor
P put the text in the temporary buffer before the cursor
" (az)nyy Copy n lines into a nameable buffer named in parentheses, omit n to indicate the current
line The line
"(az)p puts the contents of the nameable buffer named parentheses after the current line
"(az)P puts the contents of the nameable buffer named parentheses before the current line


undo and redo in vi

Command Undo
u Undo the last modification
U Undo all modifications on the current line
. Repeat the last modification
, repeat the previous f, F, t or T find command in the opposite direction
; repeat the previous f, F, t or T find command
"np Retrieve the last nth delete (there is a certain number of deletes in the buffer, usually 9)
n Repeat the previous / or? Find command
N Repeat the previous / or? command in the opposite direction


save text and exit vi

Command Save and/or Exit Actions
:w Save file without exiting vi
:w file Save changes in file without exiting vi
:wq or ZZ or :x Save file and exit vi
:q! Exit vi without saving file
: e! Abandon all changes and start editing from the last time the file was saved


options in vi

Option function
: set all print all options
: set nooption close option option
: set nu print line number before each line
: set showmode display input mode or replacement mode
: set noic ignore case when searching
: set list display tab character (^ I) and end-of-line symbols
: set ts=8 set tab stops for text input
:set window=n set the text window to display n lines


state of vi

Option Function
: .= print the line number of the current line
:= print the number of lines in the file
^g display the file name, the current line number, the total number of lines in the file and the percentage of the file position
: l use the letter "l" to display many special characters such as tabs and newlines


Positioning paragraphs and placing tags in text

Option Action
{ Insert { in the first column to define a paragraph
[[return to the beginning of the paragraph
]] move forward to the beginning of the next paragraph
m(az) mark the current position with a letter, such as mz for mark z
'(az) move the cursor to the specified mark, for example, use 'z to move to z


concatenate lines in vi

Option Action
J Join the next line to the end of the current line
nJ Join the next n lines


Cursor Placement and Screen Adjustment

Option Action
H Move the cursor to the top line
of the screen nH Move the cursor to the nth line below the top line of the screen
M Move the cursor to the middle of the screen
L Move the cursor to the bottom line of the screen
nL Move the cursor to the bottom line of the screen The nth line of
^e(ctrl+e) will scroll the screen up one line
^y will scroll the screen one line down
^u will scroll the screen half a page
^d will scroll the screen half a page
^b will scroll the screen one page
^f will Scroll the screen down one page
^l Redraw the screen
z-return Set the current line to the top line of the screen
nz-return Set the nth line below the current line to the top line of the screen
z. Set the current line to the center of the screen
nz . set the nth line on the current line to the center of the screen
z- set the current line to the bottom line of the screen
nz- set the nth line on the current line to the bottom line of the screen


shell escape command in vi

Option Function
: !command Execute the command command of the shell, such as: !ls
:!! Execute the previous shell command
: r!command Read the input of the command command and insert it, such as: r!ls will execute ls first, and then read the content
:w!command Use the current edited file as the standard input of the command command and execute the command command, such as :w!grep all
:cd directory Change the current working directory to the directory represented by directory
:sh will start a subshell, use ^ d (ctrl+d) returns vi
:so file to read and execute commands in the shell program file


Macros and abbreviations in vi
(avoid control keys and symbols, don't use characters K, V, g, q, v, *, = and function keys)

Option Function
: map key command_seq Define a key to run command_seq, such as: map e ea, e can move to a word at any time

Append the text at the end of
:map Display all defined macros on the status line
: umap key Remove the macro for this key
: ab string1 string2 Define an abbreviation such that when string1 is inserted, string1 is replaced by string2. when inserting text

, type string1 and press the Esc key, the system will insert string2
:ab Display all abbreviations
:una string Cancel string abbreviations


Indent text in vi

Option Function
^i(ctrl+i) or tab When inserting text, insert the width of the movement, the width of the movement is pre-defined
: set ai Turn on automatic indentation
: set sw=n Set the movement width to n characters
n<< Move all n lines to the left by one width
n>> Make all n lines move to the right by one width, for example, 3>> will move each of the next three lines to the right by a moving width

Guess you like

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