Vim command cheat sheet

ChineseEnglish

Introduction: Vim command lookup tables, annotated vimrc profiles, classic Vim keyboard diagrams, practical Vim books, Markdown format, directory-based search, systematic learning, quick familiarity with use! 📚

Repeats

  1. . # Repeat (Dot) command, repeats the previous command
  2. N{command} # Repeat a command N times, e.g. 10k, cursor moves up 10 lines

The use of macros and regular expressions can also be used to reduce the number of repetitive operations.

Cursor motions

Note: In normal mode, any of the actions can be repeated.

  1. # -------------------- Unit level cursor movement --------------------
  2. h # The cursor moves to the left of the character
  3. j # Move the cursor down one line
  4. k # Move the cursor up one line
  5. l # The cursor moves to the right of the character
  6. # -------------------- Word-level cursor movement --------------------
  7. w # [count] words forward. exclusive motion
  8. W # [count] WORDS forward. exclusive motion
  9. e # Forward to the end of word [count] inclusive
  10. E # Forward to the end of WORD [count] inclusive
  11. b # [count] words backward. exclusive motion
  12. B # [count] WORDS backward. exclusive motion
  13. # -------------------- Block level cursor movement -------------------
  14. 0 # To the first character of the line
  15. ^ # To the first non-blank character of the line. exclusive motion. Any count is ignored
  16. $ # To the end of the line
  17. gg # Goto line [count], default first line, on the first non-blank character line
  18. G # Goto line [count], default last line, on the first non-blank character line
  19. [N]G # Jump to the Nth row, for example 10G is to move to the tenth row
  20. :N # Jump to the Nth line, for example: 10<Enter> is to move to the tenth line
  21. N% # Move to the N% position of the file, for example 10% is moved to the 10% position of the file
  22. N| # Move to N columns of the current row
  23. <Enter> # Move to the first non-blank character in the next line
  24. N<Enter> # Move the cursor down N lines
  25. ge # Move backward to the end of the word
  26. gE # Move backward to the end of a word separated by a whitespace
  27. ) # Move forward one sentence (separated by periods)
  28. ( # Move backward one sentence (period separated)
  29. } # Move forward one paragraph (separated by blank lines)
  30. { # Move backward one paragraph (separated by blank lines)
  31. + # Move to the first non-blank character in the next line (same as the Enter key)
  32. - # Move to the first non-blank character in the previous line
  33. H # Move to the upper part of the screen (H: High)
  34. M # Move to the middle of the screen (M: Middle)
  35. L # Move to the bottom of the screen (L: Low)
  36. gm # Move to the middle of the line
  37. gj # Move the cursor down one screen line (ignore automatic line wrapping)
  38. gk # Move the cursor up one screen line (ignore auto-wrap)
  39. <S+Up> # Hold down the <Shift> key and then press the <Up> arrow key to page up
  40. <S+Down> # Hold down the <Shift> key and then press the <Down> arrow key to page down
  41. <S+Left> # Hold down the <Shift> key and press the <Left> arrow key to move one word to the left
  42. <S+Right> # Hold down the <Shift> key and press the <Right> arrow key to move one word to the right
  43. :ju[mps] # Print the jump list
  44. :cle[arjumps] # Clear the jump list of the current window
  45. # ------------------ Screen-level cursor movement -------------------
  46. zz # Adjust the cursor line to the center of the screen
  47. zt # Adjust the cursor line to the upper part of the screen
  48. zb # Adjust the cursor line to the bottom of the screen
  49. Ctrl+e # Scroll up one line
  50. Ctrl+y # Scroll down one line
  51. Ctrl+u # Move up 1/2 a screen
  52. Ctrl+d # Move down 1/2 a screen
  53. Ctrl+f # Move forward one full screen
  54. Ctrl+b # Move back one full screen
  55. # ------------------ Programming assistance level cursor movement ---
  56. % # Match jump to the corresponding {} () []
  57. gd # Jump to the local definition (the definition of the word under the cursor)
  58. gD # Jump to the global definition (the definition of the word under the cursor)
  59. gf # Open the file whose name is the file name under the cursor
  60. [[ # Jump to the previous top-level function
  61. ]] # Jump to the next top-level function
  62. [m # Jump to the previous member function
  63. ]m # Jump to the next member function
  64. [{ # Jump to the previous unmatched {
  65. ]} # Jump to the next unmatched }
  66. [( # Jump to the previous unmatched (
  67. ]) # Jump to the next unmatched )
  68. [c # The last difference (when diffing)
  69. ]c # The next difference (when diffing)
  70. [/ # Jump to the beginning of the C comment
  71. ]/ # Jump to the end of the C comment
  72. `` # Go back to the last jumped position
  73. '' # Go back to the last jumped position
  74. `. # Back to the last edited position
  75. '. # Back to the last edited position

Insert mode

  1. i # Enter insert mode at the cursor
  2. I # Enter insert mode at the beginning of the line
  3. a # Enter insert mode after the cursor
  4. A # Enter insert mode at the end of the line
  5. o # Insert a new row in the next row and enter insert mode
  6. O # Insert a new row in the previous row and enter insert mode
  7. s # Delete the character under the cursor and insert text
  8. S # Delete the current line and insert text
  9. gi # Go to the position of the last insert mode
  10. gI # Insert text at the start of line (column 1)
  11. <Esc> # Exit insert mode
  12. Ctrl+[ # Exit insert mode (equivalent to <Esc> key)
  13. Ctrl+C # Like Ctrl-[ and <Esc>, but does not check for abbreviation

Insert mode commands

Note: Enter insert mode by i, I, a, A, o, O, s, S command.

  1. <Up> # Cursor up
  2. <Down> # Cursor down
  3. <Left> # Move the cursor to the left
  4. <Right> # Move the cursor to the right
  5. <S+Up> # Hold down the <Shift> key and then press the <Up> arrow key to page up
  6. <S+Down> # Hold down the <Shift> key and then press the <Down> arrow key to page down
  7. <S+Left> # Hold down the <Shift> key and press the <Left> arrow key to move one word to the left
  8. <S+Right> # Hold down the <Shift> key and press the <Right> arrow key to move one word to the right
  9. <PageUp> # Page up
  10. <PageDown> # Page down
  11. <Delete> # Delete the character at the cursor
  12. <Backspace> # Backspace key is to delete characters backward
  13. <Home> # Cursor jump to the beginning of the line
  14. <End> # Cursor jump to end of line
  15. Ctrl+d # Decrease indent
  16. Ctrl+f # Decrease indent
  17. Ctrl+t # Increase indent
  18. Ctrl+h # Delete the previous character, equivalent to Backspace
  19. Ctrl+o # Temporarily exit insert mode, execute a single command and return to insert mode
  20. Ctrl+u # Delete all characters from the current line to the beginning of the line
  21. Ctrl+w # Delete a word before the cursor
  22. Ctrl+\ Ctrl+O # Temporarily exit insert mode (cursor hold), execute a single command and return to insert mode
  23. Ctrl+R 0 # Insert the contents of the register (internal clipboard No. 0), the register name can be followed by Ctrl+R
  24. Ctrl+R " # Insert anonymous register content, which is equivalent to p paste in insert mode
  25. Ctrl+R = # Insert expression calculation result, equal sign followed by expression
  26. Ctrl+R : # Insert the last command line command
  27. Ctrl+R / # Insert the last search keyword
  28. Ctrl+v {char} # Insert non-numeric literals
  29. Ctrl+v {code} # Insert the ASCII/Unicode character encoding represented by three digits, such as Ctrl+v 065
  30. Ctrl+v 065 # Insert a decimal ASCII character (two digits) 065 is the A character
  31. Ctrl+v x41 # Insert hexadecimal ASCII characters (three numbers) x41 is the A character
  32. Ctrl+v o101 # Insert octal ASCII characters (three numbers) o101 is the A character
  33. Ctrl+v u1234 # Insert hexadecimal Unicode characters (four digits)
  34. Ctrl+v U12345678 # Insert hexadecimal Unicode characters (eight digits)
  35. Ctrl+K {ch1} {ch2} # Insert digraph (see :h digraph), quickly enter Japanese or symbols, etc.

Autocomplete

  1. Ctrl+n # Automatic text completion in insert mode, the most commonly used completion
  2. Ctrl+P # Automatic text completion in insert mode
  3. Ctrl+e # When there is a completion list, terminate this completion and continue typing
  4. Ctrl+X # Enter completion mode.Smart completion commands all start with the key combination Ctrl+X
  5. Ctrl+X Ctrl+L # Whole line completion
  6. Ctrl+X Ctrl+N # In insert mode, complete according to keywords in the current file
  7. Ctrl+X Ctrl+K # Completion according to dictionary
  8. Ctrl+X Ctrl+T # Completion according to the synonym dictionary
  9. Ctrl+X Ctrl+F # Complete file name in insert mode
  10. Ctrl+X Ctrl+I # Completion based on keywords in the header file
  11. Ctrl+X Ctrl+] # Complete according to tags
  12. Ctrl+X Ctrl+D # Complete macro definition
  13. Ctrl+X Ctrl+V # Completing Vim commands
  14. Ctrl+X Ctrl+U # User-defined completion method
  15. Ctrl+X Ctrl+S # Spelling suggestions, for example: an English word
  16. Ctrl+X Ctrl+O # Insert Omnifunc completion

Text editor

  1. r # Replace the current character
  2. R # Enter replacement mode until you press <Esc> to leave
  3. [N]s # Delete [count] characters and start insert
  4. [N]S # Delete [count] lines and start insert
  5. [N]x # Delete [count] characters under and after the cursor.Does the same as "dl"
  6. [N]X # Delete [count] characters before the cursor. Does the same as "dh"
  7. cc # Rewrite the current line (delete the current line and enter insert mode), same as S
  8. cw # Overwrite the current word at the beginning of the cursor
  9. ciw # Rewrite the word under the cursor
  10. caw # Rewrite the word under the cursor, and include leading and trailing spaces
  11. c0 # Rewrite to the beginning of the line
  12. c^ # Rewrite to the beginning of the line (the first non-zero character)
  13. c$ # Rewrite to the end of the lin
  14. C # Rewrite to the end of the line (same as c$)
  15. ci" # Rewrite the content in double quotes
  16. ci' # Rewrite the content in single quotes
  17. cib # Rewrite the content in parentheses
  18. cab # Rewrite the content in the parentheses (including the parentheses themselves)
  19. ci) # Rewrite the content in parentheses
  20. ci] # Rewrite the content in the brackets
  21. ciB # Rewrite the content in the braces
  22. caB # Rewrite the content in the braces (including the braces themselves)
  23. ci} # Rewrite the content in the braces
  24. cit # Rewrite the content in the XML tag
  25. cis # Rewrite the current sentence
  26. c2w # Write two words instead
  27. ct( # Rewrite before the parentheses
  28. dd # Delete (cut) a line
  29. d0 # Delete (cut) to the beginning of the line
  30. d^ # Delete (cut) to the beginning of the line (the first non-zero character)
  31. d$ # Delete (cut) to the end of the line
  32. D # Delete (cut) to the end of the line (same as d$)
  33. dw # Delete (cut) the current word
  34. diw # Delete (cut) the word under the cursor
  35. daw # Delete (cut) the word under the cursor, and include leading and trailing spaces (if any)
  36. di" # Delete the content in double quotes
  37. di' # Delete the content in single quotes
  38. dib # Delete the content in parentheses
  39. di) # Delete the content in parentheses
  40. dab # Delete the content in the parentheses (including the parentheses themselves)
  41. di] # Delete the content in the square brackets
  42. diB # Delete the content in the braces
  43. di} # Delete the content in the braces
  44. daB # Delete the content inside the braces (including the braces themselves)
  45. dit # Delete the content in the tag in XML
  46. dis # Delete the current sentence
  47. d2w # Delete the next two words
  48. dt( # Delete to the front of the parenthesis
  49. dgg # Delete to the head of the file
  50. dG # Delete to the end of the file
  51. d} # Delete the next paragraph
  52. d{ # Delete the previous paragraph
  53. Nd # Delete N lines from the beginning of the current line
  54. :Nd # Delete line N
  55. :1,10d # Delete 1~10 lines
  56. ~ # Replace case
  57. g~iw # Replace the case of the current word
  58. gUiw # Convert words to uppercase
  59. guiw # Convert the current word to lowercase
  60. guu # Convert the entire line to lowercase
  61. gUU # Convert the entire line to uppercase
  62. << # Reduce indent
  63. >> # Increase indent
  64. == # Auto indent
  65. Ctrl+A # Increase the number
  66. Ctrl+X # Reduce the number

Text object

Note: Only applicable to visual mode or after operator, for example: operation includes select v, delete d, copy y, modify c, etc.

  1. aw # Operate the entire word, excluding the delimiter (aw: a word)
  2. aW # Manipulate entire words, including separators (aW: a Word)
  3. iw # Operate the entire word, excluding the separator (iw: inner word)
  4. iW # Operate the entire word, including the separator (iW: inner Word)
  5. is # Operate the entire sentence, excluding the separator (s: sentence)
  6. ib # Operation contains block, from [( to ]) (b: block)
  7. iB # Operation contains large blocks, from [{ to ]} (B: Block)
  8. ab # Operate a block, from [( to ])(b: block)
  9. aB # Operate a large block, from [{ to ]} (B: Block)
  10. ap # Operate a paragraph (p: paragraph)
  11. ip # Operation contains paragraph
  12. i) # Manipulate parentheses string
  13. a) # Manipulate the string in the parentheses, including the parentheses themselves
  14. i] # Operation bracket string
  15. a] # Operate the bracket string, including the bracket itself
  16. i} # Manipulate brace strings
  17. a} # Manipulate brace strings, including the braces themselves
  18. i' # Operation single quoted string
  19. a' # Manipulate a single quoted string, including the single quote itself
  20. i" # Manipulate double quoted strings
  21. a" # Manipulate double quoted strings, including the double quotes themselves
  22. a` # Manipulate a backtick string
  23. i` # Operation contains backquote string
  24. a> # Manipulate a <> block
  25. i> # Operation contains <> block
  26. at # Manipulate a tag block, for example from <aaa> to </aaa> (t: tag)
  27. it # The operation contains a tag block, such as from <aaa> to </aaa>
  28. 2i) # Operate outside the two parentheses
  29. 2a) # Operate the outer two levels of parentheses, including the parentheses themselves
  30. Nf) # Move to the Nth parenthesis
  31. Nt) # Move to the Nth parenthesis

The text object can be simply summarized as:

  1. ci'、ci"、ci(、ci[、ci{、ci< # Change the text content in these paired punctuation marks separately
  2. di'di"、di(、dib、di[、di{、diB、di< # Delete the text content in these paired punctuation marks respectively
  3. yi'、yi"yi(、yi[、yi{、yi< # Copy the text content of these paired punctuation marks separately
  4. vi'、vi"、vi(、vi[、vi{、vi< # Select the text content in these paired punctuation marks respectively

cit、dit、yit、vit,Operate the content between a pair of tags separately, and edit HTML and XML are easy to use! In addition, if you change the above i to a, you can operate the matching punctuation and the content in the matching punctuation at the same time.

Move text

  1. :[range]m[ove]{address}

Parameter Description

  • [range]: Indicates the range of rows to be moved.
  • {address}: Indicates the target position of the movement, both of these parameters can be defaulted.

For example

  1. :m+1 # Move down 1 line
  2. :m-2 # Move up 1 line
  3. :8,10m2 # Move the contents of lines 8~10 of the currently opened file to the bottom of line 2

Formatting text

  1. [N]>> # Shift [count] lines one 'shiftwidth' rightwards
  2. [N]<< # Shift [range] lines one 'shiftwidth' left
  3. :ce[nter] # Set the text of the bank to be centered
  4. :le[ft] # Set the text of the bank to the left
  5. :ri[ght] # Set the text of the bank to the right
  6. :[range]ce[nter] [width] # Center lines in [range] between [width] columns
  7. :[range]le[ft] [indent] # Left-align lines in [range]. Sets the indent in the lines to [indent]
  8. :[range]ri[ght] [width] # Right-align lines in [range] at [width] columns
  9. gq # Format the current line
  10. gqq # Format the current line. With a count format that many lines
  11. gq[N]q # Format [count] lines
  12. gqap # Format the current paragraph
  13. gq[N]ap # Format [count] paragraphs
  14. gq[N]j # Format the current line and the following [count] lines
  15. gqQ # Format before the paragraph to the end of the text
  16. J # Join [count] lines, with a minimum of two lines
  17. gj # [count] display lines downward
  18. == # Filter [count] lines like with ={motion}

Copy and paste

Format of the copy command

  1. :[range]co[py]{address}

Parameter Description:

  • [range]:Indicates the range of lines to be copied, where copy can be abbreviated as :co or :t
  • {address}:Indicates the destination of the copy. Both of these parameters can be defaulted to indicate the current line where the Vim cursor is located.

For example: [:5copy.] means to copy line 5 of the file currently opened by Vim to the current line (indicated by .), that is, to create a copy of line 5 and place it below the current line.

The subscript lists some examples and uses of file copy using the abbreviation t of the copy command, which is used to understand the purpose of the copy command copy.

  1. :3,5t. # Copy the content from lines 3 to 5 below the current line
  2. :t5 # Copy the current line below line 5
  3. :t. # Copy the current line below the current line (equivalent to yyp in normal mode)
  4. :t$ # Copy the current line to the end of the text
  5. :'<,'>t0 # Copy the highlighted line to the beginning of the file

Copy and paste commands

  1. p # After pasting to the cursor
  2. P # Paste before the cursor
  3. v # Enter visual mode
  4. V # Visual mode is marked by line
  5. Ctrl+V # Visual mode is marked by column
  6. y # Copy marked content
  7. y$ # Copy the current position to the end of the line
  8. yy # Yank (copy) a line
  9. Y # Copy the current line, same as yy
  10. yiw # Copy the current word
  11. Nyy # Copy the contents of N lines under the cursor
  12. ddp # Cut the current line and paste (first delete the current line, copy it to the register, and paste)
  13. v0 # Select the current position to the beginning of the line
  14. v$ # Select the current position to the end of the line
  15. viw # Select the current word
  16. vib # Select things in parentheses
  17. vi) # Select the content in parentheses
  18. vi] # Select the content in the square brackets
  19. viB # Select the content in the braces
  20. vi} # Select the content in the braces
  21. vis # Select the content in the sentence
  22. vab # Select the content in the parentheses (including the parentheses themselves)
  23. va) # Select the content in the parentheses (including the parentheses themselves)
  24. va] # Select the content in the brackets (including the brackets themselves)
  25. vaB # Select the content inside the braces (including the braces themselves)
  26. va} # Select the content inside the braces (including the braces themselves)
  27. :set paste # Allow paste mode (to avoid automatic indentation affecting formatting when pasting)
  28. :set nopaste # Prohibit paste mode
  29. "?yy # Copy the current line to the register?, The question mark represents the register name from 0-9
  30. "?d3j # Delete the contents of the three lines under the cursor and put them in the register?, The question mark represents the register name of 0-9
  31. "?p # Paste the contents of the register? After the cursor
  32. "?P # Paste the contents of the register? In front of the cursor
  33. :registers # Display the contents of all registers
  34. :[range]y # Copy range, for example: 20,30y is to copy 20 to 30 lines, and :10y is to copy the tenth line
  35. :[range]d # Delete range, for example: 20,30d is to delete 20 to 30 lines, and :10d is to delete the tenth line
  36. "_[command] # Use [command] to delete content without copying (not polluting registers)
  37. "*[command] # Use [command] to copy the content to the system clipboard (requires the Vim version to have clipboard support)

Revocation and restoration

  1. [N]u # The undo command can be combined. For example, Nu, N is any integer, which means to undo N operations, the same below. (u: undo)
  2. [N]U # Undo the entire operation
  3. Ctrl+r # Cancel the last u command (r: redo)
  4. Ctrl+R # Rewind the previous command

Find and replace

Find command in normal mode

  1. /pattern # Search pattern from the cursor to the end of the file
  2. ?pattern # Search pattern from the cursor to the head of the file
  3. n # Search down for matching content
  4. N # Search forward
  5. % # Matching bracket movement, including (), {}, []. Combining the following two commands is quite powerful for programmers (premise: you need to move the cursor to the parentheses first)
  6. * # Search down the word under the cursor
  7. # # Search forward for the word under the cursor
  8. f{char} # Search backward for the first character of the current line as {char}, 2fv can find the second character of v
  9. F{char} # Search forward for the first character in the current line that is {char}
  10. t{char} # Search backward before the first character in the current line that is {char}
  11. T{char} # Search forward before the first character in the current line that is {char}
  12. ; # Repeat the last character search command (f/t command)
  13. , # Reverse the direction to find the last character search command (f/t command)
  14. tx # Search the current line before the specified string
  15. fx # Search the current line to the specified string
  16. <Esc> # Abandon the search. For example, after starting the f command, I found that I wanted to use the F command, and the <Esc> exit key gave up the search

Note: The key can abort most commands.

Replace command in normal mode

  1. :[range]s[ubstitute]/{pattern}/{string}/[flags]

Parameter Description:

  • pattern:It is the string to be replaced, which can be represented by regexp.
  • string:Replace pattern by string.
  • [range]:There are the following values.
[range] value Description
null Default cursor line
. The current line where the cursor is
N Line N
$ the last line
‘a Mark the line where a (has been marked with ma before)
$-1 The penultimate row, you can add or subtract a certain value to a certain row to obtain a certain row
1,10 Line 1~10
1,$ First line to last line
1,. First line to current line
.,$ Current line to last line
‘a,’b Mark the line where a is located to the line where the mark b is located (marked with ma, mb before)
% All rows (equivalent to 1, $)
?str? Search upwards from the current position and find the first line of str (str can be regular)
/str/ Search down from the current position to find the first line of str (str can be regular)

Note that all the above representation methods for range can be used to set the relative offset through + and-operations.

  • [flags] has the following values:
[flags]value Description
g Replace all matches (global) in the specified range
c Ask the user to confirm before replacing (confirm)
e Ask the user to confirm before replacing (confirm)
i not case sensitive
null Only replace the first match in the specified range

For example:

  1. :s/p1/p2/g # Replace all p1 with p2 in the current line
  2. :%s/p1/p2/g # Replace all p1 with p2 in the current file
  3. :%s/p1/p2/gc # Replace all p1 with p2 in the current file, and ask you whether to replace it everywhere
  4. :10,20s/p1/p2/g # Replace all p1 in lines 10 to 20 with p2
  5. :%s/1\\2\/3/123/g # Replace "1\2/3" with "123" (special characters are marked with backslashes)
  6. :%s/\r//g # Delete DOS line break ^M
  7. :%s///gn # Count the number of matches in a pattern
  8. :%s/^\s*$\n//g # Delete all blank lines in the Vim open file
  9. :g/^\s*$/d # Delete all blank lines in the Vim open file
  10. :%s/^M$//g # Delete the explicit ^M symbol in the Vim file (operating system line break problem)

Visual mode

Note: In Vim visual mode, you can select an editing area, and then perform operations such as inserting, deleting, replacing, and changing the case of the selected file content.

  1. v # Switch to character-oriented visual mode
  2. V # Switch to line-oriented visual mode
  3. Ctrl+V # Switch to block-oriented visual mode
  4. > # Increase indent
  5. < # Decrease indent
  6. d # Delete the highlighted text
  7. x # Delete the highlighted text
  8. c # Rewrite the text, that is, delete the highlighted text and enter the insert mode
  9. s # Rewrite the text, that is, delete the highlighted text and enter the insert mode
  10. y # Copy text
  11. ~ # Convert case
  12. o # Jump to the other end of the marked area
  13. O # Jump to the other end of the marker block
  14. u # Marked area converted to lower case
  15. U # Convert marked area to uppercase
  16. gv # Reselect the last highlighted selection
  17. g Ctrl+G # Show statistics of the selected area
  18. ggVG # Select full text
  19. <Esc> # Press <Esc> to exit visual mode

In addition: The Vim Normal command can execute commands in the normal mode in the command line mode. When the Normal command is combined with the Vim visualization mode, a lot of repetitive tasks can be completed with few operations.

Comment command

Multi-line comments

  1. Ctrl+v # Enter the command line mode, press Ctrl+v to enter the visual mode, then press j or k to select multiple lines and mark the lines that need to be commented
  2. I # Press the capital letter I, and then insert the comment character, such as #, //
  3. <Esc> # Press the <Esc> key to comment all

Uncomment multiple lines

  1. Ctrl+v # Enter the command line mode, press Ctrl+v to enter the visual mode, press the letter l to select the number of columns horizontally, such as #, // (need to select 2 columns)
  2. j k # Press the letter j, or k to select the comment symbol
  3. d # Press the d key to uncomment all

Complex annotation

  1. :Start line, end line s/^/ comment character /g # Add a comment at the beginning of the specified line
  2. :Start line, end line s/^ comment character //g # Uncomment at the beginning of the specified line
  3. :3,5 s/^/#/g # Comment lines 3-5
  4. :3,5 s/^#//g # Uncomment lines 3-5
  5. :1,$ s/^/#/g # Annotate the entire document
  6. :1,$ s/^#//g # Uncomment the entire document
  7. :%s/^/#/g # Annotate the entire document, this method is faster
  8. :%s/^#//g # Uncomment the entire document

Open file

  1. vim . # Open the file manager, display the catalog file, edit by selecting the file
  2. vim filename # Open or create a new file, and place the cursor at the beginning of the first line
  3. vim + filename # Open the file and place the cursor at the beginning of the last line
  4. vim +n filename # Open the file and place the cursor at the beginning of line n
  5. vim -c cmd file # Before opening the file file, execute the specified Vim command cmd
  6. vim -b file # Open the file in binary mode, some special characters (such as line break ^M) can be displayed in this mode
  7. vim -d file1 file2 # Use Vim to open file1 and file2 at the same time and diff the difference between the two files
  8. vim -r filename # The system crashed the last time I was editing with Vim, restore the file
  9. vim -R file # Open the file as read-only, but you can still use :wq! to write
  10. vim -M file # The modification function is forcibly closed and cannot be used :wq! Write
  11. vim -o file1 file2 # When you want to open a Vim file in the terminal, split and display multiple files horizontally
  12. vim -O file1 file2 # When you want to open a Vim file in the terminal, split and display multiple files vertically
  13. vim -x file # Open the file encrypted
  14. vim +/target file # Open file and move the cursor to the first target string found

Save and exit

Note: Save and exit in normal mode.

  1. :w # Write the file and save it, the time stamp of the file will be modified
  2. :w {filename} # Save file by name
  3. :w !sudo tee % # Save the file with super user privileges, you can also do this :w !sudo tee%> /dev/null
  4. :wa # Save all files
  5. :wall # Save all files
  6. :wqall # Save all files and exit
  7. :q # Close the window where the cursor is located and exit (q: quit)
  8. :q! # Force quit (q: quit)
  9. :qa # Abandon all file operations and force exit
  10. :qall # Abandon all file operations and exit
  11. :x # Save the file and exit, the time stamp of the file will not be modified
  12. ZZ # Save the changed file, and close the exit window
  13. ZQ # Close the window without saving the file

File operations

  1. :e[dit] {filename} # Open the file and edit, open the file by the absolute or relative path of the file, Tab key to complete the path
  2. :e[dit] . # Open the file manager, browse the files in the current directory, select and edit
  3. :e[dit] # Reload current file
  4. :E[xplore] # Open the file manager, and display the directory where the active buffer is located
  5. :saveas {filename} # Save as specified file
  6. :o {filename} # Open another file in the current window (o: open)
  7. :r {filename} # Read the file and insert the content after the cursor
  8. :r !dir # Capture and insert the output of the dir command after the cursor
  9. :on[ly] # Close other windows except the window where the cursor is located, same as Ctrl+W o
  10. :clo[se] # Close the file in the window where the cursor is, the same as Ctrl+W c
  11. :cd {path} # Switch Vim current path
  12. :cd - # Go back to the last current directory
  13. :pwd # Show Vim current path
  14. :n[ew] {filename} # Open a new window to edit the new file filename
  15. :new # Open a new window to edit a new file
  16. :ene[w] # Create a new file in the current window
  17. :vnew # Edit the new file in a new window divided into left and right
  18. :tabnew # Edit the new file in a new tab
  19. :fin[d] {file} # Find the file {file} in path and edit it
  20. :f[ile] # Display the current file name and cursor position
  21. :f[ile] {name} # Set the current file name to name
  22. :files # Show all alternate file names

Opened file operation

  1. :ls # Investigation cache list
  2. :bn # Switch to the next cache
  3. :bp # Switch to the previous cache
  4. :bd # Delete cache
  5. :b 1 # Switch to cache 1
  6. :b abc # Switch to the cache whose file name starts with abc
  7. :badd {filename} # Add files to the cache list
  8. :set hidden # Set hidden mode (unsaved cache can be switched away or closed)
  9. :set nohidden # Turn off the hidden mode (unsaved cache cannot be switched away or closed)
  10. n Ctrl+^ # To switch the cache, enter the numeric cache number first, then press Ctrl+6

Multi-window operation

The split-screen window is based on the Ctrl+W shortcut key, Ctrl is the control function key, W stands for Windom, and Ctrl+W stands for control window.

  1. :sp {filename} # Split the window horizontally and open the file in a new window filename
  2. :vs {filename} # Split the window vertically and open the file in a new window filename
  3. :split # Copy the current window to a horizontal window, the content is synchronized, the cursor can be different
  4. :vsplit # Copy the current window to another vertical window, the content is synchronized, the cursor can be different
  5. Ctrl+W # Switch to the next window
  6. Ctrl+W s # Horizo​​ntal split window
  7. Ctrl+W v # Split window longitudinally
  8. Ctrl+W w # Cycle to the next window
  9. Ctrl+W W # Cycle to the previous window
  10. Ctrl+W p # Skip to the last visited window
  11. Ctrl+W r # reverse window
  12. Ctrl+W c # Close the current window, but cannot close the last window
  13. Ctrl+W q # Exit the current window, if it is the last window, close vi
  14. Ctrl+W o # Keep only the active window, close other (o: other) windows, same as :on[ly]
  15. Ctrl+W h # Jump to the left window
  16. Ctrl+W j # Jump to the window below
  17. Ctrl+W k # Jump to the upper window
  18. Ctrl+W l # Jump to the right window
  19. Ctrl+W + # Increase the row height of the current window, you can add a number in front
  20. Ctrl+W - # Decrease the row height of the current window, you can add a number in front
  21. Ctrl+W < # Reduce the column width of the current window, you can add a number in front
  22. Ctrl+W > # Increase the column width of the current window, you can add a number in front
  23. Ctrl+W = # Make all windows the same width and height
  24. Ctrl+W H # Move the current window to the far left
  25. Ctrl+W J # Move the current window to the bottom
  26. Ctrl+W K # Move the current window to the top
  27. Ctrl+W L # Move the current window to the far right
  28. Ctrl+W x # Exchange window
  29. Ctrl+W f # Open the file named under the cursor in a new window
  30. Ctrl+W gf # Open the file named under the cursor in a new tab
  31. Ctrl+W T # Move the current window to a new tab
  32. Ctrl+W P # Jump to the preview window
  33. Ctrl+W z # Close preview window
  34. Ctrl+W _ # Maximize the current window vertically
  35. Ctrl+W | # Maximize the current window horizontally

Tags

  1. :tabs # Show all tabs
  2. :tabe {filename} # Open the file filename in a new tab
  3. :tabn[ext] # Next tab
  4. :tabp[revious] # Previous tab
  5. :tabc[lose] # Close current tab
  6. :tabo[nly] # Close other tabs
  7. :tabn N # Switch to the Nth tab page, for example: tabn 3 Switch to the third tab page
  8. :tabm n # Label move
  9. :tabfir[st] # Switch to the first tab
  10. :tabl[ast] # Switch to the last tab
  11. :tab help # Open help in tab
  12. :tab drop {file} # If the file has been opened by other tabs and windows, skip over, otherwise open a new tab
  13. :tab split # Open the file in the current window in a new tab
  14. :tab ball # Open all files in the cache with tabs
  15. :set showtabline=? # Set to 0 to not display the tab page label, 1 will be displayed on demand, 2 will be permanently displayed
  16. Ngt # Switch to the Nth tab page, for example, 2gt will switch to the second tab page
  17. gt # Next tab
  18. gT # Previous tab

Vim bookmarks

  1. :marks # Show all bookmarks
  2. ma # Save the current position to bookmark a, the lowercase letter of the book signature is in the file, and the uppercase global
  3. 'a # Jump to the line of bookmark a
  4. `a # Jump to the location of bookmark a
  5. `. # Jump to the last edited line
  6. 'A # Jump to full text bookmark A
  7. [' # Jump to the previous bookmark
  8. ]' # Jump to the next bookmark
  9. '< # Jump to the beginning of the last visual mode selection area
  10. '> # Jump to the end of the last visual mode selection area

Spell Check

  1. :set spell # Turn on spell check
  2. :set nospell # Turn off spell check
  3. ]s # The next misspelled word
  4. [s # The last misspelled word
  5. zg # Add words to the spelling vocabulary
  6. zug # Undo the last word added
  7. z= # Spelling suggestion

Code folding

  1. zf{motion} # Operator, manually define a fold (f:fold)
  2. :{range}fold # Define the lines included in the range {range} as a fold
  3. zf # Create code folding
  4. zF # Specify the number of rows to create a fold
  5. za # Toggle fold
  6. zA # Switch folding recursively
  7. zc # Close a fold under the cursor (c: close)
  8. zC # Close all folds under the cursor (C: Close)
  9. zd # Delete the fold under the cursor
  10. zD # Recursively delete all folds
  11. zE # Delete all folds
  12. zi # Toggle fold
  13. zm # All codes are folded one level
  14. zM # Fold all code, set foldlevel=0, set foldenable
  15. zr # All codes open one layer
  16. zR # Open all code and set foldlevel to the maximum
  17. zn # Fold none, reset foldenable and open all codes
  18. zN # Fold Normal, reset foldenable and restore all folds
  19. zo # Open a layer of code
  20. zO # Turn on all code folding under the cursor

Document encryption/decryption

The document is encrypted. When you open the file, you will be prompted to enter the password twice in the lower left corner of the screen before you can operate. After saving the file and exiting, you must enter the normal password to open the file correctly, otherwise garbled characters will be displayed.

  1. vim -x {filename} # Enter the encryption password and confirm the password again. Note: Save the content without modifying it, otherwise the password setting will not take effect
  2. :X # Enter the encryption password in command mode and confirm the password again. Note: Save the content without modifying it, otherwise the password setting will not take effect
  3. :set key=password # Enter the encryption password in command mode and confirm the password again. Note: Save the content without modifying it, otherwise the password setting will not take effect

Document decryption, set decryption by command mode.

  1. :X # In command mode, directly press Enter to indicate that the password is empty. Note: Save the content without modifying it, otherwise the decryption setting will not take effect
  2. :set key= # Set the key password to be empty in command mode. Note: Save the content without modifying it, otherwise the password setting will not take effect

Macro recording

Macro is the function of recording and playback. It is an integration of a series of Vim command operations. Using macros can achieve a lot of repetitive work.

  1. qa # Start recording the macro named a
  2. q # End recording macro
  3. @a # Play the macro named a
  4. @@ # Play the previous macro
  5. @: # Repeat the last ex command (colon command)

Macro example:You need to type a Tab key at the beginning of the following multi-line text to indent the beginning of the line.

  1. set nu
  2. set tabstop=4
  3. set shiftwidth=4
  4. set softtabstop=4
  5. set autoindent
  6. set wrap
  7. syntax on

Record macro

  • Move the cursor to the first line first.
  • In Normal mode, press the q key and a letter to start recording. For example, press qa to register the macro as a.
  • Press I to insert at the beginning of the line, and press Tab in edit mode. Press to return to Normal mode.
  • Press the j key to move the cursor to the next line.
  • Press the q key to finish recording.

Use macro

  • Using the macro a recorded above, press @a to play the macro named a.
  • Move the cursor to the second line in Normal mode, press @a, and use macro a again.
  • Press N@a for multiple operations, where N is a positive integer, which means that the macro is executed N times. For example, move the cursor to line 3, operate macro a on the remaining 5 lines, and press 5@a.

The above recording macro, using macro two common operations, complete the beginning of multiple lines of text, type a Tab key to indent the beginning of the line!

Other commands

  1. ga # Display the ASCII code or Unicode code of the character under the cursor
  2. g8 # Display the UTF-8 encoding byte order of the character under the cursor
  3. gi # Go back to the place where you entered the insert last time, and switch to insert mode
  4. K # Query the help of the word under the cursor
  5. Ctrl+G # Display the name of the file being edited, as well as size and location information
  6. g Ctrl+G # Display file size, number of characters, number of words and number of lines, also available in visual mode
  7. Ctrl+PgUp # On the last tab page, GVim OK, some terminal software needs to set the corresponding keyboard code
  8. Ctrl+PgDown # The next tab page, GVim OK, some terminal software needs to set the corresponding keyboard code
  9. Ctrl+R Ctrl+W # Insert the word under the cursor in command mode
  10. Ctrl+Insert # Copy to the system clipboard (GVIM)
  11. Shift+Insert # Paste the contents of the system clipboard (GVIM)
  12. Ctrl+X Ctrl+E # Scroll up in insert mode
  13. Ctrl+X Ctrl+Y # Scroll down in insert mode
  14. :map # To view the map shortcuts of the current Vim configuration
  15. :inoremap # To view the inoremap shortcuts of the current Vim configuration
  16. :nnoremap # To view the nnoremap shortcuts of the current Vim configuration
  17. :set ff=unix # Set change behavior unix
  18. :set ff=dos # Set change behavior dos
  19. :set ff? # View line break settings
  20. :set nohl # Clear search highlight
  21. :set termcap # See what will be received from the terminal and what commands will be sent to the terminal
  22. :set guicursor= # Solve the problem of some strange characters in NeoVim in SecureCRT/PenguiNet
  23. :set t_RS= t_SH= # Solve the strange characters in the Vim8.0 terminal function in SecureCRT/PenguiNet
  24. :set fo+=a # Enable real-time automatic formatting of text segments
  25. :earlier 15m # Go back to the contents of the file 15 minutes ago
  26. :.!date # Insert time in current window
  27. :%!xxd # Start binary editing
  28. :%!xxd -r # Save binary edit
  29. :r !curl -sL {URL} # After reading the URL content and adding it to the cursor
  30. :g/^\s*$/d # Delete blank lines
  31. :g/green/d # Delete all lines containing green
  32. :v/green/d # Delete all lines that do not contain green
  33. :g/gladiolli/# # Search words and print the results, and add the line number before the results
  34. :g/ab.*cd.*efg/# # Search for lines containing ab, cd and efg, print the result and line number
  35. :v/./,/./-j # Compress blank lines
  36. :Man bash # View man in Vim, first call :runtime! ftplugin/man.vim to activate
  37. /fred\|joe # Search for fred or joe
  38. /\<\d\d\d\d\> # Search exactly four numbers
  39. /^\n\{3} # Search for three consecutive blank lines

History commands

History command format:

  1. :his[tory] [{name}] [{first}][, [{last}]]

Parameter Description:

  • {name}:Specifies the history type.
  • {first}:Specifies the starting position of the command history, defaults to the first record.
  • {last}:Specifies where the command history ends, defaults to the last record.

In command line mode.

  1. :his[tory] # View the history of all commands entered in the command line mode
  2. :his[tory] all # Show all types of history
  3. :history c 1,5 # List the first to fifth command line history
  4. :history search or / or # View search history
  5. :call histdel("") # delete history
  6. :help :history # See help for the :history command

In normal mode.

  1. q/ # View search history used q/ entered
  2. q? # View usage q? Entered search history
  3. q: # View command line history

Register

View register value.

  1. :reg # View all register values
  2. :reg {register} # View the specified register value

Recall register value.

  1. "{register} # Recall register value in normal mode
  2. :Ctrl+r "registerName # After entering Ctrl+r in command mode, Vim will automatically type "register reference symbol
  3. Ctrl+r registerName # In insert mode (no need to enter register reference symbol ")

Vim register classification

Register name Citation method Description
Unnamed register “” The default register, all copy and modify operations (x, s, d, c, y) will copy the data to the unnamed register
Named register “a - “z or “A - “Z {register} can only be one of 26 English letters, from a-z, A-Z register contents will be merged into the corresponding lowercase letters
Copy special register “0(Number 0) Only when the copy operation (y) is used, the data will be copied to the unnamed register and the copy special register at the same time
Numbered register “1 - “9 All data without ranges (‘(‘,’)’,’{‘,’}’) and operations involving more than 1 line of delete and modify operations (x, s, d, c) will be copied to the stepwise temporary cache register , And when new data is added, it progresses step by step. The data of 1 is copied to 2, 2 to 3, and the contents of the last 9 registers will be deleted
Black hole register “_ Almost all the data involved in the operation will be copied to the register. If you want the data to be operated not to pass through the register, you can specify a black hole register. The data will disappear when the register arrives, and it cannot be displayed and does not exist.
System clipboard “+ or “* When interacting with the GUI external to Vim, you need to use a special system clipboard
Expression register “= The most special one of all registers is used to calculate expressions. After entering the register application, it will prompt “=” in the command line, enter the expression as needed, and the result will be displayed at the cursor
Other registers - -

Vim configuration file

Note: Vim configuration files are available in global and user versions, and user configuration files take precedence over global system configuration files.

  1. :ve[rsion] # Check the Vim version, and also check the priority order and location of Vim loading configuration files
  2. :echo $MYVIMRC # Use this command in Vim command mode to output the location of the Vim configuration file
  3. :edit $MYVIMRC # Use this command to open the Vim configuration file in Vim command mode
  4. :so[urce] $MYVIMRC # After the Vim configuration file is changed, use this command to load the new configuration options. If the vimrc file happens to be the currently active buffer, this command can be simplified to: so %.
  5. :echo $VIM # Output the location of the global vimrc configuration file, stored in the Vim installation directory
  6. :echo $HOME # Output the location of the user vimrc configuration file, stored in the user's home directory

Vim configuration instructions, please refer to vimrc configuration file for details. Note: Vim configuration can be set individually in command mode and only takes effect in the current window!

  1. syntax # List the defined grammar items
  2. syntax clear # Clear defined grammar rules
  3. syntax on # Allow syntax highlighting
  4. syntax off # Prohibit syntax highlighting
  5. set history=200 # Record 200 historical commands
  6. set bs=? # Set Backspace key mode, modern editor is :set bs=eol,start,indent
  7. set sw=4 # Set the indent width to 4
  8. set ts=4 # Set the tab width to 4
  9. set noet # Set not to expand Tab to space
  10. set et # Set expand Tab to space
  11. set winaltkeys=no # Set the normal capture of the Alt key under GVim
  12. set nowrap # Turn off word wrap
  13. set ttimeout # Allow terminal key detection to time out (the function key under the terminal is a series of scan codes starting with Esc)
  14. set ttm=100 # Set the terminal key detection timeout to 100 milliseconds
  15. set term=? # Set the terminal type, such as the common xterm
  16. set ignorecase # Set whether the search ignores case
  17. set smartcase # Smart case, ignore case by default, unless the search content contains uppercase letters
  18. set list # Set to display tabs and line breaks
  19. set nu # Set the display line number, you can use :set nonu to prohibit the display line number
  20. set number # Set the display line number, you can use :set nonumber to prohibit the display line number
  21. set relativenumber # Set display relative line number (distance between other lines and current line)
  22. set paste # Enter paste mode (disable indentation when pasting and other things that affect formatting)
  23. set nopaste # End paste mode
  24. set spell # Allow spell checking
  25. set hlsearch # Set highlight search
  26. set ruler # Always show cursor position
  27. set nocompatible # The setting is not compatible with the original vi mode (must be set at the very beginning)
  28. set incsearch # Dynamic incremental display of search results during search input
  29. set insertmode # Vim is always in insert mode, use Ctrl+o to execute commands temporarily
  30. set all # List all option settings

Vim plugins

vim-commentary:Vim batch comment tool, you can comment multiple lines and remove multiple lines of comments.

  1. gcc # Comment the current line
  2. gc{motion} # Annotate the area marked by {motion}, such as gcap annotate the entire paragraph
  3. gci{ # Comment the content in braces
  4. gc # Press gc to annotate the selected area under Visual Mode
  5. :7,17Commentary # Comment lines 7 to 17

NERDTree:This plugin is used to list the directory tree of the current path.

  1. ? # Quick help document
  2. o # Open a directory or open a file, create a buffer, can also be used to open bookmarks
  3. go # Open a file, but the cursor remains in NERDTree, creating a buffer
  4. t # Open a file and create a Tab, which is also effective for bookmarks
  5. T # Open a file, but the cursor still stays in NERDTree. Tab is created, which is also effective for bookmarks
  6. i # Split the window to create the file horizontally, creating a buffer
  7. gi # Split the window where the file was created horizontally, but the cursor remains in NERDTree
  8. s # Split the window that creates the file vertically, creating a buffer
  9. gs # Similar to gi and go
  10. x # Collapse the currently open directory
  11. X # Collapse all open directories
  12. e # Open the selected directory by file management
  13. D # Delete bookmark
  14. P # Uppercase, jump to the current root path
  15. p # Lowercase, jump to the previous path where the cursor is located
  16. K # Jump to the first subpath
  17. J # Jump to the last subpath
  18. C # Set the root path to the directory where the cursor is
  19. u # Set the parent directory as the root path
  20. U # Set the parent directory to follow the path, but keep the original directory open
  21. r # Refresh the directory where the cursor is
  22. R # Refresh the current root path
  23. I # Show or not show hidden files
  24. f # Turn file filters on and off
  25. q # Close NERDTree
  26. A # Show NERDTree in full screen, or close full screen
  27. Ctrl+j Ctrl+k # Move between directories and files at the same level, ignoring subdirectories and subfiles

asyncrun.vim:The plug-in uses the asynchronous mechanism of Vim 8 / NeoVim, allowing you to run Shell commands in the background and display the results in Vim’s Quickfix window in real time.

  1. :AsyncRun ls # Run the command ls asynchronously and output the result to quickfix. Use :copen to view
  2. :AsyncRun -raw ls # Run the command asynchronously, the result of ls does not match errorformat

Vim mode

  1. Normal mode # Press <Esc> or Ctrl+[ to enter, the file name is displayed in the lower left corner or empty
  2. Insert mode # Press i to enter, the lower left corner shows --INSERT--
  3. Visual mode # Press v to enter, the lower left corner shows --VISUAL--
  4. Replacement mode # Press r or R to start, the lower left corner shows --REPLACE--
  5. Command line mode # Press : or / or ? To start

External command

  1. :!{command} # Execute a one-time Shell command, the following command: :!pwd, change the directory in the current Vim mode
  2. :!! # Re-execute the most recently run command
  3. :sh[ell] # Start an interactive Shell to execute multiple commands, the exit command exits and returns to Vim
  4. :!ls # Run the external command ls and wait for the return
  5. :r !ls # Capture the output of the external command ls and insert it after the cursor
  6. :w !sudo tee % # sudo saves the current file in the future, it can also be like this :w !sudo tee % > /dev/null
  7. :call system('ls') # Call the ls command, but do not display the returned content
  8. :!start notepad # Start Notepad under Windows, you can add silent at the top
  9. :sil !start cmd # Open cmd in the current directory under Windows
  10. :%!prog # Run a text filter program, such as sorting JSON format:%!python -m json.tool

Quickfix window

  1. :copen # Open the quickfix window (view compilation, grep and other information)
  2. :copen 10 # Open the quickfix window and set the height to 10
  3. :cclose # Close the quickfix window
  4. :cfirst # Jump to the first error message in quickfix
  5. :clast # Jump to the last error message in quickfix
  6. :cc [nr] # View error [nr]
  7. :cnext # Skip to the next error message in quickfix
  8. :cprev # Jump to the previous error message in quickfix

Help information

  1. :h[elp] {command} # To display the help of related commands, you can also enter :help instead of the command. To exit the help, you need to enter :q
  2. :h tutor # Getting started document
  3. :h quickref # Quick help
  4. :h index # Query all keyboard command definitions in Vim
  5. :h summary # Help you better use the built-in help system
  6. :h Ctrl+H # Query what Ctrl+H does in normal mode
  7. :h i_Ctrl+H # Query what does Ctrl+H do in insert mode
  8. :h i_<Up> # Query what is on the arrow keys in insert mode
  9. :h pattern.txt # Regular expression help
  10. :h eval # Scripting help
  11. :h function-list # View the list of functions in VimScript
  12. :h windows.txt # Window help
  13. :h tabpage.txt # Help on using tabs
  14. :h +timers # Show help for the +timers feature
  15. :h :! # See how to run external commands
  16. :h tips # View the documentation of common techniques built into Vim
  17. :h set-termcap # See how to set the key scan code
  18. :viu[sage] # Displays help for common commands. The purpose is to simulate the corresponding Nvi commands
  19. :exu[sage] # Displays help for the Ex command. The purpose is to simulate the corresponding Nvi commands
  20. :ve[rsion] # View the Vim version, and also view the priority and location of the Vim load configuration file

Internet resources

Vim usage suggestions

  • Never use Ctrl+C instead of . It has a completely different meaning and is easy to mistakenly interrupt the running background script.
  • Many people use Ctrl+[ instead of , the little finger of the left hand is Ctrl, and the little finger of the right hand [ is very convenient after being proficient.
  • If you see strange characters when using Vim 8 embedded terminal in some terminals, use :set t_RS= t_SH= to solve it.
  • If you see strange characters when using NeoVim in some terminals, use :set guicursor= to solve it.
  • Use ciw, ci[, ci”, ci( and diw, di[, di”, di( commands to quickly rewrite/delete text.
  • When moving the cursor left or right in the line, use w b e or W B E instead of h l or the arrow keys, which will be much faster.
  • Shift is equivalent to moving the accelerator key, w b e moves the cursor very slowly, but W B E moves very fast.
  • You should be good at summarizing new skills, such as moving to a non-blank character at the beginning of a line, using the 0w command is easier to enter than the ^ command.
  • Use the dip command on a blank line to delete all adjacent blank lines, and viw can choose continuous blanking.
  • It is much more convenient to use >8j >} ap =i} == when indenting.
  • In insert mode, when you find a word is wrong, you should use Ctrl+W. This is faster than .
  • The y d c command can be a good combination of f t and /X such as dt) and y/End.
  • The c d x command will automatically fill the register “1 to “9, and the y command will automatically fill the “0 register.”
  • When using the v command to select text, you can use 0 to make a U-turn selection, which is sometimes useful.
  • When writing an article, you can write a code block, and then select it and execute it:! The python code block will be replaced with the result.
  • After searching, you often use :nohl to eliminate the highlight. It is used very frequently and you can map it to .
  • When searching, you can use Ctrl+R Ctrl+W to insert the word under the cursor, and the command mode can also be used in this way.
  • When mapping keys, noremap should be used by default, and map should only be used when specifically needed.
  • After copying the text with y, press Ctrl+R in the command mode and then press the double quotation mark 0 to insert the previously copied content.
  • GVim under Windows can set set rop=type:directx,renmode:5 to enhance the display.
  • When you feel that doing something is inefficient, you should stop and think about the correct and efficient way to do it.

Books

Practical Vim(English version) | Practical Vim(Chinese Version)

Vim keyboard diagram

Reference