English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
All Unix Like systems will have the vi document editor built-in, while other document editors may not exist.
But at present, we use the vim editor more.
vim has the ability to edit programs, and can actively distinguish the correctness of syntax with font color, which is convenient for program design.
Related articles:The Most Comprehensive Vim Shortcut Key Map — From Beginner to Advanced
Vim is a text editor developed from vi. It has a particularly rich set of convenient programming features such as code completion, compilation, and error jump, and is widely used by programmers.
In simple terms, vi is an old-fashioned word processor, but it is very complete in terms of functions, but there are still places for improvement. Vim can be said to be a very useful tool for program developers.
Even the official website of vim (http://www.vim.org) Even myself says vim is a program development tool rather than a word processing software.
vim keyboard map:
basically vi/vim is divided into three modes, namelyCommand mode (Command mode),Insert mode (Insert mode)andBottom line command mode (Last line mode). The functions of these three modes are respectively:
The user just started vi/vim, enters command mode.
In this state, keystrokes will be recognized by Vim as commands, not input characters. For example, if we press i now, we will not input a character; i is treated as a command.
Here are some commonly used commands:
i Switch to insert mode to input characters.
x Delete the character at the current cursor position.
: Switch to bottom line command mode to input commands on the bottom line.
If you want to edit text: Start Vim, enter command mode, press i, and switch to insert mode.
Command mode only has some basic commands, so you still need to rely on the bottom line command mode to input more commands.
Press i in command mode to enter insert mode.
In input mode, you can use the following keys:
character keys and Shift combinationto input characters
ENTERto enter a new line
BACK SPACEto delete the character before the cursor
DELto delete the character after the cursor
arrow keysto move the cursor in the text
HOME/ENDto move the cursor to the beginning of the line/end of line
Page Up/Page Downto scroll up/scroll down
Insertto switch the cursor to input/replace mode, the cursor will become a vertical line/underline
ESCto exit input mode and switch to command mode
Press: (English colon) to enter the底线 command mode.
The底线 command mode can input a single or multiple character commands, and there are many available commands.
In the底线 command mode, the basic commands are (the colon has been omitted):
q Exit program
w Save file
Press the ESC key at any time to exit the底线 command mode.
Simply put, we can think of the following icons to represent these three modes:
If you want to use vi to create a file named w3When editing the file codebox.txt, you can do it this way:
$ vim w3codebox.txt
Directly input vi Filename You can enter vi's normal mode. Please note, remember to add the filename after vi, regardless of whether the file exists or not!
In normal mode, just press i, o, a, etc. to enter input mode!
In edit mode, you can find that –INSERT will appear in the status bar at the bottom left.- This indicates that you can input any character.
At this time, the keyboard except Esc In addition to this key, all other keys can be treated as general input keys, so you can perform any editing.
Alright, assuming I have finished editing it according to the style above, then how should I exit? Yes! That's right! Just press Esc This button! You will soon find that the – INSERT – at the bottom left of the screen has disappeared!
OK, we need to archive, and the command to save and leave is very simple, input :wq You can save and leave now!
OK! In this way, we have successfully created a w3the file codebox.txt.
In addition to the simple examples of i, Esc, and :wq mentioned above, vim actually has a very large number of keys that can be used.
Methods to move the cursor | |
---|---|
h or the left arrow key (←) | Move the cursor one character to the left |
j or the down arrow key (↓) | Move the cursor one character down |
k or the up arrow key (↑) | Move the cursor one character up |
l or the 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 buttons to move the cursor. If you want to move multiple times, for example, to move down 30 lines, you can use "33The combination key '0↓"', That is, add the desired number of times and press the action! | |
[Ctrl] + [f] | The screen moves down one page, equivalent to the [Page Down] key (commonly used) |
[Ctrl] + [b] | The screen moves up one page, equivalent to the [Page Up] key (commonly used) |
[Ctrl] + [d] | The screen moves down half a page |
[Ctrl] + [u] | The screen moves up half a page |
+ | Move the cursor to the line below the non-space character |
- | Move the cursor to the line above the non-space character |
n<space> | That 'n' represents 'number', for example 20 . Press the number key and then the space bar, the cursor will move right n characters on this line characters. For example 20<space> will move the cursor to the right 20 characters away. |
0 or function key [Home] | This number '0' moves to the first character of this line (commonly used) |
$ or function key [End] | Move to the end of this line (commonly used) |
H | The cursor moves to the first character of the top line of this screen |
M | The cursor moves to the first character of the central line of this screen |
L | The cursor moves 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 first line of this file 20 line (can be used with :set nu) |
gg | Move to the first line of this file, which is equivalent to 1G ah! (commonly used) |
n<Enter> | n is a number. The cursor moves down n lines (commonly used) |
Search and Replace | |
/word | Search for a string named 'word' below the cursor. For example, to search for 'vbird' in a file For this string, input /Enter 'vbird'! (commonly used) |
?word | Search for a string named 'word' above the cursor. |
n | This 'n' is an English keyboard key. It represents repeating the previous search action. For example, If we just executed /Search downward for the string 'vbird'. Pressing 'n' will continue to search for the next string named 'vbird'. string. If it is executing ?vbird, then pressing n will continue to search for the string named vbird upwards! |
N | This N is an English key. It is exactly opposite to n, for 'reverse' to perform the previous search action. For example /vbird, press N then it means 'up' to search vbird. |
Use /word combined with n and N is very helpful! It can help you repeatedly find some keywords you are searching for! | |
:n1,n2s/word1/word2/g | n1 and n2 is a number. In the n1 and n2 lines between search word1 this string, and replace the string with
word2 ! For example, in 100 to 200 lines between search vbird and replace it with VBIRD then: 『:100,200s/vbird/VBIRD/g』. (commonly used) |
:1$s/word1/word2/g or :%s/word1/word2/g | Search for word from the first line to the last line1 string, and replace the string with word2 ! (commonly used) |
:1$s/word1/word2/gc or :%s/word1/word2/gc | Search for word from the first line to the last line1 string, and replace the string with word2 ! And display a prompt character to the user for confirmation (confirm) Do you need to replace! (commonly used) |
Delete, copy, and paste | |
x, X | In a line of text, x is to delete one character backward (equivalent to [del] key), X is to delete one character forward (equivalent to [backspace] or the backspace key) (commonly used) |
nx | n is a number, delete n consecutive characters. For example, I want to delete 10 characters, 『10』. |
dd | Delete the entire line where the cursor is (commonly used) |
ndd | n is a number. Delete the n lines below the cursor, for example 20dd is to delete 20 lines (commonly used) |
d1G | Delete all data from the cursor to the first line |
dG | Delete all data from the cursor to the last line |
d$ | Delete the character at the cursor to the last character of the line |
d0 | That is the number 0, delete the character at the cursor to the first character of the line |
yy | Copy the line where the cursor is (commonly used) |
nyy | n is a number. Copy the n lines below the cursor, for example 20yy is to copy 20 line (commonly used) |
y1G | Copy the line where the cursor is to the beginning of all lines |
yG | Copy the line where the cursor is to the end of all lines |
y0 | Copy all data from the character at the cursor to the beginning of the line |
y$ | Copy all data from the character at the cursor to the end of the line |
p, P | p is to paste the copied data below the cursor, while P is to paste it above the cursor! For example, the cursor is currently at the 20 lines, and it has been copied 10 If there is data, press p after that. that 10 line data will be pasted on the original 20 line after, that is, from 21 line starts pasting. But if you press P? Then the original 20 line will be pushed to become 30 line. (commonly used) |
J | Combine the data of the current cursor line and the next line into the same line |
c | Repeat delete multiple data, such as downward delete 10 line, [ 10cj ] |
u | Undo the previous action (commonly used) |
[Ctrl]+r | Redo the previous action (commonly used) |
This u with [Ctrl]+r is a very commonly used command! One is to undo, and the other is to redo! With these two functional keys, your editing, hehe! It's very pleasant! | |
• | Don't doubt it! This is the decimal point! It means to repeat the previous action. If you want to repeat delete, repeat paste, and other actions, just press the decimal point '•'! (commonly used) |
Enter input or replace editing mode | |
---|---|
i, I | Enter input mode (Insert mode): i is 'starting from the current cursor position', I is 'starting from the first non-space character of the current line'. (commonly used) |
a, A | Enter input mode (Insert mode): a is 'starting from the next character of the current cursor position', A is 'starting from the last character of the cursor line' (commonly used) |
o, O | Enter input mode (Insert mode): This is the case with the English letter o in uppercase and lowercase. o is to input a new line below the current cursor position; O to input a new line above the current cursor position (commonly used)! |
r, R | Enter replace mode (Replace mode): r will only replace the character at the cursor once; R will continue to replace the text at the cursor until you press ESC stop; (commonly used) |
Among these keys, in the lower left corner of the vi screen, there will be a『--INSERT--』or『--REPLACE--』 As the name suggests, this action!! Especially note that we also mentioned above, when you want to input characters into the document, You must see INSERT or REPLACE at the lower left corner to input, otherwise! | |
[Esc] | Exit editing mode and return to general mode (commonly used) |
Command line storage, exit, and other instructions | |
---|---|
: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 you can write or not, It's still related to the file permissions of the document! |
:q | Exit vi (commonly used) |
:q! | If you have modified a file and don't want to save it, use ! to force exit without saving the file. |
Pay attention, the exclamation mark (!) often means 'force' in vi~ | |
:wq | Exit after saving, if :wq! is used, it will be forced to save and exit (commonly used) |
ZZ | That's the capital Z! If you have made changes, save the current file and then exit! Equivalent to (save and exit) |
ZQ | Do not save and force exit. Equivalent to :q! |
:w [filename] | Store the edited data into another file (similar to save as) |
:r [filename] | Read another file's data into the editing data. That is, to『filename』 This file content is added to the line behind the cursor |
:n1,n2 w [filename] | Move n1 to n2 is stored in the file filename. |
:! command | Temporarily leave vi to execute command in command line mode and display the result! For example 『:! ls /『home』can be viewed in vi /The file information output by ls under home! |
Changes in vim environment | |
:set nu | Show line numbers, after setting, the line number will be displayed before each line |
:set nonu | 相反 to set nu, to cancel line numbers! |
Pay special attention to vi/In vim, numbers are very meaningful! Numbers usually mean doing something repeatedly! They may also represent going to the nth something.
For example, to delete 50 line, then use『50dd』, right! Numbers are added before actions, such as moving down 20 lines? That is『20j』or『20↓』can be done.