Vim
Vim - The Ubiquitous Text Editor.
Introduction
Vim is a text editor developed from vi. Its features such as code completion, compilation, and error navigation are particularly rich and convenient for programming, making it widely used among programmers.
Installation
Linux systems usually come with Vim pre-installed. Open a terminal and type vim to start it.
If manual installation is required, Vim's official website provides documentation for downloading. Compile and install according to your needs.
Vim Modes and Common Keybindings
The basic operations of Vim are explained in the built-in tutorial. Open a terminal and type vimtutor to enter the tutorial.
These operations typically take about twenty to thirty minutes to become familiar with.
Command Mode
The default mode when entering Vim.
In this state, keyboard actions are recognized by Vim as commands rather than input characters. For example, pressing i at this point will not input a character; i is treated as a command.
Vim's arrow keys are ↑, ↓, ←, →, or alternatively h, j, k, l.
1 2 3 4 5 | |
Here are commonly used commands in Command Mode:
iSwitch to Insert Mode, start inputting text at the current cursor position. Press Esc to return to Normal Mode.xDelete the character after the cursor.:Switch to Command-line Mode, to enter commands at the bottom line.aSwitch to Insert Mode, start inputting text after the cursor.oSwitch to Insert Mode, insert a new line below the cursor;OSwitch to Insert Mode, insert a new line above the cursor.pPaste clipboard content below the cursor;PPaste clipboard content above the cursor.ddDelete the entire line where the cursor is located.dThedcommand is also for deletion, often used in combination with other keys.uUndo the last change to the text.yTheycommand can copy the selected area. Requires pressingvto enter Visual Mode.yyCopy the current line.Ctrl + rRedo the last undone operation.:wSave the file, often used withqto save and exit.:qQuit Vim.:q!Force quit Vim without saving changes.
Some other commands:
- The
ccommand is used for change, equivalent todi. - The
=command can apply automatic indentation in default format to selected lines. ==Auto-indent the current line.- The
.command can repeat the last executed command. - The
ggcommand can jump to the beginning of the code; theGcommand can jump to the beginning of the last line of code; prefixingGwith a number jumps to the specified line. wcan jump to the beginning of the next word;ecan jump to the end of the current or next word;bcan jump to the beginning of the current or previous word;0can jump to the beginning of the line;$can jump to the end of the line.w,e,0,$can also be combined with other commands, such asde,dw,d0, andd$, which correspond to deleting to the end of the word, deleting to the beginning of the next word, deleting to the beginning of the line, and deleting to the end of the line, respectively.
In Command Mode, press / to bring up the search box at the bottom. Enter the characters to search for, press Enter to view the search results. If there are multiple results, press n to jump to the next result; press N to jump to the previous one.
In Command Mode, press * to search for the word under the cursor.
Before entering a command, typing a number n will cause the command to repeat n times.
Insert Mode
Press i in Command Mode to enter Insert Mode. Press Esc to return to Command Mode.
In Insert Mode, you can use the following keys:
- Character keys and Shift combinations to input characters.
- ENTER, the Enter key, for line breaks.
- BACK SPACE, the Backspace key, to delete the character before the cursor.
- DEL, the Delete key, to delete the character after the cursor.
- Arrow keys to move the cursor in the text.
- HOME/END to move the cursor to the beginning/end of the line.
- Page Up/Page Down to page up/down.
- Insert to toggle the cursor between insert and replace mode; the cursor will become a vertical line/underscore.
- ESC to exit Insert Mode and switch to Command Mode.
In Insert Mode, press Ctrl+o to enter "Insert - Command Mode". After executing one operation, it will automatically return to Insert Mode.
Command-line Mode
Press : in Command Mode to enter Command-line Mode.
In Command-line Mode, you can enter commands consisting of one or more characters. There are many available commands.
In Command-line Mode, the basic commands are:
:help/:hView the English Vim online help documentation.:wSave the file.:qQuit Vim.:wqSave the file and quit Vim.:q!/:!qForce quit Vim without saving changes.:e filenameCan open the specified file in the current directory.- The
:scommand is for substitution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
If the command is in the form :! command, the command will be executed in the bash terminal.
Press Esc to exit Command-line Mode.
Visual Mode
Press v to enter Visual Mode, often used for selecting areas. Press V (Shift+v) to enter Visual Line Mode, used for selecting lines.
Press Ctrl+v or Ctrl+q to enter Visual Block Mode.
After entering Visual Block Mode, press I or A to enter Insert Mode (equivalent to i and a). After exiting Insert Mode, the changes made to the current line will be applied to the same position on every selected line. Often used for batch adding comments.
After selection, typing y or d can also execute the corresponding commands.
The three Visual Modes can be converted between each other via key presses.
Vim Shortcuts
Refer to The Most Comprehensive Vim Shortcut Keymap—From Beginner to Advanced
Advanced Knowledge
The . Command
Vim users inevitably resist repetitive text modifications because Vim inherently requires two extra keystrokes compared to other editors—Esc and i. However, Vim actually provides the repeat command ., which is suitable for repetitive text addition, modification, and deletion operations.
The . command can repeat the last executed command. But this "command" is not limited to a single command; it can also be a combination of number + command; entering Insert Mode + inputting text + Esc is also a type of command. Therefore, using the . command appropriately is key to achieving maximum efficiency.
For example, each line of the following code is missing a semicolon at the end:
1 2 3 4 | |
Using . in combination with the command A (move to end of line and insert) allows for efficiently adding the missing semicolons.
1 2 3 | |
Another example: In the following code, the array names in the last five assignment statements are all incorrect:
1 2 3 4 5 | |
Changing them one by one is too tedious, and the s command in Command-line Mode would change them all.
The first method is to use it with the s command in Normal Mode (delete the character under the cursor and enter Insert Mode). Go to the first letter of the first incorrect array name, press 3s/cw, input the correct array name, and exit. Then move the cursor to each subsequent one and use the . command.
The second, more time-saving method is to use search mode for modification. Type /bok, press Enter, and use the n key to go to the first letter of the first incorrect array name. Type 3s new_array_name <Esc>, and finally repeat n..
The third method uses the simple search command f. In Normal Mode on a line, f + single character searches for that character in the line and moves the cursor to it; press ; to find the next occurrence, , to find the previous one. So for the code above, just type fb;;;, then enter Insert Mode to modify, and then ;.. This method is suitable when only movement within a line is needed.
Macros
Vim's macro function can repeat arbitrarily long command sequences.
Before using a macro, you need to "record" it, meaning record a series of keystrokes and then replay them, achieving the effect of repetition. Recording is simple: in Normal Mode, type q to start recording. Next, assign a command key for the macro, which can be any of the 26 letters. The bottom left will then show recording @the_letter_you_chose. You can then start recording commands. Similarly, press q in Normal Mode to stop recording.
To use it, press : to enter Command-line Mode, type @the_recorded_letter, and the previously recorded commands will be invoked.
Combining . with macros, i.e., record macro → invoke macro → . repeat command → number + ., can achieve very high efficiency.
The normal Command
This command is related to Normal Mode. Its effect is to repeat a command on specified lines.
Press : to enter Command-line Mode and input the following command:
1 | |
Or:
1 | |
The meaning of the above command is to execute the command command in Normal Mode on lines a~b.
Since the normal command can be repeated by the . command and is easy to understand, its usage frequency is even higher than that of macros.
Number + . + Macro + normal
The above three commands can be combined. For example:
I downloaded a book, and I need every chapter to become a "heading" to facilitate conversion to formats like mobi or to generate a TOC for navigation. How can I do this?
Here is the process using Vim:
- Press / to bring up the search box, enter a regular expression to search.
- Use the
qcommand to start recording a macro. - Type the
I#command, then press ESC. - Use the
qcommand to stop recording the macro. - Type
normal n@letterto go to the next occurrence and repeat the previous step. - Type
number + .to repeat multiple times.
External Links
- Vim Official Website
- Configuration provided by the original author
- Vim Debugging: Getting Started with termdebug
- Vim scripting cheatsheet
- Learn Vimscript the Hard Way
- Linux vi/vim | Rookie Tutorial
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用