- 1. About VIM
- 2. Start Vim
- 3. Document operations
- 4. Movement of the cursor
- 4.1 Basic Movement
- 4.2 Turning screen
- 4.3 marks
- 5. Inserting text
- 5.1 Basic Insertion
- 5.2 Overwrite Insert
- 6. Cut Copy and register
- 6.1 Cut and copy, paste
- 6.2 Text Objects
- 6.3 Registers
- 7. Find and replace
- 7.1 Find
- 7.2 Replacement
- 7.3 Regular Expressions
- 8. Typesetting
- 8.1 Basic Typesetting
- 8.2 Spell Check
- 8.3 Counting words
- 9. Editing Multiple Files
- 9.1 Edit multiple files at once
- 9.2 Multi-label editing
- 9.3 Buffers
- 10. Split screen Editing
- 10.1 Horizontal Split
- 10.2 Vertical Split
- 10.3 Close child window
- 10.4 resizing windows
- 10.5 Switching and moving windows
- 11. Quick Edit
- 11.1 Changing the case
- 11.2 Replacement (Normal mode)
- 11.3 Undo and Redo (normal mode)
- 11.4 Macros
- 12. Editing Special files
- 12.1 File Plus Decryption
- 12.2 Encoding of the file
- 12.3 File formats
- 13. Programming Assistance
- 13.1 Some keys
- 13.2 Ctags
- 13.3 cscope
- 13.4 Gtags
- 13.5 Compiling
- 13.6 Quick Edit window
- 13.7 Auto-Complete
- 13.8 multi-line indent indent
- 13.9 Folding
- 14. Command line
- 14.1 shortcut keys in command line mode:
- 14.2 Executing external commands
- 15. Other
- 15.1 Working directory
- 15.2 Some shortcut keys (in the collection)
- 15.3 online Help
- 15.4 some small functions
1. About VIM
Vim is my favorite editor and the second most powerful editor under Linux. Although Emacs is recognized as the world's number one, I think it is efficient to use Emacs without using VI for editing. If it is a beginner VI, it is a smart decision to run Vimtutor. (If your system environment is not Chinese, and you want to use Chinese vimtutor, run Vimtutor ZH) 1.1 Several modes of VIM
- Normal mode: You can use the shortcut key command, or press: To enter the command line.
- Insert mode: You can enter text, in normal mode, press I, A, O and so on can enter the insertion mode.
- Visual mode: Press V in normal mode to enter the visual mode, in the visual mode, move the cursor to select text. Press V to enter the visual row mode, which is always selected for the entire row. CTRL + V enters the visual block mode.
- Replace mode: In normal mode, press R to enter.
2. Start Vim
- Vim-c cmd file: Executes the specified command before opening the file;
- Vim-r file: Restore the last exception exit files;
- Vim-r file: Open files in a read-only manner, but can be forced to save;
- Vim-m file: Open files in a read-only manner and cannot be forced to save;
- Vim-y num File: Sets the size of the editing window to num rows;
- Vim + file: Start at the end of the file;
- Vim +num file: starting from Num line;
- Vim +/string File: Open file and rest the cursor on the first string found.
- Vim--remote file: Opens the specified files with the existing Vim process. This is useful if you do not want to enable multiple Vim sessions. Note, however, that if you use VIM, you will find a server called Vim, and if you already have a gvim running, you can open the file in an existing gvim with Gvim--remote file.
3. Document operations
- : E file--closes the currently edited document and opens a new file. If changes to the current file are not saved, VI warns.
- : e! FILE--Discard changes to the current file and edit the new file.
- : E+file-Start a new file and edit it from the end of the file.
- : E+n file-Starts a new document and edits from Nth line.
- : Enew--compiles a new unnamed document. (ctrl-w N)
- : E--Reloads the current document.
- : e! --Reload the current document and discard the changes you have made.
- : e# or ctrl+^--back to the file you just edited, very useful.
- : F or Ctrl+g--Displays the document name, whether it is modified, and the cursor position.
- : F filename-Changes the file name of the edit, then saves the equivalent of Save As.
- GF--Opens a file with the name of the string where the cursor is located.
- : W--Save the changes.
- : n1,n2w filename-selectively saves content from one N1 line to another N2 row.
- : 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 you want to edit. If it is in the terminal, the Netrw File browser window will open, and if it is gvim, a browser window will open with a graphical interface. In fact: Browse can follow any command that edits the document, such as SP. The starting directory opened with Browse can be set by Browsedir:
- : Set Browsedir=last--with the last accessed directory (default);
- : Set Browsedir=buffer--Use the directory where the current file is located;
- : Set Browsedir=current--with the current working directory;
- : Sex-Split a window horizontally, browsing the file system;
- : Vex--split a window vertically to browse the file system;
4. Movement of the cursor 4.1 basic movement
The following moves are all in normal mode.
- H or BACKSPACE: Move left one character;
- L or space: move one character to the right;
- J: Move down one line;
- K: Move up one line;
- GJ: Move to the next line within a paragraph;
- GK: Move to the previous line within a paragraph;
- + or ENTER: Moves the cursor to the next line of the first non-whitespace character.
- -: Moves the cursor to the first non-whitespace character on the previous line.
- W: Move forward one word, the cursor stops at the beginning of the next word;
- W: Moves the beginning of the next word, but ignores some punctuation;
- E: Move forward one word, the cursor stops at the end of the next word;
- E: Move to the end of the next word, or move to punctuation if the ending has punctuation;
- B: Move back one word and the cursor stops at the beginning of the previous word;
- B: Move to the beginning of the previous word, ignoring some punctuation;
- GE: Move back one word and the cursor stops at the end of the previous word;
- GE: With ge , but ' word ' contains punctuation that is adjacent to the word.
- (: Move forward 1 sentences.)
- ): Move back 1 sentences.
- {: Move forward 1 paragraphs.
- }: Move back 1 paragraphs.
- FC: Move the cursor to the next C character in the same line
- FC: Move the cursor to the previous C character in the same line
- TC: Move the cursor to the next C character in the same line
- Tc: After moving the cursor to the previous C character in the same line
- ;: Use with F & T, repeat once
- ,: Use with F & T, reverse repeat once
All of the above can be used with n, such as entering 3h under normal mode (as described below), the cursor moves 3 characters to the left.
- 0: Move to the beginning of the line.
- G0: Moves to the beginning of the screen line where the cursor is located.
- ^: Move to the first non-whitespace character of the bank.
- g^: Same ^ , but moves to the first non-empty character of the current screen line.
- $: Moves to the end of the line.
- g$: Moves the end of the line on the screen of your cursor.
- n|: Move the cursor over the recursive n column.
- NG: To the nth line of the file.
- :n<cr> moves to nth row.
- :$<cr> moves to the last row.
- H: Move the cursor to the top row of the screen.
- M: Move the cursor to the middle line of the screen.
- L: Move the cursor to the bottom line of the screen.
- GG: To the head of the file.
- G: To the tail of the file.
4.2 Turning screen
- Ctrl+f: Turn down one screen.
- Ctrl+b: Turn up one screen.
- Ctrl+d: Flip half screen.
- Ctrl+u: Flip half screen.
- Ctrl+e: Scrolls down one line.
- Ctrl+y: Scrolls up one line.
- n%: The location of the file n%.
- ZZ: Moves the current line 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 marks
Use markers to move quickly. After you reach the marker, you can return to the original position with Ctrl+o. Ctrl+o and Ctrl+i are much like backwards and forwards on the browser.
- M{A-Z}: Marks the location of the cursor, local tags, only for the current file.
- M{A-Z}: Marks the location of the cursor, the global tag. After marking, exit vim, reboot, Mark still valid.
- ' {A-Z}: Move to marker location.
- ' {A-Z}: Moves to the beginning of the marker line.
- ' {0-9}: Go back to the last position [2-10] when you close vim.
- ": Moves to the last edited position. "It's OK, but ' accurate to the column, and ' ' to the line. If you want to jump to an older location, you can press C-o and jump to the updated location with C-i.
- ': Move to the last place you left.
- '.: Move to the last changed place.
- : Marks displays all tags.
- :d Elmarks a B--delete markers A and B.
- :d elmarks a-c--delete markers A, B, and C.
- :d Elmarks a c-f--delete markers A, C, D, E, F.
- :d elmarks! --Deletes all the tokens of the current buffer.
- : Help mark-motions see more about Mark's knowledge.
5. Insert Text 5.1 Basic insert
- I: Insert in front of the cursor; a tip: Press 8, then I, enter insert mode, enter =, press ESC to enter command mode, there will be 8 =. This is useful when inserting split lines, such as 30i+<esc> inserting 36 + split lines.
- I: The first non-empty word match either inserted in the current line;
- GI: Inserted in the first column of the current row;
- A: Insert after the cursor;
- A: Insert at the end of the current line;
- o: Insert a new line below;
- O: Insert a new row above;
- : R filename Inserts the contents of another file at the current location.
- : [n]r filename Inserts the contents of another file on nth row.
- : 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: Overwrites 1 (n) words after the cursor.
- C[n]l: Overwrites n letters after the cursor.
- C[n]h: Overwrites the first n letters of the cursor.
- [N]CC: Modifies the current [n] row.
- [N]s: Replaces 1 (n) characters after the cursor with the input text, which is equivalent to C[n]l.
- N S: Deletes the specified number of rows and replaces them with the text you enter.
Note that the form similar to CNW,DNW,YNW can also be written as NCW,NDW,NYW. 6. Cut Copy and register 6.1 cut and copy, paste
- [n]x: N characters to the right of the clipping cursor, equivalent to d[n]l.
- N X: N characters to the left of the clipping cursor, equivalent to D[N]H.
- Y: Copies the selected text in Visual mode.
- yy or Y: Copies the entire line of text.
- Y[n]w: Copy one (n) word.
- Y[n]l: Copies the 1 (n) characters to the right of the cursor.
- Y[n]h: Copies the 1 (n) characters to the left of the cursor.
- y$: Copies the cursor from its current position to the end of the line.
- Y0: Copies the cursor from its current position to the beginning of the line.
- :m,ny<cr> copies the contents of m rows to n rows.
- y1g or Ygg: Copies all lines above the cursor.
- YG: Copies all lines below the cursor.
- Yaw and Yas: Copy a word and copy a sentence, even if the cursor is not at the beginning of the word and the beginning of the sentence.
- D: Delete (cut) the selected text in Visual mode.
- d$ or D: Deletes (cuts) the contents of the current position to the end of the line.
- D[n]w: Delete (cut) 1 (n) words
- D[n]l: Delete (cut) 1 (n) characters to the right of the cursor.
- D[n]h: Delete (cut) 1 (n) characters to the left of the cursor.
- D0: Delete (cut) the contents of the current position to the beginning of the line
- [n] DD: Delete (cut) 1 (n) rows.
- :m,nd<cr> cuts the contents of m rows to n rows.
- D1G or DGG: Cuts all lines above the cursor.
- DG: Cuts all lines below the cursor.
- Daw and Das: Cut a word and cut a sentence, even if the cursor is not at the beginning of the word and the beginning of the sentence.
- D/f<cr>: This is a more advanced combination command that will delete the contents of the current position between the next F.
- P: Paste after the cursor.
- P: Paste before the cursor.
6.2 Text Objects
- AW: A Word
- As: one sentence.
- AP: a section.
- AB: A piece (enclosed in parentheses).
Y, D, C, v All can be followed by a text object. 6.3 Registers
- A-Z: Both can be used as register names. "Ayy the contents of the current row into a register.
- A-Z: An uppercase index register allows you to append content to the register. such as "Ayy appends the contents of the current row to the a register.
- : REG Displays the contents of all registers.
- "": registers that are used by default when the register index is not added.
- "*: Current selection buffer," *yy the contents of the current row into the current selection buffer.
- "+: System Clipboard. "+yy the contents of the current row into the system Clipboard.
7. Find and Replace 7.1 find
- /something: Find something in the text that follows.
- Something: Look for something in the previous text.
- /pattern/+number: Stop the cursor on the number line following the line containing the pattern.
- /pattern/-number: Stops the cursor on line number preceding the line containing the pattern.
- N: Look backwards for the next.
- N: Look forward to the next.
You can use grep or vimgrep to find out where a pattern has appeared,
Where: grep is called outside of the grep program, and: Vimgrep is Vim's own search algorithm.
Usage:: Vim[grep]/pattern/[g] [j] Files
The meaning of G is that if a pattern appears more than once in a row, this line also appears multiple times in the result.
The meaning of J is that after grep ends, the result is stopped in item J, and the default is stop at the first item.
Vimgrep can be preceded by a number to limit the search results, such as
: 1vim/pattern/% only finds the first occurrence of that pattern in this file.
In fact, Vimgrep is especially useful when reading plain text ebooks, and can generate a directory for navigation.
For example, the title of each section in an ebook is: N. Xxxx. You can do this:
: vim/^d{1,}./%
Then use: CW or: Copen to view the results, you can use c-w h to move the Quickfix window to the left,
It's more like a catalogue. 7.2 Replacement
- : S/old/new-Replaces the first old of the current line with new.
- : s/old/new/g-Replaces all old on the current line with new.
- : n1,n2s/old/new/g-replace file with new N1 line to all old of N2 line.
- :%s/old/new/g-Replaces all old in the file with new.
- :%s/^/xxx/g-Insert xxx,^ at the beginning of each line to indicate the beginning of a row.
- :%s/$/xxx/g-Insert xxx,$ at the end of each line to represent the end of the line.
- At the end of all replacement commands, add C, and each substitution will require user confirmation. such as:%S/OLD/NEW/GC, plus I ignore case (ignore).
There is a more flexible way than replace, it is to match to a pattern after executing some kind of command,
The syntax is: [Range]g/pattern/command
For example:%g/^ xyz/normal DD.
Represents the DD command in normal mode for lines that begin with a space and XYZ.
The provisions on range are:
- If range is not specified, the current row is represented.
- M,n: Rows from M to N.
- 0: The first line (probably).
- $: Last line
- .: When moving forward
- %: All lines
7.3 Regular Expressions
A regular expression is used for advanced lookup substitution.
- \d: Represents the decimal number (I guess)
- \s: Indicates a space
- \s: non-null characters
- \a: English alphabet
- \|: Indicates or
- \.: Indicates.
- {M,n}: represents m to n characters. This is in conjunction with \s and \a, such as \a\{m,n}, which represents m to n English letters.
- {m,}: Represents m to an infinite number of characters.
- * *: All subdirectories under the current directory.
: Help pattern gets more. 8. Typesetting 8.1 Basic typesetting
- << indent one shiftwidth to the left
- >> indent one shiftwidth to the right
- : CE (nter) line Text Center
- : Le (FT) bank text on left
- : Ri (ght) bank text on right
- GQ to rearrange the selected text, that is, to line the text over long
- Gqq re-arrange to move forward
- GQNQ row N rows
- GQAP reflow Current Segment
- Gqnap re-rank n-Segment
- Gqnj rearrange the current row and the following n rows
- GQQ reflow current segment to end of article
- J stitching the current line and the next line
- GJ with J , but no spaces after merging.
8.2 Spell Check
- : Set spell-turn on spell check function
- : Set nospell-turn off spell check function
- ]s-move to the next misspelled word
- [S-action is similar to the previous command, but it is a search in the opposite direction
- z=-displays a list of misspelled words from which you can select
- Zg-tells the spelling checker that the word is spelled correctly
- zw-In contrast to the previous command, tell the spelling checker that the word is misspelled
8.3 Counting words
G ^g can count the number of characters in a document and the number of lines. Place the cursor on the last character, minus the number of lines in characters to roughly count the number of words in the Chinese document. The above applies to MAC or Unix file formats. If it is a Windows file format (that is, a newline character has two bytes), the word count is: number of characters-number of lines * 2. 9. Edit multiple Files 9.1 edit multiple files at once
We can open more than one file at a time, such as
VI a.txt b.txt c.txt
- Use: Next (: N) to edit the next file.
- : 2n edit the next 2 files.
- Use: Previous or: N to edit the previous file.
- Use: Wnext, save the current file, and edit the next file.
- Use: wprevious, save the current file, and edit the previous file.
- Use: args to display a list of files.
- : N filenames or: args filenames specifies a new file list.
- Vi-o filenames edits multiple files in a horizontal split window.
- Vi-o filenames edit multiple files in a vertical split of multiple windows.
9.2 Multi-label editing
- Vim-p files: Open multiple files, each of which occupies a tab page.
- : Tabe, Tabnew--if you add a filename, open the file in a new tab, or open an empty buffer.
- ^w GF--Opens the file specified in the path under the cursor in the new tab.
- : TABN--Switch to the next tab. Control + PageDown, can also.
- : TABP--Switch to the previous tab. Control + PageUp, can also.
- [n] GT--Switch to the next tab. If n is added earlier, switch to Nth label. The first label is numbered 1.
- : Tab Split-Opens the contents of the current buffer in a new tab.
- : Tabc[lose]--Close the current tab.
- : Tabo[nly]--Close other tabs.
- : Tabs--Lists all the tabs and the windows they contain.
- : Tabm[ove] [n]--Moves the tab page and moves to the Nth tab. such as the TABM 0 Current tab, it will become the first tab page.
9.3 Buffers
- : buffers or: LS or: Files displays a list of buffers.
- ctrl+^: Toggles between the last two buffers.
- : BN--the next buffer.
- : BP-the last buffer.
- : BL--the last buffer.
- : B[n] OR: [n]b--Switch to nth buffer.
- : NbW (Ipeout)--Deletes the nth buffer completely.
- : NBD (elete)--Deletes the nth buffer, is not actually deleted, and is also in the unlisted list.
- : Ba[ll]--opens all buffers in the current page, one window per buffer.
10. Split screen Editing
- Vim-o file1 file2: Split the window horizontally while opening file1 and File2
- Vim-o file1 file2: Split the window vertically while opening file1 and File2
10.1 Horizontal Split
- : Split (: SP)-divides the current window horizontally into two windows. (ctrl-w s or ctrl-w ctrl-s) Note If ctrl-s may freeze the terminal under the terminal, press CTRL-Q to continue.
- : Split filename-splits the window horizontally and displays another file in a new window.
- : Nsplit (: NSP)-horizontally splits a window with n rows high.
- : [N]new--horizontally splits a window with N rows high and edits a new file. (Ctrl-w N or ctrl-w ctrl-n)
- Ctrl+w F--splits a window horizontally and opens a file in a new window with the name of the word in the cursor.
- C-w c-^--split a window horizontally to open the file you just edited.
10.2 Vertical Split
- : Vsplit (: VSP)--splits the current window into two windows with a horizontal distribution. (Ctrl-w V or CTRL ctrl-v)
- : [N]vne[w]--A new window is split vertically.
- : Vertical Horizontal Split command: The corresponding vertical split.
10.3 Close child window
- : Qall--Close all windows and exit Vim.
- : Wall--Save all the modified Windows.
- : Only-keep the current window and close other windows. (Ctrl-w o)
- : Close-Closes the current window and Ctrl-w C achieves the same function. (like: q:x works likewise)
10.4 resizing windows
- Ctrl+w +--the current window increases by one row. You can also increase n rows with N.
- Ctrl+w---the current window decreases by one line. You can also use N to reduce the n rows.
- Ctrl+w _--the current window expands to as large as possible. You can also set the number of rows with N.
- : Resize N--current window n row high.
- Ctrl+w =--All windows are the same height.
- N Ctrl+w _--the height of the current window is set to n rows.
- Ctrl+w <--the current window is reduced by one column. You can also use N to reduce the N column.
- Ctrl+w >--The current window is widened by one column. You can also use N to increase the width of 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 and moving windows
If the mouse is supported, it is easy to toggle and resize the subwindow.
- Ctrl+w ctrl+w: Switch to the next window. Or a ctrl+w W.
- CTRL+W P: Switch to the previous window.
- Ctrl+w h (l,j,k): Toggles the window to the left (right, bottom, top).
- Ctrl+w T (b): Switches to the top (bottom) side of the window. <BR>
- Ctrl+w H (L,K,J): Moves the current window to the leftmost (right, top, bottom) polygon.
- Ctrl+w R: Rotates the position of the window.
- Ctrl+w T: Moves the current window to the new tab.
11. Quick Edit 11.1 Change case
- ~: Reverses the case of the character where the cursor is located.
- U or U in visual mode: Turns the selected text to uppercase or lowercase.
- Gu (U) range (such as $, or g), you can convert all letters from the current position of the cursor to the specified position into lowercase or uppercase. As GGGUG, the letters between the beginning and the last line are all lowercase. Again, like gu5j, the current line and the following four lines all become lowercase.
11.2 Replacement (Normal mode)
- R: Replaces the character at the cursor, and also supports kanji.
- R: Enter replace mode and press ESC to return to normal mode.
11.3 Undo and Redo (normal mode)
- [n] U: cancels one (n) change.
- : Undo 5--undo 5 changes.
- : Undolist-Your undoing history.
- CTRL + R: Redo the last change.
- U: Cancels all changes in the current line.
- : Earlier 4m--back 4 minutes ago
- : Later 55s--forward 55 seconds
11.4 Macros
- . --Repeat the last edit action
- QA: Start recording macro A (keyboard action record)
- Q: Stop Recording
- @a: Play Macro A
12. Edit Special file 12.1 file plus decrypt
- Vim-x file: Start editing an encrypted document.
- : X--Sets the password for the current file.
- : Set key=--Removes the password for the file.
This is the relatively advanced VI technique summarized by the Dian Fox. 12.2 Encoding of the file
- : E ++enc=utf8 filename, let vim open this file with Utf-8 encoding.
- : W ++ENC=GBK, no matter what encoding the current file, dump it into GBK encoding.
- : Set Fenc or: Set fileencoding to view the encoding of the current file.
- Adding set Fileencoding=ucs-bom,utf-8,cp936,vim in VIMRC will select the appropriate encoding based on the file you want to open. Note: Do not leave spaces between encodings. The cp936 corresponds to the GBK encoding. The Ucs-bom corresponds to the file format under Windows.
Let vim correctly handle file format and file encoding, depending on the correct configuration of ~/.VIMRC 12.3 file format
There are roughly three types of file formats: UNIX, DOS, Mac. The difference between the three formats is mainly the key to enter the code: DOS is carriage return add line, Unix under only line break, Mac only carriage return.
- : E ++ff=dos filename, let vim open this file in DOS format.
- : w ++ff=mac filename, this file is stored in MAC format.
- : Set FF, which displays the format of the current file.
- Add set Fileformats=unix,dos,mac in VIMRC to allow Vim to automatically recognize the file format.
13. Programming Assistance 13.1 Keys
- GD: Jump to the definition of local variables;
- GD: Jump to the definition of the global variable, start searching from the beginning of the current file;
- g;: Last modified place;
- G,: The next modified place;
- [[: Jump to the beginning of the previous function block, need to have a separate row of {.
- ]]: Jump to the beginning of the next function block, you need to have a separate row of {.
- []: Jumps to the end of the previous function block and requires a separate line}.
- [: Jumps to the end of the next function block and requires a separate line}.
- [{: 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 the end of the current comment block;
- %: Not only can you move to a matching (), {} or [], but also in # if, #else, #endif之间跳跃.
The following brace matching is useful for programming.
- Ci ', di ', Yi ': Modify, cut or copy the content between.
- Ca ', da ', ya ': Modify, Cut, or copy the content between '.
- Ci ", di", Yi ": Modify, Cut or copy" between the contents.
- Ca ", Da", ya ": Modify, Cut, or copy" between the contents, including ".
- CI (, Di (, Yi (: Modify, Cut, or copy the content between ().
- CA (, Da (, ya (: Modify, Cut, or copy the content between (), contains ().
- ci[, di[, yi[: Modify, Cut, or copy the content between [].
- ca[, da[, ya[: Modify, Cut, or copy the contents of [], including [].
- ci{, di{, yi{: Modify, Cut, or copy the contents of {}.
- ca{, da{, ya{: Modify, Cut, or copy the contents of {}, including {}.
- Ci<, di<, yi<: Modify, Cut, or copy the contents between <>.
- Ca<, da<, ya<: Modify, Cut, or copy the contents between <>, including <>.
13.2 Ctags
- Ctags-r: Generate tag file,-R indicates also generate tags for files in subdirectories
- : Set Tags=path/tags--tell ctags which tag file to use
- : Tag xyz--jumps to the definition of XYZ, or places the cursor on XYZ by pressing C-], returning with C-T
- : Stag xyz--Displays the definition of XYZ with a split window, or c-w], if C-w n], opens a window with n rows high
- :p tag XYZ--Opens the definition of XYZ in the preview window, the hotkey is c-w}.
- :p Close-Closes the preview window. The hotkey is C-w Z.
- :p Edit Abc.h--editing abc.h in the preview window
- :P Search ABC--searches the file for the current file and the include of the current file, showing the lines that contain ABC.
Sometimes a tag may have multiple matches, such as a function overload, and a function name will have multiple matches. This situation jumps to the first match first.
- : [N]tnext--Next [n] matches.
- : [N]tprev--previous [n] matches.
- : TFirst--First match
- : Tlast--Last match
- : Tselect tagname--Open selection list
TAB key
- : Tag xyz<tab>--Complement the tag name starting with XYZ and continue pressing the TAB key to show the other.
- :tag/xyz<tab>-the tag name that contains XYZ in the name is complete.
13.3 cscope
- CSCOPE-RBQ: Generate Cscope.out file
- : CS Add/path/to/cscope.out/your/work/dir
- : CS Find c func--Find out where func is called
- : CW--Open Quickfix window to view results
13.4 Gtags
Gtags combines the functions of ctags and Cscope. Before using gtags, you need to install the GNU Gtags. Then run gtags in the project catalog.
- : Gtags FuncName is positioned at the definition of funcname.
- : gtags-r funcname Query where FuncName is referenced.
- : gtags-s symbol locates the symbol where it appears.
- : gtags-g string Goto string where it appears. : Gtags-gi string ignores case.
- : gtags-f filename Displays a list of functions in filename. You can use: gtags-f% to display the current file.
- : Gtags-p pattern Displays files in the path that contain a specific pattern. Example: Gtags-p. h$ displays all header files: gtags-p/vm/Displays the files under the VM directory.
13.5 Compiling
VIM provides: Make to compile the program, the default call is made, if you have makefile in the current directory, simply: do.
If you do not have a make program, you can change the program that makes calls by configuring the MAKEPRG option. If you have only one Abc.java file, you can set this up:
Set Makeprg=javac\ Abc.java
Then: Make. If the program is wrong, you can view the error through the Quickfix window. However, if you want to correctly locate the error, you need to set the Errorformat to let vim recognize the error message. Such as:
: Setl efm=%a%f:%l:\%m,%-z%p^,%-c%.%#
%f represents a file name,%l represents a line number,%m represents an error message, and others are not understood. Please refer to: Help Errorformat. 13.6 Quick Edit window
It is actually a function provided by the Quickfix plug-in, which is useful for compiling the debug program:)
- : Copen--Open the Quick edit window.
- : Cclose--Close the Quick edit window.
The Quick Modify window is useful when make programs, after make:
- : CL--Lists errors in the Quick edit window.
- : CN--Navigates to the next error.
- : CP--Navigates to the previous error.
- : CR--Navigates to the first error.
13.7 Auto-Complete
- C-x C-s--spelling suggestions.
- C-x c-v--Complete vim options and commands.
- C-x c-l--Complete line completion.
- C-x c-f--Auto-complete file path. After the popup menu, press C-f to cycle through the selection, of course, you can press C-n and c-p.
- C-x c-p and C-x c-n-fill in the current word with the words that appear in the document. Press C-p and c-n directly also can.
- C-x C-o--can be used to complete the keyword and function name when programming.
- C-x C-i--based on keyword completion in the header file.
- C-x c-d--complement macro definition.
- C-x C-n--fill in the key words that appear in the buffer. Press C-n or c-p directly.
When the completion menu pops up:
- C-P forward switch member;
- C-n switch members backwards;
- C-E exit the drop-down menu and return to the original input text;
- C-y exit the drop-down menu and accept the current option.
13.8 multi-line indent indent
- In normal mode, double-click >; indent the cursor.
- If you press n first, then double-click, the n line below the cursor is indented.
- corresponding, press two <;, the line where the cursor is indented.
- If you are editing a code file, you can adjust it with =.
- In visual mode, select the block of code you want to adjust, press =, and the code will be indented according to the writing rules.
- or n =, adjust the indentation of the N-line code.
13.9 Folding
- ZF-Creates a collapsed command that can be used on a visual area;
- ZD--Deletes the current row's collapse;
- ZD--Deletes the current row's collapse;
- ZFAP--the segment where the cursor is folded;
- Zo--Open the collapsed text;
- ZC--fold folding;
- ZA--open/close current folding;
- ZR-opens nested lines of folding;
- ZM--Close nested folding lines;
- ZR (ZO)--Open all folding lines;
- ZM (ZC)--Close all folding lines;
- ZJ--jumps to the next folding place;
- ZK-Jumps to the previous folding place;
- Zi--enable/disable fold;
14. Command line
In normal mode press: Enter command line mode 14.1 shortcut keys in command line mode:
- Up and DOWN ARROW keys: Previous or Next command. If you have already entered a partial command, find the previous or next matching command.
- Left/Right ARROW key: Move one character to/from.
- 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: Move to the beginning of the command line.
- C-E: Move to the end of the command line.
- Shift-left: Move one word to the left.
- Shift-right: Move one word to the right.
- @: Repeats the last colon command.
- Q: In normal mode, q then press ': ' to open the command line history buffer and edit the command as if you were editing a file.
- q/and Q? You can open the Find history.
14.2 Executing external commands
- :! CMD executes the external command.
- :!! Executes the last external command.
- : SH calls the shell and returns vim with exit.
- : R!cmd Inserts the return result of the command into the current location of the file.
- : M,NW!cmd The contents of the M line to n rows of the file as the command Input Execution command.
15. Other 15.1 working directory
- :P WD Displays the working directory of Vim.
- : CD path changes the working directory of Vim.
- : Set Autochdir allows vim to automatically switch the working directory based on the edited file.
15.2 Some shortcut keys (in the collection)
- K: Open the manpage of the word where the cursor is located.
- *: Searches down the word of the cursor.
- g*: Ibid., but partially in line.
- #: Search up the word for the cursor.
- g#: Ibid., but partially in line.
- G C-g: Count the number of words in the full or statistical section.
15.3 online Help
- : H (ELP) or F1 opens the total help.
- : Help user-manual Open the user manual.
- The format of the command Help is: The first line indicates how to use the command, then the Indentation section explains the function of the command, and then the further information.
- : Helptags Somepath generates an index for the document in Somepath.
- : Helpgrep can search the entire help document, and the matching list appears in the Quickfix window.
- CTRL +] jump to tag theme, ctrl+t jump back.
- : ver displays version information.
15.4 some small functions
- Simple calculator: In insert mode, enter C-r =, and then enter the expression, you can get the calculation results at the cursor.
Vim User Manual