YouTip LogoYouTip

Linux Vim

All Unix-like systems come with the vi text editor built-in, while other text editors may not necessarily be present. However, the editor we use more frequently nowadays is the vim editor. vim has programming capabilities, such as actively using font colors to identify the correctness of syntax, which is convenient for programming. Related articles: [The Most Complete Vim Shortcut Key Map β€” From Beginner to Advanced](#) * * * ## What is vim? Vim is a text editor developed from vi. It is particularly rich in features convenient for programming, such as code completion, compilation, and error jumping, and is widely used among programmers. Simply put, vi is an old-fashioned word processor, but its features are already quite complete, though there is still room for improvement. vim can be considered a very useful tool for program developers. Even the official vim website ([https://www.vim.org/](https://www.vim.org/)) itself states that vim is a program development tool, not a word processing software. ### vim Keyboard Diagram VI / VIM Keyboard Quick Reference Chart Β· Click a key Β· Learn more Cursor Movement Β· Editing Operations Β· Enter Insert Mode Β· Visual Mode Β· Search/Jump Β· Other Commands Esc Esc Must Learn! ~ ` Case/Mark ! 1…9 Repeat Number @ 2 Execute Macro # 3 Search Forward $ 4 End of Line % 5 Bracket Matching ^ 6 Soft Start of Line & 7 Repeat Replace * 8 Search Backward ( 9 Previous Sentence ) 0 0=Start of Line _ - Start of Previous Line + = Format ↡ Enter Start of Next Line Tab Tab Q q Macro/Ex W w Next Word E e End of Word R r Replace T t To Character Y y Copy (yank) U u Undo I i Insert O o New Line P p Paste { [ Previous Paragraph } ] Next Paragraph Cap A a Append S s Replace Insert D d Delete F f Find within Line G g G Jump H h ← / Top of Screen J j ↓ / Join Lines K k ↑ / Help L l β†’ / Bottom of Screen : ; : Command Line " ' Mark/Register Sft Z z Exit/Scroll X x Delete Character C c Change V v Visual Mode B b Previous Word N n Next/Previous Match M m Mark/Middle of Screen < , . . Repeat ? / Search ↑ Click any key to view detailed explanation and example usage * * * ## Using vi/vim Basically, vi/vim is divided into three modes: **Command Mode, Insert Mode, and Command-Line Mode**. ### Command Mode **When the user starts vi/vim, they enter Command Mode.** In this state, keyboard actions are recognized by Vim as commands, not character input. For example, pressing 'i' at this point does not input a character; 'i' is treated as a command. Here are some commonly used commands in Normal Mode: * i -- Switch to Insert Mode, start typing text at the current cursor position. * x -- Delete the character under the current cursor. * : -- Switch to Command-Line Mode to enter commands at the bottom line. * a -- Enter Insert Mode, start typing text at the position after the cursor. * o: Insert a new line below the current line and enter Insert Mode. * O -- Insert a new line above the current line and enter Insert Mode. * dd -- Cut the current line. * yy -- Copy the current line. * p (lowercase) -- Paste clipboard content below the cursor. * P (uppercase) -- Paste clipboard content above the cursor. * u -- Undo the last operation. * Ctrl + r -- Redo the last undone operation. * :w -- Save the file. * :q -- Quit the Vim editor. * :q! -- Force quit the Vim editor without saving changes. If you want to edit text, simply start Vim, enter Command Mode, and press 'i' to switch to Insert Mode. Command Mode only has some basic commands, so you still need to rely on **Command-Line Mode** to input more commands. ### Insert Mode Pressing 'i' in Command Mode enters Insert Mode. Use the Esc key to return to Normal Mode. In Insert Mode, you can use the following keys: * **Character keys and Shift combinations** to input characters * **ENTER**, the Enter key, to create a new line * **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 within the text * **HOME**/**END** to move the cursor to the start/end of the line * **Page Up**/**Page Down** to scroll up/down * **Insert** to toggle the cursor between Insert/Replace mode; the cursor will become a vertical line/underline * **ESC** to exit Insert Mode and switch to Command Mode ### Command-Line Mode Pressing : (English colon) in Command Mode enters Command-Line Mode. In Command-Line Mode, you can enter single or multiple character commands; there are many available commands. In Command-Line Mode, the basic commands are (the colon is omitted): * `:w`: Save the file. * `:q`: Quit the Vim editor. * `:wq`: Save the file and quit the Vim editor. * `:q!`: Force quit the Vim editor without saving changes. You can press the ESC key at any time to exit Command-Line Mode. Simply put, we can think of these three modes as represented by the diagram below: !(#) * * * ## vi/vim Usage Examples ### Using vi/vim to Enter Normal Mode If you want to use vi to create a file named tutorial.txt, you can do this: $ vim tutorial.txt Simply entering **vi filename** will take you into vi's Normal Mode. Please note that you must add the filename after vi, regardless of whether the file exists! !(#) ### Press i to Enter Insert Mode (also called Edit Mode) and Start Editing Text In Normal Mode, you can enter Insert Mode by pressing i, o, a, etc.! In Edit Mode, you will notice that –INSERT– appears in the status bar at the bottom left, indicating that you can input any characters. At this point, except for the **Esc** key, all other keys on the keyboard can be considered as regular input buttons, so you can perform any editing. !(#) ### Press the ESC Button to Return to Normal Mode Okay, assuming I have finished editing it as shown above, how should I exit? Yes! That's right! Just press the **Esc** button! Immediately, you will notice that – INSERT – at the bottom left of the screen disappears! ### In Normal Mode, Press **:wq** to Save and Leave vi OK, we need to save the file. The command to save and leave is simple; just enter **:wq** to save and exit! !(#) OK! This way, we have successfully created a tutorial.txt file. * * * ## vi/vim Key Explanation Besides the simple examples of i, Esc, and :wq above, vim actually has many more keys that can be used. ### Part 1: Cursor Movement, Copy/Paste, Search/Replace, etc. Available in Normal Mode | Cursor Movement Methods | | --- | | h or Left Arrow Key (←) | Move the cursor one character to the left | | j or Down Arrow Key (↓) | Move the cursor one character down | | k or Up Arrow Key (↑) | Move the cursor one character up | | l or Right Arrow Key (β†’) | Move the cursor one character to the right | | If you place your right hand on the keyboard, you will find that hjkl are arranged together, so you can use these four keys to move the cursor. If you want to move multiple times, for example, move down 30 lines, you can use the combination keys "30j" or "30↓", that is, add the desired number of times (number) and then press the action! | | + | Move the screen 'down' one page, equivalent to the key (Commonly used) | | + | Move the screen 'up' one page, equivalent to the key (Commonly used) | | + | Move the screen 'down' half a page | | + | Move the screen 'up' half a page | | + | Move the cursor to the first non-space character of the next line | | - | Move the cursor to the first non-space character of the previous line | | n | n represents a 'number', for example, 20. After pressing the number and then the spacebar, the cursor will move n characters to the right on this line. For example, 20 will move the cursor 20 characters forward. | | 0 or Function Key | This is the number '0': move to the first character of this line (Commonly used) | | $ or Function Key | Move to the last character of this line (Commonly used) | | H | Move the cursor to the first character of the top line of this screen | | M | Move the cursor to the first character of the middle line of this screen | | L | Move the cursor to the first character of the bottom line of this screen | | G | Move to the last line of this file (Commonly used) | | nG | n is a number. Move to the nth line of this file. For example, 20G will move to the 20th line of this file (can be used with :set nu) | | gg | Move to the first line of this file, equivalent to 1G! (Commonly used) | | n | n is a number. Move the cursor down n lines (Commonly used) | | Search and Replace | | /word | Search for a string named word below the cursor. For example, to search for the string vbird in the file, just enter /vbird! (Commonly used) | | ?word | Search for a string named word above the cursor. | | n | This n is an English key. It represents repeating the previous search action. For example, if we just executed /vbird to search for vbird downwards, pressing n will continue searching downwards for the next string named vbird. If we executed ?vbird, then pressing n will continue searching upwards for the string named vbird! | | N | This N is an English key. It is the opposite of n, performing the previous search action in 'reverse'. For example, after /vbird, pressing N means searching 'upwards' for vbird. | | Using /word with n and N is very helpful! It allows you to repeatedly find the keywords you are searching for! | | :n1,n2s/word1/word2/g | n1 and n2 are numbers. Search for the string word1 between lines n1 and n2, and replace that string with word2! For example, to search for vbird between lines 100 and 200 and replace it with VBIRD: ' :100,200s/vbird/VBIRD/g '. (Commonly used) | | :1,$s/word1/word2/g or :%s/word1/word2/g | Search for the string word1 from the first line to the last line, and replace that string with word2! (Commonly used) | | :1,$s/word1/word2/gc or :%s/word1/word2/gc | Search for the string word1 from the first line to the last line, and replace that string with word2! And display a prompt character for the user to confirm (confirm) whether to replace before replacing! (Commonly used) | | Delete, Copy, and Paste | | x, X | Within a line, x deletes one character backwards (equivalent to the key), X deletes one character forwards (equivalent to the key) (Commonly used) | | nx | n is a number, delete n characters consecutively backwards. For example, to delete 10 characters consecutively, '10x'. | | dd | Cut the entire line where the cursor is located (Commonly used), use p/P to paste. | | ndd | n is a number. Cut n lines downwards from the cursor, for example, 20dd cuts 20 lines (Commonly used), use p/P to paste. | | d1G | Delete all data from the cursor to the first line | | dG | Delete all data from the cursor to the last line | | d$ | Delete from the cursor to the last character of that line | | d0 | That is the number 0, delete from the cursor to the first character of that line | | yy | Copy the entire line where the cursor is located (Commonly used) | | nyy | n is a number. Copy n lines downwards from the cursor, for example, 20yy copies 20 lines (Commonly used) | | y1G | Copy all data from the cursor line to the first line | | yG | Copy all data from the cursor line to the last line | | y0 | Copy all data from the character under the cursor to the start of the line | | y$ | Copy all data from the character under the cursor to the end of the line | | p, P | p pastes the copied data below the cursor line, P pastes it above the cursor line! For example, if the cursor is currently on line 20 and 10 lines of data have been copied, pressing p will paste those 10 lines after the original line 20, starting from line 21. But if you press P? Then the original line 20 will be pushed to become line 30. (Commonly used) | | J | Combine the data of the line where the cursor is located with the next line into one line | | c | Delete multiple data repeatedly, for example, delete 10 lines downwards, | | u | Undo the previous action. (Commonly used) | | +r | Redo the previous action. (Commonly used) | | This u and +r are very commonly used commands! One is undo, and the other is redo~ Using these two function keys, your editing, hehe! Will be very happy! | | . | Don't doubt it! This is the period! It means repeating the previous action. If you want to repeat delete, repeat paste, etc., just press the period '.'! (Commonly used) | ### Part 2: Explanation of Buttons Available to Switch from Normal Mode to Edit Mode | Enter Insert or Replace Edit Mode | | --- | | i, I | Enter Insert Mode: i is 'input from the current cursor position', I is 'input starting from the first non-space character of the current line'. (Commonly used) | | a, A | Enter Insert Mode: a is 'input starting from the next character of the current cursor position', A is 'input starting from the last character of the current cursor line'. (Commonly used) | | o, O | Enter Insert Mode: This is the lowercase and uppercase of the English letter o. o inputs a new line below the current cursor line; O inputs a new line above the current cursor line! (Commonly used) | | r, R | Enter Replace Mode: r only replaces the character under the cursor once; R will continuously replace the text under the cursor until ESC is pressed; (Commonly used) | | Among the above keys, ' --INSERT-- ' or ' --REPLACE-- ' will appear at the bottom left of the vi screen. You can tell what the action is from the name!! Pay special attention, as we mentioned above, when you want to input characters in the file, you must see INSERT or REPLACE at the bottom left before you can input! | | | Exit Edit Mode and return to Normal Mode (Commonly used) | ### Part 3: Explanation of Buttons Available to Switch from Normal Mode to Command-Line Mode | Command-Line Save, Exit, etc. Commands | | --- | | :w | Write the edited data to the hard disk file (Commonly used) | | :w! | If the file attribute is 'read-only', force write to the file. However, whether it can be written or not still depends on your file permissions for that file! | | :q | Leave vi (Commonly used) | | :q! | If the file has been modified and you don't want to save it, use ! to force leave without saving the file. | | Note that the exclamation mark (!) in vi often has the meaning of 'force'~ | | :wq | Save and leave, if it is :wq! then it is force save and leave (Commonly used) | | ZZ | This is uppercase Z! If modified, save the current file, then exit! The effect is the same as (save and exit) | | ZQ | Do not save, force exit. The effect is the same as :q!. | | :w | Save the edited data as another file (similar to Save As) | | :r | Read the data of another file into the edited data. That is, add the content of 'filename' after the cursor line | | :n1,n2 w | Save the content from n1 to n2 as the file filename. | | :! command | Temporarily leave vi to the command line mode to execute command and display the result! For example, ' :! ls /home ' allows you to view the file information output by ls under /home in vi! | | Changes to vim Environment | | :set nu | Display line numbers; after setting, the line number will be displayed as a prefix on each line | | :set nonu | Opposite to set nu, cancel line numbers! | Pay special attention, in vi/vim, numbers are very meaningful! Numbers usually mean repeating an action several times! They can also mean going to the nth something. For example, to delete 50 lines, use '50dd', right? The number is added before the action, like if I want to move down 20 lines? Then '20j' or '20↓' will do.
← Linux YumRef Html Entities Z β†’