linux vi command

in the vi editor.
First press ESC to exit, then press the : key, then enter wq to save and exit, and then hit Enter to exit the editing state.

 

 

The following are VI commands related to file modification and saving:

Insert command

i Insert I before the current position Insert

I at the beginning of the current line Insert A

after the current position Insert

A at the end

of the current line Insert a line after the current line

O Insert before the current line One line

search command

/text to find text, press n to find the next one, and n to find the previous one.

?text find text, reverse search, press n key to find the next one, press n key to find the previous one.

There are some special characters in vim that need to be escaped when searching.*[]^%/?~$

:set ignorecase ignore case search

:set noignorecase do not ignore case search to

find very long words, if a word is very long , it is troublesome to type, you can move the cursor to the word, and press the * or # key to search for the word, which is equivalent to / search. The # command is equivalent to ? search.

:set hlsearch Highlight search results, all results are highlighted instead of just one match.

:set nohlsearch Turn off the highlighted search display

:nohlsearch Turn off the current highlighting, which will be highlighted again if you search again or press the n or N key.

:set incsearch Step-by-step search mode, searching for the currently typed character without waiting for typing to complete.

:set wrapscan Re-search, when the head or tail of the file is found, return to continue the search, enabled by default.

The replacement command

ra replaces the current character with a, the current character is the character where the cursor is located.

s/old/new/ replace new with old, replace the first match of the current line

s/old/new/g replace new with old, replace all matches of the current line

%s/old/new/ replace new with old, replace The first match of all lines

%s/old/new/g Replace new with old, replace all matches of the whole file

: 10,20 s/^/ /g Add four in front of each line on line 10 to line 20 spaces, for indentation.

ddp swaps the line under the cursor with the line immediately below it.

The movement command

h moves one character to the left
l moves one character to the right. This command is rarely used, and is generally replaced by w.
k move up one character
j move down one character
More than four commands can be used with numbers, such as 20j is to move down 20 lines, 5h is to move 5 characters to the left, in Vim, many commands can be used with numbers, such as Delete 10 characters 10x, insert 3 after the current position! , 3a! < Esc >, the Esc here is required, otherwise the command will not take effect.

w Move forward one word (the cursor stops at the beginning of the word), and if the end of the line is reached, go to the beginning of the next line. This command is fast and can replace the l command.

b move backward one word 2b move backward 2 words

e, the same as w, but the cursor stops at the end of the word

ge, same as b, the cursor stops at the end of the word.

^ Move to the first non-whitespace character on the line.

0 (the number 0) is moved to the first character of the line, and

< HOME > is moved to the first character of the line. Same as 0 health.

$ move to the end of the line 3$ move to the end of the line 3 lines below

gg move to the beginning of the file. = [[

G (shift + g) move to end of file. = ]]

The f (find) command can also be used to move, fx will find the first x character after the cursor, 3fd will find the third d character.

F Same as f, reverse search.

Jump to the specified line, colon + line number, carriage return, such as jumping to line 240 is: 240 carriage return. Another method is line number + G, such as 230G to jump to line 230.

Ctrl + e Scroll down one line

Ctrl + y Scroll up one line

Ctrl + d Scroll half screen down

Ctrl + u Scroll up half screen

Ctrl + f Scroll down one screen

Ctrl + b Scroll up one screen

Undo and redo

u Undo ( Undo)
U Undo the operation on the entire line
Ctrl + r Redo (Redo), that is, the undo of the undo.

Delete command

x deletes the current character

3x deletes the current cursor start three characters backward

X deletes the previous character of the current character. X=dh

dl deletes the current character, dl =x

dh deletes the previous character

dd deletes the current line

dj deletes the previous line

dk deletes the next line

10d deletes the 10 lines starting from the current line.

D Deletes the current character to the end of the line. D=d$

d$ delete all characters after the current character (this line)

kdgg delete all lines before the current line (excluding the current line)

jd G ( jd shift + g) delete all lines after the current line (excluding the current line)

:1,10d Delete lines 1-10

: 11, $d Delete lines 11 and all later

: 1, $d Delete all lines

J(shift + j) Delete empty lines between two lines, actually merging two lines.

Copy and paste

yy Copy the current line

nyy Copy the n lines starting after the current one, such as 2yy copy the current line and the next line.

p Paste after the current cursor. If the yy command is used to copy a line, it will be pasted on the next line of the current line.

shift+p Paste before current line

: 1,10 co 20 Insert lines 1-10 after line 20.

:1,$ co $ makes a copy of the entire file and appends it to the end of the file.

In normal mode, press v (word by word) or V (by line) to enter visual mode, then use the jklh command to move to select some lines or characters, and press y again to copy

the ddp exchange current line and its next line

xp exchange The current character and the next character

cut command

Press v (word by word) or V (line by line) to enter the visual mode in normal mode, then use the jklh command to move to select some lines or characters, and then press d to cut cut

ndd Cut n lines after the current line. Use the p command to paste the cut content

: 1,10d Cut lines 1-10. Use the p command to paste the cut content.

:1, 10 m 20 moves lines 1-10 after line 20.

Exit command

: wq save and exit

ZZ save and exit

:q! force quit and ignore all changes

:e! discard all changes and open the original file.

Guess you like

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