1. Several modes of Vim

  • Normal mode: You can use the shortcut key to run a command, or press: to enter a command line.
  • Insert mode: You can enter text. In normal mode, press I, A, or O to enter the insert mode.
  • Visual mode: Press V in normal mode to enter visual mode. In visual mode, move the cursor to select text. Press V to enter visual line mode, always full line selection. CTRL + V to enter visible block mode.
  • Replacement mode: In normal mode, press R to enter.

2. Start the Vim

  • vim -c cmd file: Run the specified command before opening the file.
  • vim -r file: Restores the last abnormal exit file.
  • vim -R file:: Opens a file in read-only mode, but can be forced to save;
  • vim -M file: Files are opened in read-only mode and cannot be forcibly saved.
  • vim -y num file: Set the size of the edit window to num lines;
  • vim + file:: starts at the end of the file;
  • vim +num file:: start at line num;
  • vim +/string file: Open file and hover the cursor over the first string found.

    *vim --remote file: opens the specified file using the existing vim process. This is useful if you don’t want to enable multiple VIM sessions. But be aware that if you use Vim, you’ll be looking for a server named Vim; If you already have gvim running, you can use itgvim --remote fileOpen the file in an existing Gvim.

3. Document operations

  • :e file: Closes the currently edited file and opens a new file. Vi warns if changes to the current file are not saved.
  • :e! file: Abandons the modification of the current file and edits the new file.
  • :e+file: Starts a new file and edits it from the end.
  • :e+n file: starts a new file and edits from line NTH.
  • :enew: Compiles a new unnamed document. (CTRL-W n)
  • :e: Reloads the current document.
  • :e!Reload the current document and discard the changes.
  • : e# or CTRL + ^: Go back to the file you just edited, very useful.
  • : f or CTRL + g: Displays the document name, whether to modify, and cursor position.
  • :f filename: Changes the file name of the edited file, saving it at this time is equivalent to saving as.
  • gf: Opens the file whose name is the string where the cursor is located.
  • :w: Saves the modification.
  • :n1,n2w filename: Selectively saves the contents from one n1 line to another n2 line.
  • :wq: Save and exit.
  • ZZ: Save and exit.
  • :x: Save and exit.
  • :q[uit]— Exits the current window. (Ctrl-w Q or Ctrl-W Ctrl-q)
  • :saveas newfilename: save as
  • :browse e: opens a file browser that lets you select the file to edit. If it is a terminal, it will open the netrW file browsing window; In the case of Gvim, a graphical browsing window opens. Actually: Browse can be followed by any command to edit the document, such as sp, etc. The start directory opened with Browse can be set by Browsedir:
  • :set browsedir=last: Use the directory you accessed last time (default);
  • :set browsedir=buffer: Uses the current file directory;
  • :set browsedir=current: Using the current working directory;
  • :Sex: Split a window horizontally to browse the file system.
  • :Vex: Vertically split a window to view the file system.

4. Cursor movement

4.1 Basic Movement

