Linux-Vim shortcut keys

Vim key arrangement

  • space bar
  • Enter
  • represents the key combination of and f

Write, save, exit

:q[uit] "Exit
:q! " Force quit
:w[rite] " save
:w! "Force saving. Whether the save can be successful depends on the user's permissions on the file.
:w ! sudo tee % " If you don't have permission to save, try this command
ZZ " Two capital Zs, exit directly without modification, exit after saving modifications

:w newfilename "Save as new file
:1, 10 w newfilename "Save the contents of lines 1 to 10 as a new file
:1, 10 w >> filename " Save lines 1 to 10 as a new file
:r filename "Append the contents of the target file to the line below the current cursor
:3 r filename "Append the contents of the target file to line 3

:! ls " Temporarily leave Vim to view the files in the current directory, press Enter and return to Vim

Cursor movement

h " Arrow keys ←
j " Arrow keys ↓
k " Arrow keys ↑
l "Direction keys →

0 "Move to the beginning of the line
$ " moves to the carriage return character at the end of the line
g_ "Move to the last non-null character at the end of the line
gg "Move to the first line
G "Move to last line"

w " moves to the beginning of the next word
e " moves to the end of the word
b " moves to the beginning of the word

" uncommonly used
nh "Move n spaces to the left, n is a number
nl "Move n spaces to the right
nj "Move down n lines
nk "Move up n lines
n<space> "Move n spaces to the right, same as nl

H " Move to the first character of the first line of the current screen
M " Move to the first character in the middle line of the current screen
L " moves to the first character of the last line of the current screen
 + " moves to the next line after a non-whitespace character
- " moves to the previous line of non-whitespace characters

:n<cr> "Jump to line n

Page turning

<c-f> "Move down one page
<c-d> "Move down half a page
<c-b> "Move up one page
<c-u> "Move up half a page

Find and Replace

/word " Search word word downwards from the cursor position
?word " Search word word upward from the cursor position
n " English letter n, locate the next matching target according to / or ? search direction
N " is the opposite of n, positioning the matching target

:n1,n2s/word1/word2/g " n1, n2 represents numbers, replace the words in lines n1 to n2
:1,$s/word1/word2/g " Full text replacement, can also be written as :%s/word1/word2/g
:1,$s/word1/word2/gc "Replace the full text and a confirmation prompt will appear.

Copy, paste, delete

x "Delete one character backwards
nx " Delete n characters backward
X " Delete one character forward
nX "Delete n characters forward

dd "Delete the current line
ndd "Delete n lines down
d1G / dgg "Delete data from the first row to the current row
dG "Delete data from the current row to the last row
d$ "Delete the current character to the end of the line
D "Delete the current character to the end of the line
d0 "Delete from the beginning of the line to the current character

yy "Copy the current line
Y "Copy the current line
nyy "Copy n lines starting from the current line
y1G / ygg "Copy from the first line to the current line
yG "Copy from the current line to the last line
y0 "Copy from the beginning of the line to the current character
y$ "Copy from the current character to the end of the line

p, P " Paste, p pastes to the line below the cursor, P pastes to the line above the cursor
yyp " copy and paste
ddp "Delete and paste, equivalent to moving the current line down


" + y " Copy this article to the system clipboard
" + p " pastes the system clipboard into Vim (does not affect text formatting)

Insert

i " Enter insert mode before the cursor
I "Enter insert mode before the first non-empty character on the left side of the current line, similar to the <c-a> shortcut key in other editors
a " enters insert mode after the cursor
A " Enters insert mode before the first non-empty character on the right side of the current line, similar to the <c-e> shortcut key in other editors
o " inserts in the line next to the cursor
O "Inserts the line above the cursor
s "Delete the current character and enter insert mode
S "Delete the current line and enter insert mode

vc "Delete the current character and enter insert mode
cc "Delete the current line and enter insert mode
c0 "Delete the cursor position to the beginning of the line and enter insert mode
cg_ "Delete the last non-empty character from the cursor position to the end of the line and enter insert mode
ce " Delete the cursor position to the end of the word and enter insert mode
cw " deletes the cursor position to the end of the word and enters insert mode
ciw "Delete the current word and enter insert mode
cip "Delete the entire paragraph and enter insert mode
ci( "Deletes the content within the current brackets and enters insert mode. Applicable to all paired tags such as ([{<!-- --><'`


Undo and redo

u "Cancel
<c-r> "Redo
. " Repeat to complete the operation

Replace

~ "Reverse case of current character
g~~ "Reverse the case of characters on the front line
vu "The current character is lowercase
vU "The current character is uppercase
vU "The current character is uppercase
viwu "The current character is lowercase
viwU "The current character is uppercase
ggguG " All characters in the text are lowercase
gggUG " All characters in the text are uppercase

:%s/\<./\u & amp;/g " Capitalize the first letter of all words (need to use :nohls to remove highlighting)
:%s/\<./\l & amp;/g " Lowercase the first letter of all words
:%s/.*/\u & amp; " Capitalize the first letter of each line
:%s/.*/\l & amp; " Lowercase the first letter of each line

Capitalization

~ "Reverse case of current character
g~~ "Reverse the case of characters on the front line
vu "The current character is lowercase
vU "The current character is uppercase
vU "The current character is uppercase
viwu "The current character is lowercase
viwU "The current character is uppercase
ggguG " All characters in the text are lowercase
gggUG " All characters in the text are uppercase

:%s/\<./\u & amp;/g " Capitalize the first letter of all words (need to use :nohls to remove highlighting)
:%s/\<./\l & amp;/g " Lowercase the first letter of all words
:%s/.*/\u & amp; " Capitalize the first letter of each line
:%s/.*/\l & amp; " Lowercase the first letter of each line

Multi-window operation

:sp filename "Split windows up and down
:vs[p] filename " Split windows left and right

<c-w>h[j[k[l]]] " Move the cursor to the window in that direction according to the direction keys

<c-w>[N]> "N digits, optional, increase the width of the current window by N columns"
<c-w>[N]< "N digits, optional, reduce the width of the current window by N columns"
<c-w>[N] + "N digits, optional, increase the height of the current window by N lines"
<c-w>[N]- "N digits, optional, reduce the height of the current window by N lines"
<c-w>= "Set all windows to have the same width and height

<c-w>[N]n "N digits, optional, open a new window N lines high, default is half of the entire window"
<c-w>[N]s "N digits, optional, vertically split the current window into two upper and lower windows for display"
                    "The new window can be N lines high, and the default is half the entire window"
                    "Similar to :sp current_file"
<c-w>[N]v "N digits, optional, horizontally split the current window into two left and right windows for display"
                    "The new window can be N columns wide, defaulting to half the entire window"
                    "Similar to :vs current_file"


<c-w>o "Close all windows except the current window

<c-w>r " Rotate window clockwise
<c-w>R " Rotate window counterclockwise
<c-w>x " Swap the left and right or upper and lower corresponding windows

<c-w>q "Quit window
:q "Quit window

Multiple file editing

vim file1 file2 "Open two files at the same time

:files "View the list of files currently being edited, %a represents which file is being operated on
  1 %a "file1" line 1
  2 "file2" line 0

:n " jump to the next file, where n is the letter
:N " jump to previous file


(Picture network, intrusion and deletion)