The following moves are in normal mode.

  • hTo move one character to the left;
  • lOr space: move one character right;
  • j: Move down one line;
  • k: Move up one line;
  • gj: Moves to the next line of a paragraph;
  • gk: Move to the upper line of a paragraph;
  • +orEnter: Moves the cursor to the first non-white space character on the next line.
  • -: Moves the cursor to the first non-white space character on the top line.
  • w: Move one word forward and the cursor stops at the beginning of the next word.
  • W: Move the beginning of the next word, but ignore some punctuation;
  • e: Move one word forward, stopping the cursor at the end of the next word;

    *E: Move to the end of the next word, if there is punctuation at the end, move to punctuation;

    *b: Move the cursor back one word to the beginning of the previous word.
  • B: Move to the beginning of the previous word, ignoring some punctuation;

    *ge: Move the cursor one word back to the end of the previous word.
  • gE: same as GE, except that ‘word’ contains punctuation marks adjacent to words.

    *(: Move forward one sentence.
  • ): Move back 1 sentence.
  • {: Move forward one paragraph.
  • }: Move back 1 paragraph.
  • fc: Moves the cursor to the next C character on the same line
  • Fc: Moves the cursor to a c character on the same line
  • tc: Moves the cursor to the next C character on the same line
  • Tc: Moves the cursor after a c character on the same line
  • ;: Used with F & T, repeat once
  • .: With f & T, repeat in reverse

    All the above operations can be used with N. For example, if you enter 3h in normal mode (described below), the cursor moves 3 characters to the left.
  • 0: moves to the top of the line.
  • g0: Moves to the beginning of the line on the screen where the cursor is.
  • ^: moves to the first non-whitespace character on the line.
  • g^: same as ^, but moved to the first non-null character on the current screen line.
  • $: moves to the end of the line.
  • g$: Moves the cursor to the end of the screen line.
  • n|: Move the cursor to the n column.
  • nG: to line n of the file.
  • :n<cr>: Move to line n.
  • :$<cr>: Moves to the last line.
  • H: Move the cursor to the top line of the screen.
  • M: Move the cursor to the middle line of the screen.
  • L: Moves the cursor to the bottom line of the screen.
  • gg: to the file header.
  • G: to the end of the file.

4.2 double screen

  • ctrl+f: Scroll down a screen.
  • ctrl+b: Scroll up a screen.
  • CTRL + D: Scroll down half screen.
  • ctrl+u: Scroll up half screen.
  • CTRL + E: Scroll down one line.
  • CTRL + Y: Scroll up one line.
  • ‘n%’ : to file n% location.
  • zz: Moves the current row to the center of the screen.
  • zt: Moves the current line to the top of the screen.
  • zb: Moves the current line to the bottom of the screen.

4.3 mark

Use tags to move quickly. Once you reach the mark, you can press Ctrl+ O to return to the original position. Ctrl+ O and Ctrl+ I are very much like back and forward on the browser.

  • m{a-z}: Marks the location of the cursor, local marks, only for the current file.
  • m{A-Z}: Marks the position of the cursor, global mark. After marking, exit Vim, restart, and the marking is still valid.
  • {a-z}: Moves to the marked position.
  • ‘{a-z} ‘: moves to the beginning of the marked line.
  • {0-9}: Returns to the last position left when vim was closed [2-10] times before.
  • : Moves to the position of the last edit. "That's fine, butRight down to the column, right down to the row. To jump to an older location, press C-O. To jump to a newer location, use C-I.
  • Move to the place you left last time.
  • ‘. ‘: Move to the last change.
  • :marks: Displays all tags.
  • :delmarks a b: Deletes tags A and B.
  • :delmarks a-c: Deletes tags A, B, and C.
  • :delmarks a c-f: Deletes markers A, C, D, e, and f.
  • :delmarks!: Deletes all flags in the current buffer.
  • :help mark-motions: Read more about Mark.

5. Insert text

5.1 Basic Insertion

  • i: Insert before the cursor; A quick tip: Press 8, then I to enter insert mode, type =, press Esc to enter command mode, and 8 = will appear. This is useful when inserting dividers, such as 30i+, which inserts 36 + dividers.
  • I: inserts before the first non-null character on the current line;
  • gI: inserts in the first column of the current row;
  • a: Insert after the cursor;
  • A: inserts at the end of the current line;
  • o: Create a new line below to insert;
  • O: Insert a new line above;
  • :r filename: Inserts the contents of another file at the current location.
  • :[n]r filename: Inserts the contents of another file at line NTH.
  • :r ! date: Inserts the current date and time at the cursor. Similarly, :r! Command can insert the output of other shell commands into the current document.

5.2 Overwrite Insert

  • c[n]w: Rewrite 1(n) words after the cursor.
  • c[n]l: Rewrites n letters after the cursor.
  • c[n]h: Rewrites n letters before the cursor.
  • [n]cc: Modifies the current [n] line.
  • [n]s: Replaces the 1(n) character after the cursor with the input text, equivalent to C [n] L.
  • [n]S: Deletes a specified number of lines and replaces them with the entered text.

    Note that forms like CNW, DNW and ynw can also be written as NCW, NDW and nyw.

6. Cut copy and register

6.1 Cut, Copy, and Paste

  • [n]x: Cuts n characters to the right of the cursor, equivalent to D [n] L.
  • [n]X: Cuts n characters to the left of the cursor, equivalent to D [n]h.
  • y: Copies the text selected in visual mode.
  • yy or Y: Copies the entire line of text.
  • y[n]w: Copy one (n) word.
  • y[n]l: Copies 1(n) characters to the right of the cursor.
  • y[n]h: Copies 1(n) characters to the left of the cursor.
  • y$: copies from the current cursor position to the end of the line.
  • y0: copies from the current cursor position to the beginning of the line.
  • :m,ny<cr>: copies the contents of lines M to N.
  • Y1G or ygg: Copies all lines above the cursor.
  • yG: Copies all lines below the cursor.
  • yawandyas: Copy a word and a sentence, even if the cursor is not at the beginning of the word and sentence.
  • d: Deletes (cuts) the selected text in visual mode.
  • d$ or D: Deletes (cuts) the current position to the end of the line.
  • d[n]w: Delete (cut) 1(n) words
  • d[n]l: Deletes (cuts) 1(n) character to the right of cursor.
  • d[n]h: Deletes (cuts) 1(n) character left of the cursor.
  • d0: Deletes (cuts) the current position to the top of the line
  • [n] dd: Deletes (cuts) 1(n) lines.
  • :m,nd<cr>: Cuts lines M to N.
  • d1Gordgg: Cuts all lines above the cursor.
  • dG: Cuts all lines below the cursor.
  • dawanddas: Cut a word and a sentence, even if the cursor is not at the beginning of the word and sentence.
  • d/f<cr>: this is an advanced combinatorial command that deletes everything from the current position to the next f.
  • p: Paste after the cursor.
  • P: Paste before the cursor.

6.2 Text Objects

  • aw: one word
  • as: 1.
  • ap: a.
  • ab: block (enclosed in parentheses).
  • y, d, c, v: can be followed by text objects.

6.3 register

  • a-z: can be used as register names. Ayy puts the contents of the current line into register A.
  • A-Z: Index registers with uppercase letters, you can append contents to registers. As in “Ayy appends the contents of the current line to register A.
  • :reg: Displays the contents of all registers.
  • "": Register used by default when no register index is added.
  • "*Currently select buffer,"*yyPuts the contents of the current row into the current selection buffer.
  • "+: System clipboard."+yyPuts the contents of the current line into the system clipboard.

7. Find and replace

7.1 find

  • /something: Look for something in the following text.
  • ? something: Look for something in the previous text.
  • /pattern/+number: Places the cursor on the number line following the line containing pattern.
  • /pattern/-number: Places the cursor on the number line before the line containing pattern.
  • n: Finds the next one backwards.
  • N: Look forward to the next one.
  • You can use grep or vimgrep to find out where a pattern appears,

    Where :grep is to call the external grep program, and :vimgrep is vim’s own search algorithm.

    Usage::vim[grep]/pattern/[g] [j] files

    G means that if a pattern appears more than once in a row, that row also appears more than once in the result.

    J means that after grep, the result stops at the JTH item. By default, it stops at the first item.

    Vimgrep can be preceded by a number to limit the upper limit of the search results, for example

    *:1vim/pattern/ %Only look for the first occurrence of that pattern in this file.

In fact, vimgrep is especially useful when reading plain text ebooks, generating navigable directories. For example, the title of each section in an e-book is n. XXXX. You can do this :vim/^d{1,}./ % and then use :cw or :copen to see the result. You can use c-w H to move the Quickfix window to the left to look more like a directory.

7.2 replace

:s/old/new – Replaces the first old in the current line with new. :s/old/new/g – Replaces all old in the current line with new. :n1,n2s/old/new/g – Replace all old from lines n1 to n2 of file with new. :%s/old/new/g – Replace all old in the file with new. :%s/^/ XXX /g – Insert XXX at the beginning of each line, ^ to indicate the beginning of the line. :%s/$/ XXX /g – Insert XXX at the end of each line, with $indicating the end of the line. Add c to the end of all replacement commands, and each replacement will require user confirmation. For example: %s/old/new/gc, plus I, ignore case. Another more flexible method is to run a command after matching a pattern. The syntax is [range]g/pattern/command. For example, %g/^ xyz/normal dd. Indicates to run the dd command in normal mode for lines starting with a space and xyz. The provisions of range are:

If range is not specified, the current line is represented. M,n: lines from m to N. 0: the first line (possibly). $: last line. : current line % : all lines

7.3 Regular Expressions

Advanced search and replace uses regular expressions.

\ d: it means the decimal number \ s: (I guess) for space \ s: not null character \ a: English letters | : said. Or: said. {m, n} : said m to n characters. This is used with \s and \ A, for example \ A {m,n} denotes m to N letters. {m,} : indicates m to infinitely many characters. ** : all subdirectories in the current directory. Get more help.

8. Typesetting

8.1 Basic Layout

  • <<Indent shiftwidth to the left
  • >>Indent shiftwidth to the right
  • :ce(nter)This line is centered
  • :le(ft)This line is to the left
  • :ri(ght)This line is to the right
  • gqTo rearrange the selected text, that is, to break lines of text that is too long
  • gqqRearrange the current row
  • gqnqRearrangement of n line
  • gqapReorder the current segment
  • gqnapRearrangement n segment
  • gqnjRearrange the current row with the following N rows
  • gqQRearrange the current paragraph to the end of the article
  • JConcatenates the current line and the next line
  • gJJ, but no Spaces are left after the merge.

8.2 Spelling Check

  • :set spell– Enable the spell check function
  • :set nospell– Turn off the spell checker
  • ]s– Move on to the next misspelled word
  • [s– Similar to the previous command, but searches in the opposite direction
  • z=– Displays a list of misspelled words to choose from
  • zg– Tells the spellchecker that the word is spelled correctly
  • zw– Contrary to the previous command, tells the spellchecker that the word is misspelled

8.3 Word Count

  • g ^gYou can count the number of characters and lines in a document. Place the cursor on the last character and subtract the number of lines from the number of characters to get a rough count of the word count of the Chinese document. The above applies to Mac or Unix file formats. If the file format is Windows (that is, the newline has two bytes), the count method is as follows: number of characters – number of lines x 2.

9. Edit multiple files

9.1 Editing Multiple Files at a time

We can open more than one file at a time, such as

  • vi a.txt b.txt c.txt

    Edit the next file using :next(:n).
  • :2nEdit the next two files.

    use:previousor:NEdit the previous file.

    use:wnextSave the current file and edit the next file.

    use:wpreviousSave the current file and edit the previous file.

    use:argsThe file list is displayed.

    :n filenamesor:args filenamesSpecifies a new file list.

    vi -o filenamesEdit multiple files in horizontally split Windows.

    vi -O filenamesEdit multiple files in vertically split Windows.

9.2 Multi-label Editing

  • vim -p files:Open multiple files, each occupying a TAB page.
  • :tabe, tabnewIf the file name is added, open the file in a new label, otherwise open an empty buffer.
  • ^w gfIn a new TAB, open the file specified in the path under the cursor.
  • :tabn— Switch to the next TAB. Control plus PageDown, you can do that.
  • :tabp— Switch to the previous TAB. Control + PageUp, ok.
  • [n] gt— Switch to the next TAB. If n is added, switch to the NTH TAB. The number of the first tag is 1.
  • :tab split— Opens the contents of the current buffer in a new TAB.
  • :tabc[lose]— Closes the current TAB.
  • :tabo[nly]— Close other tabs.
  • :tabs— Lists all tabs and the Windows they contain.
  • :tabm[ove] [N]— Move the TAB to after the NTH TAB. If tabm 0 is the current TAB, it becomes the first TAB.

9.3 the buffer

  • :buffersOr *:lsOr *:filesDisplays a list of buffers.
  • ctrl+^: Switches between the nearest two buffers.
  • :bn— Next buffer.
  • :bp— The previous buffer.
  • :bl— The last buffer.
  • :b[n]Or *:[n]b— Switch to the NTH buffer.
  • :nbw(ipeout)— Delete the NTH buffer completely.
  • :nbd(elete)— Delete the NTH buffer, not actually deleted, still in the unlisted list.
  • :ba[ll]Open all buffers on the current page, one window for each buffer.

10. Split-screen editing

  • vim -o file1 file2: Split the window horizontally, open file1 and file2 at the same time
  • vim -O file1 file2: Vertical split window, open file1 and file2 simultaneously

10.1 Horizontal segmentation

  • :split(:sp)Split the current window horizontally into two Windows. (Ctrl-w S or Ctrl-W Ctrl-s) Note if under terminal, Ctrl-S may freeze terminal, press Ctrl-Q to continue.
  • :split filenameSplit the window horizontally and display another file in a new window.
  • :nsplit(:nsp)Split a window n rows high horizontally.
  • :[N]newHorizontally split a window N lines high and edit a new file. (Ctrl-w n or Ctrl-w Ctrl-n)
  • ctrl+w fSplit a window horizontally and open a file named the word with the cursor in the new window.
  • C-w C-^Split a window horizontally to open the file you just edited.

10.2 Vertical Segmentation

  • :vsplit(:vsp)Split the current window into two horizontally distributed Windows. (Ctrl-w V or CTRL Ctrl-v)
  • :[N]vne[w]Split a new window vertically.
  • :verticalHorizontal split command: corresponding vertical split.

10.3 Closing subWindows

  • :qallClose all Windows and exit vim.
  • :wallSave all modified Windows.
  • :onlyKeep only the current window and close all other Windows. (CTRL-W o)
  • :closeClose the current window, Ctrl-w c will do the same. (Like: Q 😡 also works)

10.4 Resize a Window

  • ctrl+w +— The current window increases by one line. You can also increase n rows by n.
  • ctrl+w -— Decreases the current window by one line. You can also reduce n rows by n.
  • ctrl+w _The current window is expanded as large as possible. You can also set the number of rows with n.
  • :resize n— The current window is n lines high.
  • ctrl+w =— All Windows are the same height.
  • n ctrl+w _The height of the current window is set to N lines.
  • ctrl+w <Reduces the current window by one column. You can also subtract n columns by n.
  • ctrl+w >The current window is widened by one column. You can also use n to widen n columns.
  • ctrl+w |The current window is as wide as possible. You can also set the number of columns with n.

10.5 Switching between and Moving Windows

If the mouse is supported, it is easy to toggle and resize child Windows.

  • ctrl+w ctrl+w: Switches to the next window. Or CTRL + W, W.
  • ctrl+w p: Switches to the previous window.
  • ctrl+w h(l,j,k): Switches to the left (right, down, up) window.
  • ctrl+w t(b): Switch to the top (bottom) window.

  • ctrl+w H(L,K,J): Moves the current window to the far left (right, top, bottom) side.
  • ctrl+w r: Rotation window position.
  • ctrl+w T: Moves the current window to the new TAB.

11. Quick editing

11.1 Changing Case

  • ~: Reverses the case of the character where the cursor is located.

    U or U in visual mode: Changes the selected text to uppercase or lowercase.

    Gu (U) a range (such as $, or G) that converts all letters from the current position of the cursor to lowercase or uppercase. Such as ggguG, is the beginning to the last line between all letters into small. In the case of GU5j, make the current line and the next four all lowercase.

11.2 Replacement (Normal Mode)

  • r: Replaces the character at the cursor, also supporting Chinese characters.
  • R: To enter the replacement mode, press ESC to return to normal mode.

11.3 Undo and Redo (Normal Mode)

  • [n] u: Cancel one (n) change.
  • :undo 5Undo 5 changes.
  • :undolist— Your undo history.
  • ctrl + r: Rework the final changes.
  • U: Cancels all changes in the current line.
  • :earlier 4m— Go back four minutes
  • :later 55s— Forward 55 seconds

11.4 the macro

  • .— Repeat the previous edit action
  • qa: Start recording macro A (keystroke record)
  • q: Stop recording
  • @a: Plays macro A

12. Edit special files

12.1 File Encryption and decryption

  • vim -x file: Starts editing an encrypted file.
  • :XSet the password for the current file.
  • :set key=— Remove the password of the file.

    Here are the more advanced VI skills summarized by Dian Hu.

12.2 File Coding

  • :e ++enc=utf8 filename, tell Vim to open the file using utF-8 encoding.
  • :w ++enc=gbk, whatever the encoding of the current file, save it to GBK encoding.
  • :set fencor:set fileencodingTo view the encoding of the current file.

    Add set FileEncoding = UCS-BOM, UTF-8, CP936 to vIMRC and VIM will select the appropriate encoding based on the file to open. Note: Do not leave Spaces between codes. Cp936 corresponds to the GBK code. Ucs-bom corresponds to the file format in Windows.

    Getting Vim to handle file formats and file encodings correctly depends on the correct configuration of ~/.vimrc

12.3 File Format

There are roughly three file formats: Unix, DOS, MAC. The difference between the three formats mainly lies in the encoding of the carriage return key: DOS is carriage return plus line feed, Unix only line feed, MAC only carriage return.

  • :e ++ff=dos filenameTell Vim to open the file in DOS format.
  • :w ++ff=mac filenameStore the file in MAC format.
  • :set ffTo display the current file format.

    Add set fileformats= Unix, DOS, MAC to vimrc to let vim automatically recognize fileformats.

13. Programming assistance

13.1 Some Keys

  • gd: jumps to the definition of a local variable;
  • gD: Jumps to the definition of the global variable and searches from the beginning of the current file;
  • g;: Last modified place;
  • g,: The next modification;
  • [[: Jumps to the start of the previous function block, requiring a single line of {.
  • ]]To jump to the next function block, a single line of {is required.
  • []: Jumps to the end of the previous function block, requiring a single line of}.
  • ] [: Jumps to the end of the next function block, requiring a single line of}.
  • [{: jumps to the beginning of the current block;
  • ]}: jumps to the end of the current block;
  • [/: jumps to the beginning of the current comment block;
  • ] /: jumps to end of current comment block;
  • %: can not only move to a matching (),{} or [], but can also jump between #if, #else, #endif.

    The parenthesis matching below is useful for programming purposes.
  • ci', di', yi': Modify, cut, or copy the content between ‘.
  • ca', da', ya': Modify, cut, or copy ‘between the contents of, including’.
  • ci", di", yi": Modify, cut, or copy “between the contents.
  • ca", da", ya": Modify, cut, or copy the contents between, including.
  • ci(, di(, yi(: Modifies, cuts, or copies the content between ().
  • ca(, da(, ya(: Modifies, cuts, or copies the content between (), including ().
  • ci[, di[, yi[: Modifies, cuts, or copies the content between [].
  • ca[, da[, ya[: Modifies, cuts, or copies the content between [], including [].
  • ci{, di{, yi{: Modifies, cuts, or copies the contents between {}.
  • ca{, da{, ya{: Modifies, cuts, or copies the contents between {}, including {}.
  • ci<, di<, yi<: Modify, cut, or copy the content between <>.
  • ca<, da<, ya<: Modifies, cuts, or copies the content between <>, including <>.

13.2 ctags

  • ctags -R: Generates tag files. -r means tags are also generated for files in subdirectories
  • :set tags=path/tagsTell Ctags which tag file to use
  • :tag xyz— Skip to the definition of xyz, or place the cursor over xyz and press C-] to return to c-t
  • :stag xyz— Use a split window to display the definition of XYZ, or c-w]. If you use c-w n], a window n lines high will open
  • :ptag xyz— Open xyz definition in preview window with hot key c-w}.
  • :pclose— Close the preview window. The hot bond is C minus W and z.
  • :pedit abc.h— Edit abc.h in the preview window
  • :psearch abc— Searches the current file and the current file include files, showing the line containing ABC.

    Sometimes a tag can have multiple matches, such as function overloading, where a function name can have multiple matches. This will jump to the first match.
  • :[n]tnext— Next [n] match.
  • :[n]tprev— The last [n] match.
  • :tfirst— The first match
  • :tlast— The last match
  • :tselect tagname— Opens the selection list

    The TAB key to swallow
  • :tag xyz<tab>— Complete the tag name starting with XYZ. Press TAB again to display other tags.
  • :tag /xyz<tab>— will complete with tag names that contain xyz in their names.

13.3 cscope

Cscope-rbq: Generates the cscope.out file

  • :cs add /path/to/cscope.out /your/work/dir
  • :cs find c funcFind where func is called
  • :cwOpen the QuickFix window to see the results

13.4 gtags

Gtags combines the functions of Ctags and CScope. To use Gtags, you need to have GNU Gtags installed. Then run gtags in the project directory.

  • :Gtags funcnameLocate to the definition of funcName.
  • :Gtags -r funcnameQuery where funcName is referenced.
  • :Gtags -s symbolLocate where symbol appears.
  • :Gtags -g string Goto stringThe place where it appears. :Gtags -gi string ignores case.
  • :Gtags -f filenameDisplays a list of functions in filename. You can display the current file using Gtags -f %.
  • :Gtags -P patternDisplays files that contain a particular schema in their path. For example :Gtags -p. H $displays all header files, :Gtags -p /vm/ displays files in the VM directory.

13.5 build

Vim provides :make to compile the program. The default call is make. If you have a makefile in your current directory, simply :make.

If you don’t have a make program, you can change the program that make calls by configuring the MakePRg option. If you only have one abc.java file, you can set it up like this:

  • set makeprg=javac\ abc.java

    Then :make. If there is a bug in the program, you can view the error through the Quickfix window. However, to locate the error correctly, you need to set errorFormat so that VIM recognizes the error message. Such as:
  • :setl efm=%A%f:%l:\ %m,%-Z%p^,%-C%.%#

    %f indicates the file name, %l indicates the line number, %m indicates the error message, and the rest is not understood. Please refer to help ErrorFormat.

13.6 Quickly Modifying a Window

The QuickFix plugin is useful for compiling debuggers

  • :copenOpen the quick Change window.
  • :cclose— Close the Quick Change window.

    The quick modify window is very useful when making a program, when after making:
  • :clList the errors in the Quick Fix window.
  • :cn— Locate the next error.
  • :cp— Locate the previous error.
  • :cr— Locate the first error.

13.7 Automatic Completion

  • C-x C-sSpelling suggestions.
  • C-x C-v— Complete vim options and commands.
  • C-x C-l— Complete the entire line.
  • C-x C-f– Automatic file completion path. When the menu pops up, press C-F to select, or C-n and C-p.
  • C-x C-pAnd *C-x C-n— Complete the current word with words that appear in the document. You can also press C-P and C-n.
  • C-x C-oYou can complete keywords and function names when programming.
  • C-x C-i— Complete according to the keywords in the header file.
  • C-x C-d— Complete the macro definition.
  • C-x C-n— Completes by the keyword that appeared in the buffer. Just press C-N or C-P.

    When the completion menu pops up:
  • C-pForward switch member;
  • C-nBackward-switching member;
  • C-eExit the drop-down menu and return to the original input text;
  • C-yExit the drop-down menu and accept the current option.

13.8 Indent multiple lines

In normal mode, press > twice. The line where the cursor is located is indented. If you press n first, then press > twice; , the n lines below the cursor are indented. Corresponding, press <; , the line where the cursor is is indented. If you are editing a code file, you can adjust it with =. In visual mode, select the block of code to adjust, press =, and the code will be indented according to the writing rules. Or n =, adjust the indentation of n lines of code.

13.9 fold

  • zf— Creates a collapsed command that can be used on a viewable area;
  • zdDelete the current row collapse;
  • zDDelete the current row collapse;
  • zfap— Collapses the segment where the cursor is;
  • zo— Open the folded text;
  • zc— Fold up;
  • za— Open/close current collapse;
  • zr— Open nested folds;
  • zm— Fold up nested folds;
  • zR (zO)— Open all folds;
  • zM (zC)— Fold up all folded lines;
  • zj— Skip to the next fold;
  • zk— Jump to the previous fold;
  • zi — enable/disable fold;

14. The command line

In normal mode, press: to enter the CLI mode

14.1 Shortcut Keys in CLI Mode:

Up and Down arrow keys: Up or next command. If some commands have been entered, the system searches for the previous or next matching command. Left and right arrow keys: Move left/right one character.

  • C-w: Deletes a word forward.
  • C-h: Deletes a character forward, equivalent to Backspace.
  • C-u: Moves from the current position to the beginning of the command line.
  • C-b: Moves to the beginning of the command line.
  • C-e: Moves to the end of the command line.
  • Shift-Left: Move one word left.
  • Shift-Right: Move one word right.
  • @ :Repeat the previous colon command.
  • Q:In normal mode, q then presses ‘:’ to open the command line history buffer and edit the command as if it were a file.

    Q/q? You can open it to find history.

14.2 Running External Commands

  • :!CMD executes external commands.
  • :!!!!!Execute the last external command.
  • :shCall the shell and return vim with exit.
  • :r ! cmdInserts the result of the command into the current location of the file.
  • :m,nw ! cmdExecute the command with the contents between lines M and N of the file as command input.

15. Other

15.1 Working Directory

  • :pwdDisplay vim’s working directory.
  • :cd pathChange vim’s working directory.
  • :set autochdirYou can let Vim automatically switch working directories based on the files you edit.

15.2 Some Shortcut Keys (Collecting)

  • K: Opens manpage of the word where the cursor is located.
  • *: Searches down for the word where the cursor is.
  • g*: Same as above, but only partially.
  • #: Searches up for the word where the cursor is.
  • g#: Same as above, but only partially.
  • g C-g: Count the number of words in a full text or part of a statistic.

15.3 Online Help

  • :h(elp)Or F1 open total help.
  • :help user-manualOpen the user manual.

    Command help is in the following format: The first line specifies how to use the command; This is followed by an indented paragraph explaining what the command does, followed by further information.
  • :helptags somepathGenerate indexes for documents in SomePath.
  • :helpgrepThe entire help document can be searched, and a list of matches is displayed in the Quickfix window.
  • Ctrl+]Jump to the Tag theme, Ctrl+ T jump back.
  • :verThe version information is displayed.

15.4 Some Minor Functions

Simple calculator: In insert mode, type C-r = and then enter the expression to get the result at the cursor.

The author: the red line and the bay link: www.jianshu.com/p/afcf3ae8b… The copyright of the book belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.